SQL • LESSON 32

EXISTS & NOT EXISTS

Learn how to find records based on whether related records exist.

🔴 Advanced⏱ 3 Minutes⭐ 510 XP
🤔 THE QUESTION

How can we find customers who have placed at least one order?

💡 WHAT IS IT?

EXISTS checks whether a subquery returns at least one matching row, while NOT EXISTS checks that no matching row exists.

🎯 WHAT IS IT USED FOR?

They are useful when you care about whether related data exists rather than needing to return the related rows themselves.

💻 EXAMPLE
SELECT c.customer_name
FROM customers c
WHERE EXISTS (
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.customer_id
);

Mission Brief

Practice using EXISTS to find customers who have at least one matching order.

Objectives

  • SELECT
  • EXISTS
  • Subquery
  • Related records
  • Filtering