SQL exercises with solutions: EXISTS and NOT EXISTS

SQL exercises with solutions: EXISTS and NOT EXISTS

Updated on

EXISTS is true when the subquery returns at least one row; NOT EXISTS, when it returns none. These 15 exercises range from level 5 to level 8; they use the syntax of SQLite, SpeedQL’s SQL engine.

Tip: NOT EXISTS is safer than NOT IN when the subquery may contain NULLs.

Read the “EXISTS” card in the cheat sheet

Exercise 1 · level 5

Show the name of the employees who have at least one direct report. Use EXISTS.

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 hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  = )
Show the solution
SELECT name
FROM staff m
WHERE EXISTS (SELECT 1 FROM staff e WHERE e.manager_id = m.id);

Expected result (5 rows):

name
Alice
Bob
Claire
David
Emma

Exercise 2 · level 5

Show the name of the employees who have no direct reports. Use NOT EXISTS.

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 hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  = )
Show the solution
SELECT name
FROM staff m
WHERE NOT EXISTS (SELECT 1 FROM staff e WHERE e.manager_id = m.id);

Expected result (5 rows):

name
Farid
Gaelle
Hugo
Iris
Jules

Exercise 3 · level 5

Show the name of the customers who placed at least one order above 100. Use EXISTS.

Table customers (4 rows)
idnamecity
1AliceParis
2BrunoLyon
3ChloeParis
4DylanNantes
Table orders (6 rows)
idcustomer_idamount
11120
2180
32200
4350
5375
6330
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  =  AND  > )
Show the solution
SELECT name
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.amount > 100);

Expected result (2 rows):

name
Alice
Bruno

Exercise 4 · level 5

Show the name of the actors who have not appeared in any movie. Use NOT EXISTS.

Table actors (7 rows)
idnamebirth_year
1Ava Stone1985
2Ben Cole1978
3Chloe Park1990
4Dario Vega1982
5Emi Sato1995
6Felix Grant1970
7Gus Hale1988
Table casting (14 rows)
movie_idactor_idrole
11lead
12support
23lead
26support
34lead
45lead
41support
52lead
55support
75lead
76support
86lead
93lead
94support
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  = )
Show the solution
SELECT name
FROM actors a
WHERE NOT EXISTS (SELECT 1 FROM casting c WHERE c.actor_id = a.id);

Expected result (1 row):

name
Gus Hale

Exercise 5 · level 5

Show the title of the books that have never been borrowed. Use NOT EXISTS.

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
Table loans (12 rows)
idbook_idmember_idloan_datereturn_date
1112025-01-052025-01-19
2322025-01-102025-02-02
3512025-02-012025-02-10
4732025-02-03NULL
5342025-02-152025-03-01
6222025-03-022025-03-30
7852025-03-05NULL
8132025-03-102025-03-18
9612025-03-202025-04-15
10352025-04-01NULL
11942025-04-052025-04-12
121022025-04-082025-04-20
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  = )
Show the solution
SELECT title
FROM books b
WHERE NOT EXISTS (SELECT 1 FROM loans l WHERE l.book_id = b.id);

Expected result (1 row):

title
Winter Ledger

Exercise 6 · level 5

Show the name of the teams that played at least one home match that ended in a draw. Use EXISTS.

Table teams (5 rows)
idnamecityfounded
1Red FoxesLyon1950
2Blue OwlsParis1962
3Green BullsLille1971
4Gold HawksNantes1988
5Grey WolvesParis1990
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), EXISTS.

Query structure:

SELECT 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  =  AND  = )
Show the solution
SELECT name
FROM teams t
WHERE EXISTS (SELECT 1 FROM matches m WHERE m.home_id = t.id AND m.home_goals = m.away_goals);

Expected result (3 rows):

name
Blue Owls
Green Bulls
Gold Hawks

Exercise 7 · level 5

Show the code and city of the airports A from which at least one flight goes to an airport B while another flight connects B back to A. Use EXISTS.

Table airports (7 rows)
codecitycountry
CDGParisFrance
LYSLyonFrance
MADMadridSpain
LISLisbonPortugal
FCORomeItaly
BERBerlinGermany
NCENiceFrance
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), EXISTS.

Query structure:

SELECT , 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  =  AND EXISTS (SELECT  FROM   WHERE  =  AND  = ))
Show the solution
SELECT code, city
FROM airports a
WHERE EXISTS (SELECT 1 FROM flights f WHERE f.origin = a.code AND EXISTS (SELECT 1 FROM flights g WHERE g.origin = f.dest AND g.dest = f.origin));

