CodeBlanks Logo Icon CodeBlanks

Multiple EXISTS

Combine several EXISTS with AND/OR.

Multiple EXISTS

You can combine multiple EXISTS clauses with AND and OR.

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

This query returns those with both orders and reviews.

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

Multiple EXISTS — details

Each EXISTS is a separate subquery. AND requires both, OR at least one.

For example, you find customers with both an order and a review using AND, and those with either one using OR.

Practice Multiple EXISTS

Find customers with both orders and reviews using two EXISTS and AND.

Starter code

query.sql
-- two conditions
SELECT name FROM customers c
WHERE

Sample solution

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

Expected output

name
Ayşe

Questions and answers

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

1 Add AND

Question
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
  ____ EXISTS (SELECT 1 FROM reviews r WHERE r.customer_id = c.id);
Answer
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id)
  AND EXISTS (SELECT 1 FROM reviews r WHERE r.customer_id = c.id);

Explanation: With two EXISTS conditions joined by AND, the customer must have both an order and a review.

2 Multiple EXISTS: match the terms

Term Meaning
AND both
OR at least one
EXISTS existence
NOT not

3 What does AND with two EXISTS require?

  • Only the first
  • Only one of them
  • None
  • Both of them Correct answer

Explanation: AND requires both EXISTS to be true.

All lessons in Intermediate