SQL EXISTS and NOT EXISTS: check whether matching rows exist
Sometimes you don't need columns from a second table, only the answer to "is there a match?": customers with at least one delivery, employees who never handled an order. That is what EXISTS and NOT EXISTS are for, and along the way you'll meet the best-known subquery trap: NOT IN with NULL.
In short: WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id) keeps every customer who has at least one order. NOT EXISTS keeps the ones who have none. The subquery refers to the outer row through o.customer_id = c.customer_id, which makes it a correlated subquery.
Before you start: subqueries and JOIN. For the NOT IN trap, the lesson on NULL helps.
Try it now
Orders live in their own table. For each customer you ask: is there a delivered order from them in there?
Task Sales is planning a thank-you email to every customer who has already received at least one delivery. Show the name and city of every customer with at least one order whose status is 'delivered'. Use EXISTS.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
How EXISTS works
EXISTS goes in WHERE, followed by a subquery in parentheses. The condition is true as soon as the subquery returns at least one row and false when it returns none. What the subquery selects makes no difference, which is why people usually write SELECT 1.
Here's an example. Customer service wants every customer who still has an open order.
SELECT c.name, c.city
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
AND o.status = 'open'
);
| name | city |
|---|---|
| Raj Patel | Leeds |
| Northline Printing Co | Toronto |
| Daniel Kim | Seattle |
Read it one customer at a time. For Raj Patel, customer_id 4, the subquery looks for open orders with customer_id 4, finds order 1017, so EXISTS is true and he stays. For Harbor Fremont Partners it finds no open order, so they drop out.
The key line is o.customer_id = c.customer_id. From inside the subquery it reaches out to the table of the outer query. A subquery like that is called correlated. Without the link, the subquery asks the same thing for every customer: is there an open order anywhere? The answer is always yes, and every customer ends up on the list.
NOT EXISTS and the NOT IN trap
NOT EXISTS flips the question: the row stays when the subquery finds nothing. Say you want everyone on the team who has nobody reporting to them. NOT IN looks like the natural choice:
SELECT first_name, last_name
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees);
The result is empty, and there's no error. Here's why: Karen Mitchell runs the company, so her manager_id is NULL. That puts a NULL in the list NOT IN checks against. For Julia Reyes the question becomes: is 3 different from 1, from 2, from 5, from 7 and from NULL? Comparing with NULL is unknown, so the whole condition is unknown, and WHERE drops the row. The same happens to everyone. NOT EXISTS doesn't have this problem:
SELECT e.first_name, e.last_name
FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM employees r
WHERE r.manager_id = e.employee_id
);
| first_name | last_name |
|---|---|
| Julia | Reyes |
| Marcus | Allen |
| Felix | Turner |
| David | Ross |
| Laura | Hughes |
| Paul | Ward |
Karen Mitchell's NULL is harmless here: r.manager_id = e.employee_id is never true for her row, so it simply doesn't count as a match. The takeaway: for "there is no such row", NOT EXISTS is the safe choice. If you'd rather keep NOT IN, filter the NULLs out inside the subquery with WHERE manager_id IS NOT NULL.
One more exercise: walk into the NOT IN trap yourself
Open orders haven't been assigned to anyone yet, so their employee_id is NULL. Go ahead and try NOT IN first, then run it and look at the result.
Task The team lead wants to know who has never handled an order. Show the first_name, last_name and department of every employee whose employee_id does not appear in any order. Use NOT EXISTS.
employees 10 rows, 7 columns
| employee_id | first_name | last_name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|---|
| 1 | Karen | Mitchell | Management | 93600 | 2018-01-02 | NULL |
| 2 | Tom | Bradley | Sales | 62400 | 2019-04-01 | 1 |
| 3 | Julia | Reyes | Sales | 46800 | 2021-06-15 | 2 |
| 4 | Marcus | Allen | Sales | 49200 | 2020-09-01 | 2 |
| 5 | Sarah | Long | Warehouse | 39600 | 2022-02-01 | 1 |
| 6 | Felix | Turner | Warehouse | 35400 | 2024-03-18 | 5 |
| 7 | Nina | Cooper | Accounting | 52800 | 2019-10-01 | 1 |
| 8 | David | Ross | Customer service | 38400 | 2023-05-02 | 2 |
| 9 | Laura | Hughes | Customer service | NULL | 2025-09-01 | 2 |
| 10 | Paul | Ward | Accounting | 43200 | 2022-11-14 | 7 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
EXISTS, IN or JOIN?
Often all three work. WHERE customer_id IN (SELECT customer_id FROM orders WHERE status = 'open') returns the same customers as the example above. The differences show up with duplicates, NULL and the columns you need.
| EXISTS | IN | JOIN | |
|---|---|---|---|
| Question | Is there a matching row? | Is the value in this list? | Combine the rows of both tables |
| Duplicates | no, each row at most once | no | yes, one row per match |
| Columns from the second table | not in the result | not in the result | yes |
| Negation | NOT EXISTS, safe with NULL | NOT IN, empty if the list holds NULL | LEFT JOIN with IS NULL |
Rule of thumb: if you need columns from the second table, use JOIN. If you only want to filter, use EXISTS. IN works well for short fixed lists and for subqueries whose column can't contain NULL.
In the next exercise a JOIN would return a customer once per matching order line. EXISTS doesn't.
Task The furniture team is inviting customers to a showroom event. Show the name and country of every customer who has ever ordered a product in the 'Furniture' category, whatever the order status. Each customer should appear only once.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Common mistakes
The subquery isn't tied to the outer row
Without o.customer_id = c.customer_id, the subquery finds the same rows for every customer, and EXISTS is always true. The exercise then reports too many rows, for example "Your query returns 12 rows. Wanted: 9 rows." Comparing o.customer_id = o.customer_id by mistake has the same effect.
NOT IN with NULL in the subquery
If the subquery's column contains even one NULL, NOT IN returns no rows. The exercise says "Your query returns no rows". Switch to NOT EXISTS, or remove the NULLs with IS NOT NULL.
JOIN instead of EXISTS gives duplicates
A JOIN produces one result row for every matching row in the second table. A customer with two matching orders shows up twice. The exercise reports "Your query returns 5 rows. Wanted: 3 rows." EXISTS returns each customer at most once.
EXISTS after a column name
WHERE customer_id EXISTS (…) isn't valid. EXISTS stands on its own in front of the parentheses, and the link to the column goes inside the subquery. Otherwise the exercise reports a syntax error near EXISTS.
EXISTS in MySQL, PostgreSQL, SQL Server and SQLite
EXISTS and NOT EXISTS behave the same in all four databases, correlated or not. The differences are in the details the documentation spells out:
| Database | Columns in the subquery | NOT IN with NULL |
|---|---|---|
| MySQL | ignored, according to the manual; SELECT *, SELECT 1 and SELECT 5 are all the same | IN returns NULL when no match is found and the list contains NULL; NOT IN is defined as NOT (… IN …) |
| PostgreSQL | normally unimportant, the usual convention is EXISTS (SELECT 1 …); the subquery generally runs only until one row is found | the result is NULL, not true, when no value is equal and at least one is NULL |
| SQL Server | irrelevant, even EXISTS (SELECT NULL) is true as long as there is a row | comparisons with NULL return UNKNOWN, and the docs warn about unexpected results |
| SQLite (these exercises) | the number of columns and their values have no effect; EXISTS returns 1 or 0 | NULL when no match is found and the right-hand side contains NULL |
Sources: MySQL Reference Manual (Subqueries with EXISTS or NOT EXISTS, Comparison Functions and Operators), PostgreSQL documentation (Subquery Expressions), Microsoft Learn (EXISTS, IN), sqlite.org (SQL Language Expressions).
FAQ
Why SELECT 1 and not SELECT *?
Both are correct and give the same result, because EXISTS only cares whether rows come back. SELECT 1 tells the reader right away that no column is needed.
Is EXISTS faster than IN or JOIN?
It depends on the database and the execution plan; there's no general rule. Pick the form that matches your question. When speed matters, compare the plans in your own database.
Can I do NOT EXISTS with a LEFT JOIN instead?
Yes. LEFT JOIN orders o ON … WHERE o.order_id IS NULL also finds rows without a partner and is just as safe with NULL. Many people find NOT EXISTS easier to read because it states the question directly.