Before you start, you need
Answer questions with subqueries · Match rows with INNER JOIN
Look for a match for each outer row
To find out which customers have orders, you do not need to add up or show all their orders. It is enough to ask, for each customer: can I find at least one that belongs to them? EXISTS answers that question.
The outer query reads customers. The inner one looks for orders with p.customer_id = c.id. c.id is the customer we are checking at that moment: for Amy we look for Amy's orders, not anybody's.
Using a value from the outer query inside the inner one is called correlation. That condition ties the two questions together. If you forget it, you will only check whether the shop has any order at all, even one that does not belong to the customer.
SELECT 1 shows that we do not need any order data: what matters is finding a row. A customer appears only once, even if they have several orders. NOT EXISTS looks for the opposite case: that there is no match at all. To inspect the inner part, temporarily replace c.id with a known id.
WHERE EXISTS (
SELECT 1 FROM orders AS p
WHERE p.customer_id = c.id
)See it in action
Which customers have any orders?
SELECT c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS p
WHERE p.customer_id = c.id
);| name |
|---|
| Amy Wilson |
| Liam Martin |
| Megan Reed |
| David Clark |
| Sara Green |
| Laura Ward |
| Hugh Miller |
| Nia Smith |
- The outer query goes through customers. For each one, the inner query looks for orders with that customer's id.
- One match is enough to keep the customer. Three orders do not produce three copies of their name.
- If you forget p.customer_id = c.id, you are asking whether anybody has orders. That can keep customers who have none.
- NOT EXISTS reverses the decision and finds the customers with no orders. You check it in the standalone variant.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Check whether something exists
Show the name of customers who have at least one shipped order. Use EXISTS with a complete query that links p.customer_id to c.id.
Expected result: 7 rows with the column name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The question is about existence, not about counting rows.
The inner query must refer to the outer customer's id.
Inside EXISTS, link p.customer_id = c.id and filter for shipped orders.
Solution explained
SELECT c.name FROM customers AS c WHERE EXISTS (SELECT 1 FROM orders AS p WHERE p.customer_id = c.id AND p.status = 'shipped');The inner query depends on the outer customer; a single match is enough to keep them.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Find customers with no orders
Show the name of customers who have no orders at all. Use NOT EXISTS and link the order to the outer customer.
Expected result: 2 rows with the column name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Absence is decided for each customer.
Negate a correlated search for orders.
Link p.customer_id to c.id inside NOT EXISTS.
Solution explained
SELECT c.name FROM customers AS c WHERE NOT EXISTS (SELECT 1 FROM orders AS p WHERE p.customer_id = c.id);A customer appears when no order with their id exists. Other customers' orders do not change that decision.
Key takeaways
- EXISTS checks whether at least one row is found.
- The correlation links to the outer row.
- NOT EXISTS looks for the absence of matches.