Skip to main content
SQL โ€ข LESSON 32

EXISTS & NOT EXISTS

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

Advancedโฑ 3 Minutesโญ 510 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