Hacker Rank SQL Challenges Aggregation
Problems
- Revising Aggregations - The Count Function
 - Revising Aggregations - The Sum Function
 - Revising Aggregations - Averages
 - Average Population
 - Japan Population
 - Population Density Difference
 - The Blunder
 - Top Earners
 - Weather Observation Station 2
 - Weather Observation Station 13
 - Weather Observation Station 14
 - Weather Observation Station 15
 - Weather Observation Station 16
 - Weather Observation Station 17
 - Weather Observation Station 18
 - Weather Observation Station 19
 - Weather Observation Station 20
 
Revising Aggregations - The Count Function
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 COUNT(name)from city.- Filter population greater than 100000.
 Solution:
SELECT COUNT(name) FROM city WHERE population > 100000;
Revising Aggregations - The Sum Function
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 SUM(population)from city.- Filter district equal to ‘california’.
 Solution:
SELECT SUM(population) FROM city WHERE LOWER(district) = 'california';
Revising Aggregations - Averages
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 AVG(population)from city.- Filter district equal to ‘california’.
 Solution:
SELECT AVG(population) FROM city WHERE LOWER(district) = 'california';
Average Population
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 AVG(population)from city and round the value usingROUNDfunction.Solution:
SELECT ROUND(AVG(population)) FROM city;
Japan Population
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 SUM(population)from city.- Filter
 countrycodeequal to ‘JPN’.Solution:
SELECT SUM(population) FROM city WHERE UPPER(countrycode) = 'JPN';
Population Density Difference
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select difference
 MAX(population)andMIN(population)from city.Solution:
SELECT MAX(population) - MIN(population) FROM city;
The Blunder
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select difference
 AVG(salary)andAVG(REGEXP_REPLACE(salary, '[0]'))from employees and usingCEILfunction round the result to next integer.Solution:
SELECT CEIL(AVG(salary) - AVG(REGEXP_REPLACE(salary, '[0]'))) FROM employees;
Top Earners
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select product of months and salary and count of distinct employee_id.
 - Group by product of months and salary
 - Filter product of months and salary equal to
 MAX(product of months and salary)from employee table.Solution:
SELECT months * salary, COUNT(DISTINCT employee_id) FROM employee GROUP BY months * salary HAVING months * salary = (SELECT MAX(months * salary) FROM employee);
Weather Observation Station 2
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select sum of
 lat_nfield and sum oflong_wfieldROUNDupto 2 decimal.Solution:
SELECT ROUND(SUM(lat_n),2), ROUND(SUM(long_w),2) FROM station;
Weather Observation Station 13
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select sum of
 lat_nfield andROUNDupto 4 digits.- Filter
 lat_nBETWEEN 38.7880 and 137.2345Solution:
SELECT ROUND(SUM(lat_n),4) FROM station WHERE lat_n BETWEEN 38.7880 AND 137.2345;
Weather Observation Station 14
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select max of
 lat_nfield andROUNDupto 4 digits.- Filter
 lat_nless than 137.2345Solution:
SELECT ROUND(MAX(lat_n),4) FROM station WHERE lat_n < 137.2345;
Weather Observation Station 15
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 long_wfield andROUNDupto 4 digits.- Filter
 lat_nequal to (MAX(lat_n)FROM station FILTERlat_nless than 137.2345)Solution:
SELECT ROUND(long_w,4) FROM station WHERE lat_n = (SELECT MAX(lat_n) FROM station WHERE lat_n < 137.2345);
Weather Observation Station 16
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select min of
 lat_nfield andROUNDupto 4 digits.- Filter
 lat_ngreater than 38.7780.Solution:
SELECT ROUND(MIN(lat_n),4) FROM station WHERE lat_n > 38.7780;
Weather Observation Station 17
Problem -> URL
Difficulty Level: Easy
Pseudocode:
- Select
 long_wfield andROUNDupto 4 digits.- Filter
 lat_nequal to (MIN(lat_n)FROM station FILTERlat_ngreater than 38.7780)Solution:
SELECT ROUND(long_w,4) FROM station WHERE lat_n = (SELECT MIN(lat_n) FROM station WHERE lat_n > 38.7780);
Weather Observation Station 18
Problem -> URL
Difficulty Level: Medium
Refer Manhattan Distance
Pseudocode:
- Calculate
 Manhattan Distanceusing the formula andROUNDthe result upto 4 digits.Solution:
SELECT ROUND(ABS(MIN(lat_n) - MAX(lat_n)) + ABS(MIN(long_w) - MAX(long_w)), 4) FROM station;
Weather Observation Station 19
Problem -> URL
Difficulty Level: Medium
Refer Euclidean Distance
Pseudocode:
- Calculate
 Euclidean Distanceusing the formula andROUNDthe result upto 4 digits.Solution:
SELECT ROUND(SQRT(POWER((MIN(lat_n))-MAX(lat_n),2) + POWER((MIN(long_w)-MAX(long_w)),2)), 4) FROM station;
Weather Observation Station 20
Problem -> URL
Difficulty Level: Medium
Refer Median
Pseudocode:
- Calculate
 Medianusing the formula andROUNDthe result upto 4 digits.Solution:
SELECT ROUND(MEDIAN(lat_n), 4) FROM station;