Beginner

721. IN with Subquery

Learn how to use the IN operator together with a Subquery to filter records dynamically.

📘 Lesson 720 of 728 ⏱ 8 min 🎯 Intermediate

The IN operator compares a value against multiple values returned by a subquery. Instead of manually specifying values, the subquery provides the list automatically.

Business data changes continuously. Using a subquery with IN allows SQL to retrieve dynamic results without modifying the query every time new data appears.

Retrieve employees who belong to departments located in Mumbai.

SQL
SELECT
    EmployeeName
FROM Employees
WHERE DepartmentID IN
(
    SELECT DepartmentID
    FROM Departments
    WHERE City = 'Mumbai'
);

Used in customer segmentation, HR reporting, sales analysis, financial reporting, inventory management, and compliance reporting.

  • Uses the IN operator with a Subquery.
  • Compares against multiple values.
  • Returns dynamic results.
  • Improves query flexibility.
  • Commonly used for filtering.
  • Supports nested queries.
  • Useful in reporting applications.
Can the IN operator use a Subquery?

Yes. The IN operator can compare values returned by a Subquery.

How many columns should the Subquery return with IN?

The Subquery should return a single column.

Have you finished this lesson?

Introduction to SQL – All Lessons