SQL • LESSON 15

EXISTS

Learn how to use EXISTS to check whether related records exist.

🔴 Advanced⏱ 2 Minutes⭐ 250 XP
🤔 THE QUESTION

How can we find employees who have a salary greater than every employee in another group?

💡 WHAT IS IT?

The ALL operator compares a value against every value returned by a subquery.

🎯 WHAT IS IT USED FOR?

It is useful when a query needs to ensure that a value is greater than, less than, or otherwise satisfies a condition against an entire result set.

💻 EXAMPLE
SELECT name,
salary
FROM employees
WHERE salary > ALL (
  SELECT salary
  FROM employees
  WHERE department = 'HR'
);

Mission Brief

Practice using EXISTS to find employees whose department exists in the departments table.

Objectives

  • SELECT
  • FROM
  • EXISTS
  • Subquery
  • Table aliases