SQL exercises with solutions: COUNT, SUM, AVG, MIN, MAX

SQL exercises with solutions: COUNT, SUM, AVG, MIN, MAX

Updated on

Aggregates sum up many rows into one value: COUNT, SUM, AVG, MIN, MAX. These 3 exercises range from level 1 to level 2; they use the syntax of SQLite, SpeedQL’s SQL engine.

Tip: COUNT(*) counts rows; COUNT(column) skips NULL values.

Read the “COUNT, SUM, AVG, MIN, MAX” card in the cheat sheet

Exercise 1 · level 1

Show the total number of products.

Table products (6 rows)
idnamecategorypricestock
1KeyboardOffice2540
2MouseOffice150
3ScreenDisplay18012
4HeadsetAudio608
5WebcamOffice450
6SpeakerAudio3525
Show the hint

Topics to use: COUNT, SUM, AVG, MIN, MAX.

Query structure:

SELECT COUNT(*)
FROM 
Show the solution
SELECT COUNT(*)
FROM products;

Expected result (1 row):

COUNT(*)
6

Exercise 2 · level 1

Show the highest salary among all employees.

Table employees (8 rows)
idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000
5EmmaIT50000
6FaridIT47000
7GaelleHR33000
8HugoFinance44000
Show the hint

Topics to use: COUNT, SUM, AVG, MIN, MAX.

Query structure:

SELECT MAX()
FROM 
Show the solution
SELECT MAX(salary)
FROM employees;

Expected result (1 row):

MAX(salary)
50000

Exercise 3 · level 2

Show the number of draws (both teams scored the same number of goals) and the total number of goals scored in those matches.

Table matches (10 rows)
idplayed_onhome_idaway_idhome_goalsaway_goals
12025-08-021221
22025-08-033400
32025-08-095113
42025-08-102322
52025-08-164531
62025-08-171310
72025-08-232401
82025-08-243523
92025-08-304111
102025-08-315202
Show the hint

Topics to use: WHERE (filters), COUNT, SUM, AVG, MIN, MAX.

Query structure:

SELECT COUNT(*), SUM( + )
FROM 
WHERE  = 
Show the solution
SELECT COUNT(*), SUM(home_goals + away_goals)
FROM matches
WHERE home_goals = away_goals;

Expected result (1 row):

COUNT(*)SUM(home_goals + away_goals)
36

Practice with automatic checking

In SpeedQL, you write your query and it is checked straight away, on these tables and then on a hidden dataset.

Practice on the 7 “COUNT, SUM, AVG, MIN, MAX” questions