SQL • LESSON 14

IN with Subqueries

Learn how to use IN with a subquery to filter related records.

🟡 Intermediate⏱ 2 Minutes⭐ 240 XP
🤔 THE QUESTION

How can we find employees who belong to departments that have more than 5 employees?

💡 WHAT IS IT?

A subquery can return a set of values that the outer query can use for filtering.

🎯 WHAT IS IT USED FOR?

This is useful when the filtering condition depends on a group of records rather than one single value.

💻 EXAMPLE
SELECT name,
department
FROM employees
WHERE department IN (
  SELECT department
  FROM employees
  GROUP BY department
  HAVING COUNT(*) > 5
);

Mission Brief

Practice using IN with a subquery to find employees belonging to departments in a specific location.

Objectives

  • SELECT
  • FROM
  • WHERE
  • IN
  • Subquery