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
Show the total number of products.
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Keyboard | Office | 25 | 40 |
| 2 | Mouse | Office | 15 | 0 |
| 3 | Screen | Display | 180 | 12 |
| 4 | Headset | Audio | 60 | 8 |
| 5 | Webcam | Office | 45 | 0 |
| 6 | Speaker | Audio | 35 | 25 |
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
Show the highest salary among all employees.
| id | name | department | salary |
|---|---|---|---|
| 1 | Alice | Finance | 32000 |
| 2 | Bob | IT | 41000 |
| 3 | Claire | Finance | 38000 |
| 4 | David | HR | 29000 |
| 5 | Emma | IT | 50000 |
| 6 | Farid | IT | 47000 |
| 7 | Gaelle | HR | 33000 |
| 8 | Hugo | Finance | 44000 |
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
Show the number of draws (both teams scored the same number of goals) and the total number of goals scored in those matches.
| id | played_on | home_id | away_id | home_goals | away_goals |
|---|---|---|---|---|---|
| 1 | 2025-08-02 | 1 | 2 | 2 | 1 |
| 2 | 2025-08-03 | 3 | 4 | 0 | 0 |
| 3 | 2025-08-09 | 5 | 1 | 1 | 3 |
| 4 | 2025-08-10 | 2 | 3 | 2 | 2 |
| 5 | 2025-08-16 | 4 | 5 | 3 | 1 |
| 6 | 2025-08-17 | 1 | 3 | 1 | 0 |
| 7 | 2025-08-23 | 2 | 4 | 0 | 1 |
| 8 | 2025-08-24 | 3 | 5 | 2 | 3 |
| 9 | 2025-08-30 | 4 | 1 | 1 | 1 |
| 10 | 2025-08-31 | 5 | 2 | 0 | 2 |
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) |
|---|---|
| 3 | 6 |
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.