Sitemap
Nikhitha Perapola

Nikhitha Perapola

Leveraging technology to extract meaningful insights :)

HackerRank SQL Practice- Aggregation

7 min readAug 11, 2023

(Aggregate functions)

  1. Revising Aggregations — The Count Function

Query a count of the number of cities in CITY having a Population larger than 100,000.

Input Format

The CITY table is described as follows:

Solution:

select count(id) from city where population>100000;

2. Revising Aggregations — The Sum Function

Query the total population of all cities in CITY where District is California.

Input Format

The CITY table is described as follows:

Solution:

select sum(population) from city where district = 'California';

3. Revising Aggregations — Averages

Query the average population of all cities in CITY where District is California.

Input Format

The CITY table is described as follows:

Solution:

select avg(population) from city where district = 'California';

4. Average Population

Query the average population for all cities in CITY, rounded down to the nearest integer.

Input Format

The CITY table is described as follows:

Solution:

select floor(avg(population)) from city;

5. Japan Population

Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.

Input Format

The CITY table is described as follows:

Solution:

select sum(population) from city where countrycode = 'JPN';

6. Population Density Difference

Query the difference between the maximum and minimum populations in CITY.

Input Format

The CITY table is described as follows:

Solution:

select max(population)-min(population) from city;

7. The Blunder

Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard’s 0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeros removed), and the actual average salary.

Write a query calculating the amount of error (i.e.: actual — miscalculated average monthly salaries), and round it up to the next integer.

Input Format

The EMPLOYEES table is described as follows:

Note: Salary is per month.

Sample Input

Sample Output

2061

Solution:

select ceil(avg(salary)-avg(replace(salary,'0',''))) from employees;

8. Top Earners

We define an employee’s total earnings to be their monthly salary*months worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.

Input Format

The Employee table containing employee data for a company is described as follows:

where employee_id is an employee’s ID number, name is their name, months is the total number of months they’ve been working for the company, and salary is the their monthly salary.

Sample Input

Press enter or click to view image in full size

Sample Output

69952 1

Solution:

select salary*months , count(*) from employee
group by salary*months order by salary*months desc limit 1;

9. Weather Observation Station 2

Query the following two values from the STATION table:

  1. The sum of all values in LAT_N rounded to a scale of 2 decimal places.
  2. The sum of all values in LONG_W rounded to a scale of 2 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Get Nikhitha Perapola’s stories in your inbox

Join Medium for free to get updates from this writer.

Output Format

Your results must be in the form:

lat lon

Solution:

select round(sum(lat_n),2), round(sum(long_w),2) from station;

10. Weather Observation Station 13

Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880 and less than 137.2345. Truncate your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

select round(sum(lat_n),4) from station where lat_n>38.7880 and lat_n<137.2345;

11. Weather Observation Station 14

Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345. Truncate your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

select round(max(lat_n),4) from station where lat_n<137.2345;

12. Weather Observation Station 15

Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345. Round your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

SELECT ROUND(LONG_W,4)
FROM STATION
WHERE LAT_N = (SELECT MAX(LAT_N) FROM STATION WHERE LAT_N < 137.2345);

13. Weather Observation Station 16

Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780. Round your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

select round(lat_n,4) from station where lat_n>38.7780 order by 1 limit 1;

14. Weather Observation Station 17

Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780. Round your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

select round(long_w,4) from station where lat_n>38.7780 order by lat_n limit 1;

15. Weather Observation Station 18

Consider P1(a,b) and P2(c,d) to be two points on a 2D plane.

  • a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  • b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  • c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  • d happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

SELECT ROUND((MAX(LAT_N) - MIN(LAT_N) + MAX(LONG_W) - MIN(LONG_W)), 4) AS D
FROM STATION

16. Weather Observation Station 19

Consider P1(a,b) and P2(c,d) to be two points on a 2D plane where (a,b) are the respective minimum and maximum values of Northern Latitude (LAT_N) and (c,d) are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.

Query the Euclidean Distance between points P1 and P2 and format your answer to display 4 decimal digits.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

select round(sqrt(power(max(LAT_N) - min(LAT_N), 2) + power(max(LONG_W) - min(LONG_W), 2)), 4)
FROM STATION;

17. Weather Observation Station 20

A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 4 decimal places.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Solution:

SELECT ROUND(LAT_N, 4) AS median 
FROM (SELECT LAT_N, ROW_NUMBER() OVER (ORDER BY LAT_N) AS rn_asc, ROW_NUMBER() OVER (ORDER BY LAT_N DESC) AS rn_desc FROM STATION) AS subquery
WHERE rn_asc = rn_desc OR rn_asc + 1 = rn_desc OR rn_asc = rn_desc + 1;

Hope this was helpful!

Do let me know if any code could be optimized or written better. Let’s learn and grow together.

Follow for more ❤, Thanks :)

--

--

Nikhitha Perapola
Nikhitha Perapola

Written by Nikhitha Perapola

Leveraging technology to extract meaningful insights :)