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

More exercises: level 4 of the SQL course, “Sorting, limiting, removing duplicates”

These 5 exercises come from level 4 of the course, on other tables. Each one has written hints and its solution, folded by default.

Exercise 7 · course, level 4 · Guided

Show the title and year of the movies, from oldest to newest.

The row order matters.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint: ORDER BY year sorts in ascending order.

Show the solution
SELECT title, year
FROM movies
ORDER BY year;

Expected result (10 rows):

titleyear
Paper Moon City2012
Dust and Gold2014
Night Train2015
Summer Keys2016
Deep Current2017
Blue Harbor2018
Last Signal2019
Silent Peak2020
Iron Garden2021
The Quiet Hour2022

ASC is implicit: ORDER BY year and ORDER BY year ASC are the same.

Exercise 8 · course, level 4 · Practice

Show the list of movie genres, without duplicates.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint 1 · lead: A keyword placed right after SELECT removes duplicate rows.

Hint 2 · method: SELECT DISTINCT genre …

Show the solution
SELECT DISTINCT genre
FROM movies;

Expected result (5 rows):

genre
Thriller
Drama
Comedy
Sci-Fi
Western

There are 10 movies but only 5 different genres.

Exercise 9 · course, level 4 · Practice

Show the title and the director id (director_id) of each movie, sorted by increasing id, with the movies without a director at the end. For equal ids, sort by title.

The row order matters.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint 1 · lead: In SQLite, where do NULL values end up in an ascending sort?

Hint 2 · method: ORDER BY director_id NULLS LAST, title.

Show the solution
SELECT title, director_id
FROM movies
ORDER BY director_id NULLS LAST, title;

Expected result (10 rows):

titledirector_id
Last Signal1
Night Train1
Blue Harbor2
Dust and Gold2
Iron Garden3
Silent Peak3
Summer Keys4
Paper Moon City5
The Quiet Hour5
Deep CurrentNULL

Without NULLS LAST, Deep Current would come first: SQLite puts NULL first in an ascending sort.

Exercise 10 · course, level 4 · Practice

Show the name, department and salary of the employees, sorted by department (alphabetically), then by descending salary, then by name.

The row order matters.

Table staff (10 rows)
idnamedepartmentsalarymanager_idhired
1AliceFinance52000NULL2015-03-01
2BobIT4100012018-06-15
3ClaireFinance3800012019-01-10
4DavidHR2900012020-09-01
5EmmaIT5000022017-11-20
6FaridIT4700022021-02-14
7GaelleHR3300042022-05-30
8HugoFinance4400032016-08-08
9IrisIT4700052023-01-09
10JulesFinance4400032024-03-18
Show the hints

Hint 1 · lead: Three sort criteria, each with its own direction.

Hint 2 · method: ORDER BY department, salary DESC, name.

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

Expected result (10 rows):

namedepartmentsalary
AliceFinance52000
HugoFinance44000
JulesFinance44000
ClaireFinance38000
GaelleHR33000
DavidHR29000
EmmaIT50000
FaridIT47000
IrisIT47000
BobIT41000

Each sort column breaks the ties of the previous one: Hugo and Jules both earn 44000.

Exercise 11 · course, level 4 · Challenge

Show the airline, destination and price of the second cheapest flight departing from 'CDG'.

The row order matters.

Table flights (12 rows)
idairlineorigindestdepartsduration_minpriceseats_soldcapacity
1SkyJetCDGMAD2025-06-01 08:1012589150180
2AirNovaCDGLIS2025-06-01 11:40155120160170
3BlueWingLYSFCO2025-06-02 07:309575110150
4SkyJetMADCDG2025-06-02 18:2012095170180
5AirNovaBERCDG2025-06-03 09:05110105140160
6BlueWingNCEBER2025-06-03 13:5013014090150
7SkyJetCDGFCO2025-06-04 06:4513599175180
8AirNovaLISMAD2025-06-04 16:15756560120
9BlueWingFCOLYS2025-06-05 20:3010082130150
10SkyJetCDGBER2025-06-05 10:00105110120180
11AirNovaMADLIS2025-06-06 12:25807095120
12BlueWingLYSMAD2025-06-06 15:4011579100150
Show the hints

Hint 1 · lead: Filter, sort, then skip the first row.

Hint 2 · method: Filter on origin, sort by price, then LIMIT 1 OFFSET 1.

Show the solution
SELECT airline, dest, price
FROM flights
WHERE origin = 'CDG'
ORDER BY price
LIMIT 1 OFFSET 1;

Expected result (1 row):

airlinedestprice
SkyJetFCO99

OFFSET 1 skips the cheapest flight, LIMIT 1 keeps the next one.

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