Skip to main content
SQL • LESSON 97

NOT IN Subquery

How can we find employees outside a set of selected departments?

Advanced3 Minutes1100 XP
🤔 THE QUESTION

How can we find employees outside a set of selected departments?

💡 WHAT IS IT?

NOT IN subqueries exclude records whose foreign key appears in the subquery's result set.

🎯 WHAT IS IT USED FOR?

Excluding specific business units, filtering out blacklisted accounts, non-member lookups.

💻 EXAMPLE
SELECT name
FROM employees
WHERE department_id NOT IN (
    SELECT department_id
    FROM departments
    WHERE department_name IN ('HR', 'Finance')
);

🎯 Mission Objectives

Practice typing production-grade SQL code for NOT IN Subquery.

  • NOT IN subquery
  • Exclusion lists
  • Department filtering
  • Multi-level subqueries