SQL exercises with solutions: WHERE and filters

SQL exercises with solutions: WHERE and filters

Updated on

WHERE keeps the rows that match a condition: =, <>, >, BETWEEN, IN, LIKE, AND, OR, NOT. These 15 exercises range from level 1 to level 4; they use the syntax of SQLite, SpeedQL’s SQL engine.

Tip: Text and dates go between single quotes: department = 'IT'.

Read the “WHERE (filters)” card in the cheat sheet

Exercise 1 · level 1

Show the name and salary of the employees in the Finance department.

Table employees (4 rows)
idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT , 
FROM 
WHERE  = 
Show the solution
SELECT name, salary
FROM employees
WHERE department = 'Finance';

Expected result (2 rows):

namesalary
Alice32000
Claire38000

Exercise 2 · level 1

Show the name and price of the products in the 'Audio' category.

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

Topics to use: WHERE (filters).

Query structure:

SELECT , 
FROM 
WHERE  = 
Show the solution
SELECT name, price
FROM products
WHERE category = 'Audio';

Expected result (2 rows):

nameprice
Headset60
Speaker35

Exercise 3 · level 2

Show the name of the products in the 'Office' category that are out of stock (stock = 0).

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

Topics to use: WHERE (filters).

Query structure:

SELECT 
FROM 
WHERE  =  AND  = 
Show the solution
SELECT name
FROM products
WHERE category = 'Office' AND stock = 0;

Expected result (2 rows):

name
Mouse
Webcam

Exercise 4 · level 2

Show the name of the employees in the IT department who earn more than 45000.

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

Topics to use: WHERE (filters).

Query structure:

SELECT 
FROM 
WHERE  =  AND  > 
Show the solution
SELECT name
FROM employees
WHERE department = 'IT' AND salary > 45000;

Expected result (2 rows):

name
Emma
Farid

Exercise 5 · level 2

Show the title of the movies that last between 100 and 120 minutes (inclusive).

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 hint

Topics to use: WHERE (filters).

Query structure:

SELECT 
FROM 
WHERE  BETWEEN  AND 
Show the solution
SELECT title
FROM movies
WHERE duration BETWEEN 100 AND 120;

Expected result (4 rows):

title
Night Train
Blue Harbor
Dust and Gold
Deep Current

Exercise 6 · level 2

Show the title of the Novel or Crime books published after 2010.

Table books (10 rows)
idtitleauthor_idgenrepagesyear
1Cold River1Novel3202011
2Salt Roads2Travel2102016
3The Glass Hive3Sci-Fi4122019
4Winter Ledger1Crime2882014
5Desert Letters2Novel3562020
6Small Engines4Sci-Fi1982022
7Harbor Lights5Novel4452008
8Night Garden3Crime3012017
9Paper Birds5Poetry962012
10Open Maps4Travel2402021
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT 
FROM 
WHERE  IN (, ) AND  > 
Show the solution
SELECT title
FROM books
WHERE genre IN ('Novel', 'Crime') AND year > 2010;

Expected result (4 rows):

title
Cold River
Winter Ledger
Desert Letters
Night Garden

Exercise 7 · level 2

Show the name and class of the students born in 2009, in alphabetical order of the name.

Table students (8 rows)
idnameclassbirth_date
1AdeleA2009-03-14
2BrunoA2008-11-02
3CyrilB2009-07-21
4DinaB2009-01-30
5ElsaA2008-09-12
6FarahC2009-05-05
7GaelC2008-12-24
8HanaB2009-10-10
Show the hint

Topics to use: WHERE (filters), ORDER BY, LIMIT.

Query structure:

SELECT , 
FROM 
WHERE  BETWEEN  AND 
ORDER BY 
Show the solution
SELECT name, class
FROM students
WHERE birth_date BETWEEN '2009-01-01' AND '2009-12-31'
ORDER BY name;

Expected result (5 rows, in this order):

nameclass
AdeleA
CyrilB
DinaB
FarahC
HanaB

Exercise 8 · level 2

Show the id and duration (in minutes) of the flights that last more than 2 hours.

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 hint

Topics to use: WHERE (filters).

Query structure:

SELECT , 
FROM 
WHERE  > 
Show the solution
SELECT id, duration_min
FROM flights
WHERE duration_min > 120;

Expected result (4 rows):

idduration_min
1125
2155
6130
7135

Exercise 9 · level 2

Show the title and duration (in seconds) of the songs longer than 4 minutes, from the longest to the shortest.

Table songs (10 rows)
idtitleartist_idgenreduration_sreleased
1Glass Heart1Pop2142019-04-12
2Low Tide1Pop1982021-06-01
3Rust2Rock2562008-09-30
4Wires2Rock3012015-02-14
5Sunday Market3Afrobeat2332020-11-20
6Palm Wine3Afrobeat2452022-03-03
7Alma4Latin1892013-07-07
8Brisa4Pop2052018-05-25
9Night Drive5Electro2762021-10-10
10Pulse5Electro2302023-01-15
Show the hint

