Sitemap

SQL — 104 (Basics) Aggregation

4 min readMay 24, 2022

--

Introduction

This series is the revision of SQL concepts. We are doing the basic questions, then intermediate, and so on.

All questions use the aggregation concept. You can learn more here.

Previous Posts

  1. SQL 101
  2. SQL 102 (Basics)
  3. SQL — 103 (Basics)

CITY table

┌─────────────┬──────────────┐
│ Field │ Type │
├─────────────┼──────────────┤
│ ID │ NUMBER │
│ NAME │ VARCHAR2(17) │
│ COUNTRYCODE │ VARCHAR2(3) │
│ DISTRICT │ VARCHAR2(20) │
│ POPULATION │ NUMBER │
└─────────────┴──────────────┘

Question 1

[Ref: Hackerrank]

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

Solution:

SELECT COUNT(*) FROM CITY WHERE POPULATION > 100000;

Question 2

[Ref: Hackerrank]

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

Solution:

SELECT SUM(POPULATION) FROM CITY WHERE DISTRICT = 'California';

Question 3

[Ref: Hackerrank]

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

Solution:

SELECT AVG(POPULATION) FROM CITY WHERE DISTRICT = 'California';

Question 4

[Ref: Hackerrank]

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

Solution:

SELECT ROUND(AVG(POPULATION)) FROM CITY WHERE DISTRICT = 'California';

Question 5

[Ref: Hackerrank]

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

Get Ravi raj’s stories in your inbox

Join Medium for free to get updates from this writer.

Solution:

SELECT MAX(POPULATION) - MIN(POPULATION) FROM CITY ;

Question 6

[Ref: Hackerrank]

Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table but did not realize her keyboard’s 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. The table description is shown below:

┌────────┬─────────┐
│ Field │ Type │
├────────┼─────────┤
│ ID │ Integer │
│ NAME │ String │
│ SALARY │ Integer │
└────────┴─────────┘

Note: Salary is per month

Constraints
1000 < Salary < 10⁵

Sample Input

┌────┬──────────┬────────┐
│ ID │ Name │ Salary │
├────┼──────────┼────────┤
│ 1 │ Kristeen │ 1420 │
│ 2 │ Ashley │ 2006 │
│ 3 │ Julia │ 2210 │
│ 4 │ Maria │ 3000 │
└────┴──────────┴────────┘

Sample Output

2061

Explanation:

The table below shows the salaries without zeros as they were entered by Samantha:

┌────┬──────────┬────────┐
│ ID │ Name │ Salary │
├────┼──────────┼────────┤
│ 1 │ Kristeen │ 142 │
│ 2 │ Ashley │ 26 │
│ 3 │ Julia │ 221 │
│ 4 │ Maria │ 3 │
└────┴──────────┴────────┘

Samantha computes an average salary of 98.00. The actual average salary is 2159.00.

The resulting error between the two calculations is 2159.00 — 98.00 = 2061.00. Since it is equal to the integer 2061, it does not get rounded up.

Solution:

SELECT CEIL(AVG(Salary)-AVG(REPLACE(Salary,'0',''))) FROM EMPLOYEES;

Question 7

[Ref: Hackerrank]

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 2 values as 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

┌─────────────┬──────────┬────────┬────────┐
│ employee_id │ name │ months │ Salary │
├─────────────┼──────────┼────────┼────────┤
│ 12228 │ Rose │ 15 │ 1968 │
│ 33645 │ Angela │ 1 │ 3443 │
│ 45692 │ Frank │ 17 │ 1680 │
│ 56118 │ Pathrick │ 7 │ 1345 │
│ 59725 │ Lisa │ 11 │ 2330 │
│ 74197 │ Kimberly │ 16 │ 4372 │
│ 78454 │ Bonnie │ 8 │ 1771 │
│ 83565 │ Michaeal │ 6 │ 1017 │
│ 98607 │ Todd │ 5 │ 3396 │
│ 99989 │ Joe │ 9 │ 3573 │
└─────────────┴──────────┴────────┴────────┘

Sample Output

69952 1

Explanation

The table and earnings data is depicted in the following diagram:

┌─────────────┬──────────┬────────┬────────┬──────────┐
│ employee_id │ name │ months │ salary │ earnings │
├─────────────┼──────────┼────────┼────────┼──────────┤
│ 12228 │ Rose │ 15 │ 1968 │ 29520 │
│ 33645 │ Angela │ 1 │ 3443 │ 3443 │
│ 45692 │ Frank │ 17 │ 1680 │ 27336 │
│ 56118 │ Pathrick │ 7 │ 1345 │ 9415 │
│ 59725 │ Lisa │ 11 │ 2330 │ 25630 │
│ 74197 │ Kimberly │ 16 │ 4372 │ 69952 │
│ 78454 │ Bonnie │ 8 │ 1771 │ 14168 │
│ 83565 │ Michaeal │ 6 │ 1017 │ 12102 │
│ 98607 │ Todd │ 5 │ 3396 │ 16980 │
│ 99989 │ Joe │ 9 │ 3573 │ 32157 │
└─────────────┴──────────┴────────┴────────┴──────────┘

The maximum earnings value is 69952. The only employee with earnings = 69952 is Kimberly, so we print the maximum earnings value (69952) and a count of the number of employees who have earned $69952(which is 1 ) as two space-separated values.

Solution:

SELECT (months*salary) as earnings , COUNT(*) FROM Employee GROUP BY earnings ORDER BY earnings DESC LIMIT 1 ;

--

--