Skip to main content
SQL โ€ข LESSON 15

SQL EXISTS

How can we efficiently test if related records exist in another table?

Advancedโฑ 3 Minutesโญ 250 XP
๐Ÿค” THE QUESTION

How can we efficiently test if related records exist in another table?

๐Ÿ’ก WHAT IS IT?

EXISTS tests for the existence of rows in a subquery and short-circuits upon finding the first match.

๐ŸŽฏ WHAT IS IT USED FOR?

High-performance relational checks, finding active subscribers, verifying foreign key presence.

๐Ÿ’ป EXAMPLE
SELECT *
FROM employees e
WHERE EXISTS (
  SELECT 1
  FROM departments d
  WHERE d.department = e.department
);

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for SQL EXISTS.

  • โœ“ EXISTS
  • โœ“ Correlated condition
  • โœ“ SELECT 1
  • โœ“ Table Aliases