Expected result (6 rows):

codecity
CDGParis
LYSLyon
MADMadrid
LISLisbon
FCORome
BERBerlin

Exercise 8 · level 5

Using NOT EXISTS, show the id, type and price of the rooms that have never been booked.

Table rooms (10 rows)
idhotel_idtypeprice
11single70
21double95
32double130
42suite210
53single110
63double150
74double65
85double260
95suite480
105single190
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), EXISTS.

Query structure:

SELECT , , 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  = )
Show the solution
SELECT r.id, r.type, r.price
FROM rooms r
WHERE NOT EXISTS (SELECT 1 FROM bookings b WHERE b.room_id = r.id);

Expected result (1 row):

idtypeprice
10single190

Exercise 9 · level 5

Using EXISTS, show the name and city of the customers who have at least one cancelled order ('cancelled').

Table customers (6 rows)
idnamecitysignup
1AlbaParis2024-01-10
2BorisLyon2024-02-15
3CarlaParis2024-03-01
4DenisNantes2024-05-20
5EvaLyon2024-06-30
6FabioLille2024-08-08
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), EXISTS.

Query structure:

SELECT , 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  =  AND  = )
Show the solution
SELECT c.name, c.city
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.status = 'cancelled');

Expected result (1 row):

namecity
CarlaParis

Exercise 10 · level 5

Using NOT EXISTS, show the name and team of the developers who have not finished any task.

Table devs (6 rows)
idnameteamrate
1AnaWeb55
2BoWeb48
3CleoData62
4DanData58
5EveOps50
6FinnOps45
Table tasks (10 rows)
idproject_iddev_idtitlehoursstatusdone_on
111Landing page12done2025-03-10
213Data model20done2025-03-20
312Login8doingNULL
424ETL job16done2025-04-02
525CI pipeline6done2025-03-15
631Dashboard14todoNULL
733Report10done2025-04-25
842API18doingNULL
945Monitoring9todoNULL
1044Forecast22done2025-05-05
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT , 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  =  AND  = )
Show the solution
SELECT d.name, d.team
FROM devs d
WHERE NOT EXISTS (SELECT 1 FROM tasks t WHERE t.dev_id = d.id AND t.status = 'done');

Expected result (2 rows):

nameteam
BoWeb
FinnOps

Exercise 11 · level 5

Using NOT EXISTS, show the id, owner and kind of the accounts that have never paid rent (label 'rent').

Table accounts (6 rows)
idownercityopenedkind
1AliceParis2021-03-01current
2AliceParis2022-06-15savings
3BrunoLyon2020-09-10current
4ChloeLyon2023-01-20current
5DavidNice2019-11-05savings
6EmmaNice2024-04-01current
Table transactions (14 rows)
idaccount_idmade_onamountlabel
112025-01-022500salary
212025-01-05-60groceries
312025-01-12-800rent
422025-01-15500transfer
532025-01-031900salary
632025-01-20-120groceries
732025-02-01-950rent
842025-01-252100salary
942025-02-03-45restaurant
1012025-02-022600salary
1112025-02-06-75groceries
1252025-02-1030interest
1322025-02-15500transfer
1442025-02-18-600rent
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT , , 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  =  AND  = )
Show the solution
SELECT a.id, a.owner, a.kind
FROM accounts a
WHERE NOT EXISTS (SELECT 1 FROM transactions t WHERE t.account_id = a.id AND t.label = 'rent');

Expected result (3 rows):

idownerkind
2Alicesavings
5Davidsavings
6Emmacurrent

Exercise 12 · level 8

Show the name of the authors all of whose books have been borrowed at least once (among the authors who have at least one book).

Table authors (6 rows)
idnamecountrybirth_year
1Maya LindSweden1968
2Omar HaddadMorocco1975
3Julia BrandtGermany1981
4Tom ReyesMexico1990
5Ines DupuisFrance1959
6Ravi MenonIndia1984
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
Table loans (12 rows)
idbook_idmember_idloan_datereturn_date
1112025-01-052025-01-19
2322025-01-102025-02-02
3512025-02-012025-02-10
4732025-02-03NULL
5342025-02-152025-03-01
6222025-03-022025-03-30
7852025-03-05NULL
8132025-03-102025-03-18
9612025-03-202025-04-15
10352025-04-01NULL
11942025-04-052025-04-12
121022025-04-082025-04-20
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  = ) AND NOT EXISTS (SELECT  FROM   WHERE  =  AND NOT EXISTS (SELECT  FROM   WHERE  = ))
Show the solution
SELECT name
FROM authors a
WHERE EXISTS (SELECT 1 FROM books b WHERE b.author_id = a.id) AND NOT EXISTS (SELECT 1 FROM books b WHERE b.author_id = a.id AND NOT EXISTS (SELECT 1 FROM loans l WHERE l.book_id = b.id));

