← Back to SQL, page 4

sql-0063 · Medium mid

How do you list customers who have placed at least one order, without duplicates? Compare EXISTS with JOIN and DISTINCT.

EXISTS expresses a semi-join: return each customer once if at least one matching order exists. A plain JOIN returns one row per order, so a customer with two orders appears twice, and DISTINCT is then needed to undo the duplication. EXISTS states the intent directly and lets the engine stop at the first match.

Full answer

"Customers who have ordered" is a yes-or-no question per customer. A join answers a different question, "customer and order pairs":

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
SELECT c.name
FROM customers c
JOIN orders o ON o.customer_id = c.id
ORDER BY c.name;
name
Ana
Ana
Bilal
Bilal
Chloe

Ana and Bilal appear twice because they have two orders each. Adding DISTINCT hides that:

sql
SELECT DISTINCT c.name
FROM customers c
JOIN orders o ON o.customer_id = c.id
ORDER BY c.name;
name
Ana
Bilal
Chloe

The result is now right, but the database built all pairs and then removed duplicates. EXISTS asks the yes-or-no question directly:

sql
SELECT c.name
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ORDER BY c.name;
name
Ana
Bilal
Chloe

This is called a semi-join: rows from the left side are returned at most once, and no columns from the right side are needed. Many planners turn EXISTS, IN (subquery) and sometimes JOIN + DISTINCT into the same semi-join plan, so measure before claiming one is faster. The readability argument stands on its own: EXISTS says what you mean, and it cannot multiply rows.

IN (SELECT customer_id FROM orders) is another correct form here. Unlike NOT IN, plain IN is safe even with the guest order's NULL customer_id.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

SELECT c.name
FROM customers c
JOIN orders o ON o.customer_id = c.id
ORDER BY c.name;

SELECT DISTINCT c.name
FROM customers c
JOIN orders o ON o.customer_id = c.id
ORDER BY c.name;

SELECT c.name
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
ORDER BY c.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • JOIN returns one row per match, so parents repeat
  • DISTINCT after a join removes the repeats but hides intent
  • EXISTS expresses a semi-join and never duplicates left rows
  • Planners often produce the same plan; verify before claiming speed

Common mistakes

  • Joining to a child table to filter parents and then summing parent columns
  • Claiming EXISTS is always faster without looking at the plan

Follow-ups

  • How do you list customers with no orders?
  • What happens to a SUM of customer credit limits if you filter with a JOIN instead of EXISTS?

Tips

  • Use the words semi-join and anti-join; interviewers listen for them