Skip to main content
SQL • LESSON 15

SQL EXISTS

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

Advanced3 Minutes250 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