SQL exercises with solutions: ORDER BY and LIMIT

SQL exercises with solutions: ORDER BY and LIMIT

Updated on

ORDER BY sorts (ASC ascending, DESC descending); LIMIT keeps the first n rows. These 6 exercises range from level 1 to level 2; they use the syntax of SQLite, SpeedQL’s SQL engine.

Tip: Add a second sort column to break ties.

Read the “ORDER BY, LIMIT” card in the cheat sheet

Exercise 1 · level 1

Show the name and salary of every employee, from the lowest to the highest salary.

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

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT , 
FROM 
ORDER BY  ASC
Show the solution
SELECT name, salary
FROM employees
ORDER BY salary ASC;

Expected result (8 rows, in this order):

namesalary
David29000
Alice32000
Gaelle33000
Claire38000
Bob41000
Hugo44000
Farid47000
Emma50000

Exercise 2 · level 2

Show the name, department and salary of the employees, sorted by department (alphabetical order) and then, within each department, from the highest to the lowest salary.

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

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT , , 
FROM 
ORDER BY  ASC,  DESC
Show the solution
SELECT name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;

Expected result (8 rows, in this order):

namedepartmentsalary
HugoFinance44000
ClaireFinance38000
AliceFinance32000
GaelleHR33000
DavidHR29000
EmmaIT50000
FaridIT47000
BobIT41000

Exercise 3 · level 2

Show the name of the 3 cheapest products, from the cheapest to the most expensive.

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

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT 
FROM 
ORDER BY  ASC
LIMIT 
Show the solution
SELECT name
FROM products
ORDER BY price ASC
LIMIT 3;

Expected result (3 rows, in this order):

name
Mouse
Keyboard
Speaker

Exercise 4 · level 2

Show the name of the 2 highest-paid employees, highest salary first.

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

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT 
FROM 
ORDER BY  DESC
LIMIT 
Show the solution
SELECT name
FROM employees
ORDER BY salary DESC
LIMIT 2;

Expected result (2 rows, in this order):

name
Emma
Farid

Exercise 5 · level 2

Show the name and number of goals of the 3 top scorers, from the most goals to the fewest.

Table players (9 rows)
idnameteam_idpositiongoals
1Alex Moreau1FW9
2Bilal Sow1MF4
3Carl Weber2FW7
4Diego Ruiz2DF1
5Eli Novak3FW6
6Femi Adeyemi4FW11
7Goran Petrov4GK0
8Hugo Lamy5MF5
9Ivan Kral5DF2
Show the hint

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT , 
FROM 
ORDER BY  DESC
LIMIT 
Show the solution
SELECT name, goals
FROM players
ORDER BY goals DESC
LIMIT 3;

Expected result (3 rows, in this order):

namegoals
Femi Adeyemi11
Alex Moreau9
Carl Weber7

Exercise 6 · level 2

Show the city, day and maximum temperature of the 3 hottest readings, from the hottest down (on ties, by day then by city).

Table readings (15 rows)
idcitydaytemp_maxtemp_minrain_mm
1Paris2025-07-0124150
2Paris2025-07-0227170
3Paris2025-07-0322164.5
4Paris2025-07-04191412
5Paris2025-07-0523130
6Lyon2025-07-0126160
7Lyon2025-07-0229180
8Lyon2025-07-0331190
9Lyon2025-07-0424178.5
10Lyon2025-07-0522153
11Marseille2025-07-0130210
12Marseille2025-07-0232220
13Marseille2025-07-0333230
14Marseille2025-07-0429210
15Marseille2025-07-0528200
Show the hint

Topics to use: ORDER BY, LIMIT.

Query structure:

SELECT , , 
FROM 
ORDER BY  DESC, , 
LIMIT 
Show the solution
SELECT city, day, temp_max
FROM readings
ORDER BY temp_max DESC, day, city
LIMIT 3;

Expected result (3 rows, in this order):

citydaytemp_max
Marseille2025-07-0333
Marseille2025-07-0232
Lyon2025-07-0331

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 “ORDER BY, LIMIT” questions