CodeBlanks Logo Icon CodeBlanks

Summary - EXISTS

Find customers with no orders using NOT EXISTS.

Summary - EXISTS

EXISTS checks whether a subquery returns rows. NOT EXISTS finds missing records.

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

Summary EXISTS query.

name is selected from customers c. WHERE keeps only the rows where NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = … holds.

Practice Summary - EXISTS

Summary: find customers without orders using NOT EXISTS.

Starter code

query.sql
-- summary exists
SELECT name FROM customers c
WHERE

Sample solution

query.sql
SELECT name FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);

Expected output

name
Fatma
Ali

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What finds customers without orders?

  • NOT EXISTS Correct answer
  • EXISTS
  • COUNT
  • SUM

Explanation: NOT EXISTS finds missing records.

2 Summary - EXISTS: match the terms

Term Meaning
EXISTS existence
NOT EXISTS missing
fast short-circuit
NULL unaffected

All lessons in Intermediate