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.
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.