Skip to main content
SQL • LESSON 32

EXISTS & NOT EXISTS

How can we find records based on whether related transactions exist?

Advanced3 Minutes510 XP
🤔 THE QUESTION

How can we find records based on whether related transactions exist?

💡 WHAT IS IT?

EXISTS evaluates to true if the subquery returns at least one matching row.

🎯 WHAT IS IT USED FOR?

Finding customers with purchases, auditing orphan records with NOT EXISTS.

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

🎯 Mission Objectives

Practice typing production-grade SQL code for EXISTS & NOT EXISTS.

  • EXISTS
  • Correlated subquery
  • Customer analytics
  • Relational checks