SQL • LESSON 16

Correlated Subqueries

Learn how correlated subqueries compare each row against related data.

🔴 Advanced⏱ 3 Minutes⭐ 280 XP
🤔 THE QUESTION

How can we check whether a matching record exists without returning the matching rows?

💡 WHAT IS IT?

EXISTS checks whether a subquery returns at least one row.

🎯 WHAT IS IT USED FOR?

It is useful when the existence of related data matters more than the actual values returned by that related data.

💻 EXAMPLE
SELECT name
FROM employees e
WHERE EXISTS (
  SELECT 1
  FROM departments d
  WHERE d.department = e.department
);

Mission Brief

Practice using a correlated subquery to compare each employee's salary with the average salary of their department.

Objectives

  • SELECT
  • FROM
  • Correlated Subquery
  • AVG
  • GROUP relationship