Module 5 · Advanced queries

Find matches with EXISTS

By the end you will be able to search if there is a row related to each element.

About 14 min

0 of 52 lessons

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.

Minimal syntax
WHERE EXISTS (
  SELECT 1 FROM orders AS p
  WHERE p.customer_id = c.id
)

See it in action

Which customers have any orders?

SQL
SELECT c.name
FROM customers AS c
WHERE EXISTS (
  SELECT 1
  FROM orders AS p
  WHERE p.customer_id = c.id
);
Example result: 8 of 10 rows
name
Amy Wilson
Liam Martin
Megan Reed
David Clark
Sara Green
Laura Ward
Hugh Miller
Nia Smith
  1. The outer query goes through customers. For each one, the inner query looks for orders with that customer's id.
  2. One match is enough to keep the customer. Three orders do not produce three copies of their name.
  3. If you forget p.customer_id = c.id, you are asking whether anybody has orders. That can keep customers who have none.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Find customers with no orders

Not started

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.

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.

Next: Break queries into steps with CTEs