Topics to use: WHERE (filters), ORDER BY, LIMIT.

Query structure:

SELECT , 
FROM 
WHERE  > 
ORDER BY  DESC
Show the solution
SELECT title, duration_s
FROM songs
WHERE duration_s > 240
ORDER BY duration_s DESC;

Expected result (4 rows, in this order):

titleduration_s
Wires301
Night Drive276
Rust256
Palm Wine245

Exercise 10 · level 2

Show the city, day and maximum temperature of the readings on which it rained (rain_mm > 0).

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: WHERE (filters).

Query structure:

SELECT , , 
FROM 
WHERE  > 
Show the solution
SELECT city, day, temp_max
FROM readings
WHERE rain_mm > 0;

Expected result (4 rows):

citydaytemp_max
Paris2025-07-0322
Paris2025-07-0419
Lyon2025-07-0424
Lyon2025-07-0522

Exercise 11 · level 2

Show the id, arrival date and departure date of the bookings whose arrival (check_in) is before 2025-07-10.

Table bookings (11 rows)
idroom_idguest_idcheck_incheck_out
1212025-07-012025-07-04
2522025-07-022025-07-05
3832025-07-032025-07-06
4342025-07-052025-07-12
5652025-07-062025-07-08
6122025-07-082025-07-10
7932025-07-102025-07-11
8752025-07-112025-07-15
9412025-07-142025-07-16
10642025-07-152025-07-19
11652025-07-162025-07-18
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT , , 
FROM 
WHERE  < 
Show the solution
SELECT id, check_in, check_out
FROM bookings
WHERE check_in < '2025-07-10';

Expected result (6 rows):

idcheck_incheck_out
12025-07-012025-07-04
22025-07-022025-07-05
32025-07-032025-07-06
42025-07-052025-07-12
52025-07-062025-07-08
62025-07-082025-07-10

Exercise 12 · level 2

Show the id and date of the orders placed in February 2025.

Table orders (8 rows)
idcustomer_idorder_datestatus
112025-01-05shipped
222025-01-12shipped
312025-02-03paid
432025-02-10cancelled
542025-02-20shipped
652025-03-02shipped
722025-03-15paid
832025-03-28shipped
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT , 
FROM 
WHERE  BETWEEN  AND 
Show the solution
SELECT id, order_date
FROM orders
WHERE order_date BETWEEN '2025-02-01' AND '2025-02-28';

Expected result (3 rows):

idorder_date
32025-02-03
42025-02-10
52025-02-20

Exercise 13 · level 2

Show the name and hourly rate of the developers in the Data or Ops teams, from the highest rate to the lowest.

Table devs (6 rows)
idnameteamrate
1AnaWeb55
2BoWeb48
3CleoData62
4DanData58
5EveOps50
6FinnOps45
Show the hint

Topics to use: WHERE (filters), ORDER BY, LIMIT.

Query structure:

SELECT , 
FROM 
WHERE  IN (, )
ORDER BY  DESC
Show the solution
SELECT name, rate
FROM devs
WHERE team IN ('Data', 'Ops')
ORDER BY rate DESC;

Expected result (4 rows, in this order):

namerate
Cleo62
Dan58
Eve50
Finn45

Exercise 14 · level 2

Show the id, owner and opening date of the accounts opened before 2022.

Table accounts (6 rows)
idownercityopenedkind
1AliceParis2021-03-01current
2AliceParis2022-06-15savings
3BrunoLyon2020-09-10current
4ChloeLyon2023-01-20current
5DavidNice2019-11-05savings
6EmmaNice2024-04-01current
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT , , 
FROM 
WHERE  < 
Show the solution
SELECT id, owner, opened
FROM accounts
WHERE opened < '2022-01-01';

Expected result (3 rows):

idowneropened
1Alice2021-03-01
3Bruno2020-09-10
5David2019-11-05

Exercise 15 · level 4

Show the id of the invoices that were paid after their due date.

Table invoices (6 rows)
idclientissueddueamountpaid_on
1Acme2025-01-152025-02-1412002025-02-10
2Acme2025-03-012025-03-16800NULL
3Bolt2025-01-202025-03-064502025-03-01
4Bolt2025-02-252025-03-279502025-04-02
5Cyan2025-03-052025-05-04300NULL
6Cyan2024-12-102024-12-306002025-01-05
Show the hint

Topics to use: WHERE (filters).

Query structure:

SELECT 
FROM 
WHERE  > 
Show the solution
SELECT id
FROM invoices
WHERE paid_on > due;

Expected result (2 rows):

id
4
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.

Practice on the 31 “WHERE (filters)” questions