Technology  /  SQL

๐Ÿ—„๏ธ SQL 40 guides ยท updated 2026

The language of data โ€” from SELECT and JOINs to window functions, query plans, and the performance tuning that separates juniors from seniors.


๐Ÿ” Mastering SQL IN and NOT IN: Simplify Filtering with Practical Examples

When writing SQL queries, one common task is to filter rows based on whether a column value matches a list of values. Instead of writing multiple OR conditions, SQL gives us a neat solution โ€” the IN and NOT IN operators.

In this guide, youโ€™ll learn how IN and NOT IN work, how to use them effectively, and why they can make your queries shorter, faster, and more readable.


๐Ÿš€ What is IN in SQL?

The IN operator is used to match a value against a set of values. If the value exists in the list, the condition evaluates to TRUE.

๐Ÿงพ Basic Syntax

SELECT column1, column2
FROM table_name
WHERE column_name IN (value1, value2, value3, ...);

It works like a shorthand for multiple OR conditions.

๐Ÿง  Simple Example

Imagine we have a students table with a grade column. You want to find students who are in grades 9, 10, or 11.

SELECT *
FROM students
WHERE grade IN (9, 10, 11);

This is equivalent to:

WHERE grade = 9 OR grade = 10 OR grade = 11

But much cleaner and easier to maintain!


๐Ÿ“Œ Use Case: IN with Strings

Youโ€™re not limited to numbers โ€” IN works with text values too.

SELECT *
FROM customers
WHERE city IN ('New York', 'Los Angeles', 'Chicago');

This will return customers living in any of the three cities.


โš™๏ธ Use IN with Subqueries

You can also use IN with a subquery to filter based on the results of another query.

๐Ÿงพ Example

SELECT name
FROM employees
WHERE department_id IN (
SELECT id
FROM departments
WHERE region = 'North'
);

This finds employees who belong to departments in the โ€œNorthโ€ region.


๐Ÿšซ What is NOT IN in SQL?

The NOT IN operator is the opposite of IN. It filters out rows where the value is NOT present in the given list.

๐Ÿงพ Syntax

SELECT column1, column2
FROM table_name
WHERE column_name NOT IN (value1, value2, value3, ...);

๐Ÿง  Example

Get a list of employees who do not work in the Sales or HR departments:

SELECT *
FROM employees
WHERE department NOT IN ('Sales', 'HR');

๐Ÿ“› Common Pitfall: NULL with NOT IN

When using NOT IN with a list that contains NULL, it can cause unexpected behavior.

โš ๏ธ Example

SELECT *
FROM employees
WHERE department_id NOT IN (1, 2, NULL);

This will return no rows โ€” because SQL doesnโ€™t know how to handle comparisons with NULL.

โœ… To avoid this, always make sure your subqueries or lists do not contain NULLs when using NOT IN.


๐Ÿ”„ IN vs. JOIN โ€“ Which One to Use?

Sometimes, IN can be replaced with a JOIN. Hereโ€™s a quick comparison:

Example with IN:

SELECT name
FROM products
WHERE category_id IN (SELECT id FROM categories WHERE active = 1);

Same logic with JOIN:

SELECT p.name
FROM products p
JOIN categories c ON p.category_id = c.id
WHERE c.active = 1;

๐Ÿ“‹ Real-World Examples of IN and NOT IN

๐ŸŽฏ Example 1: Find users from selected countries

SELECT *
FROM users
WHERE country IN ('India', 'UK', 'Australia');

๐ŸŽฏ Example 2: Exclude blacklisted emails

SELECT email
FROM newsletter
WHERE email NOT IN (
SELECT email
FROM blacklist
);

๐Ÿ’ก Performance Tip: IN vs. EXISTS

So in complex queries, consider:

WHERE EXISTS (SELECT 1 FROM ...)

Instead of:

WHERE id IN (SELECT id FROM ...)

Test both for your dataset to see which performs better.


โœ… Summary Table

OperatorPurposeReturns TRUE Whenโ€ฆ
INMatch value in a listThe column value is in the list
NOT INExclude value from a listThe column value is not in the list

๐Ÿ›  Pro Tips


๐Ÿ Conclusion

SQLโ€™s IN and NOT IN operators offer a powerful, concise way to filter rows based on a set of values. Theyโ€™re not just easier to write, but they also make your code cleaner and more maintainable.

Whether youโ€™re selecting rows from a known list or comparing with the results of a subquery, these operators are essential tools for anyone writing SQL.