Expected result (4 rows):

name
Omar Haddad
Julia Brandt
Tom Reyes
Ines Dupuis

Exercise 13 · level 8

Show the name of the hotels that have rooms and whose rooms have all been booked at least once.

Table hotels (5 rows)
idnamecitystars
1Seaside InnNice3
2Alpine LodgeAnnecy4
3City LoftParis4
4Old MillBordeaux2
5Grand PalaceParis5
Table rooms (10 rows)
idhotel_idtypeprice
11single70
21double95
32double130
42suite210
53single110
63double150
74double65
85double260
95suite480
105single190
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), EXISTS.

Query structure:

SELECT 
FROM  
WHERE EXISTS (SELECT  FROM   WHERE  = ) AND NOT EXISTS (SELECT  FROM   WHERE  =  AND NOT EXISTS (SELECT  FROM   WHERE  = ))
Show the solution
SELECT h.name
FROM hotels h
WHERE EXISTS (SELECT 1 FROM rooms r WHERE r.hotel_id = h.id) AND NOT EXISTS (SELECT 1 FROM rooms r WHERE r.hotel_id = h.id AND NOT EXISTS (SELECT 1 FROM bookings b WHERE b.room_id = r.id));

Expected result (4 rows):

name
Seaside Inn
Alpine Lodge
City Loft
Old Mill

Exercise 14 · level 8

Show the name of the customers who ordered every product of the Kitchen category (each at least once, cancelled orders included).

Table customers (6 rows)
idnamecitysignup
1AlbaParis2024-01-10
2BorisLyon2024-02-15
3CarlaParis2024-03-01
4DenisNantes2024-05-20
5EvaLyon2024-06-30
6FabioLille2024-08-08
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
Table order_items (14 rows)
order_idproduct_idqty
111
122
241
335
321
451
562
511
624
633
741
761
852
821
Table products (7 rows)
idnamecategoryprice
1Desk LampHome35
2Coffee MugKitchen12
3NotebookOffice6
4Office ChairOffice149
5KettleKitchen45
6CushionHome22
7StaplerOffice9
Show the hint

Topics to use: WHERE (filters), JOIN, EXISTS.

Query structure:

SELECT 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  =  AND NOT EXISTS (SELECT  FROM   JOIN   ON  =  WHERE  =  AND  = ))
Show the solution
SELECT c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM products p WHERE p.category = 'Kitchen' AND NOT EXISTS (SELECT 1 FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE o.customer_id = c.id AND oi.product_id = p.id));

Expected result (1 row):

name
Carla

Exercise 15 · level 8

Show the name of the developers who have at least one task on every project of the client Acme.

Table devs (6 rows)
idnameteamrate
1AnaWeb55
2BoWeb48
3CleoData62
4DanData58
5EveOps50
6FinnOps45
Table projects (4 rows)
idnameclientbudgetdeadline
1AtlasAcme200002025-06-30
2BeaconBolt120002025-05-15
3CometAcme80002025-04-30
4DeltaCyan150002025-07-31
Table tasks (10 rows)
idproject_iddev_idtitlehoursstatusdone_on
111Landing page12done2025-03-10
213Data model20done2025-03-20
312Login8doingNULL
424ETL job16done2025-04-02
525CI pipeline6done2025-03-15
631Dashboard14todoNULL
733Report10done2025-04-25
842API18doingNULL
945Monitoring9todoNULL
1044Forecast22done2025-05-05
Show the hint

Topics to use: WHERE (filters), EXISTS.

Query structure:

SELECT 
FROM  
WHERE NOT EXISTS (SELECT  FROM   WHERE  =  AND NOT EXISTS (SELECT  FROM   WHERE  =  AND  = ))
Show the solution
SELECT d.name
FROM devs d
WHERE NOT EXISTS (SELECT 1 FROM projects p WHERE p.client = 'Acme' AND NOT EXISTS (SELECT 1 FROM tasks t WHERE t.project_id = p.id AND t.dev_id = d.id));

Expected result (2 rows):

name
Ana
Cleo

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 22 “EXISTS” questions