🧬 SQL Operators & Conditions
Estimated reading: 3 minutes 35 views

🎯 SQL IN Operator – Use IN and NOT IN for Multi-Value Filters

🧲 Introduction – What is SQL IN?

The IN operator in SQL is used to match a column’s value against a list of values. It makes multiple OR conditions concise, readable, and efficient.

🎯 In this guide, you’ll learn:

  • Basic syntax of IN and NOT IN
  • Use with numbers, strings, and subqueries
  • Best practices and common pitfalls

βœ… 1. Basic SQL IN Syntax

SELECT * FROM employees
WHERE department IN ('HR', 'IT', 'Finance');

βœ… Returns rows where department is one of the listed values.


πŸ” 2. Using NOT IN

SELECT * FROM products
WHERE category NOT IN ('Discontinued', 'Obsolete');

βœ… Excludes rows matching any value in the list.


🧠 3. IN vs Multiple ORs

-- Using OR
WHERE department = 'HR' OR department = 'IT' OR department = 'Finance';

-- Using IN
WHERE department IN ('HR', 'IT', 'Finance');

βœ… IN is cleaner and performs better on large sets.


πŸ”Ž 4. IN with Numeric Values

SELECT * FROM orders
WHERE status_code IN (200, 201, 404);

βœ… Works with integers and decimals too.


πŸ” 5. IN with Subqueries

SELECT * FROM employees
WHERE department_id IN (SELECT id FROM departments WHERE active = true);

βœ… Dynamically filters based on another table’s values.


⚠️ 6. Caution with NULLs in NOT IN

-- This may return 0 rows if the list contains NULL
WHERE status NOT IN ('active', NULL);

βœ… Always filter NULLs out in subqueries when using NOT IN.


πŸ“˜ Best Practices

βœ… Recommended❌ Avoid This
Use IN for multi-value filtersWriting long OR conditions
Filter out NULLs when using NOT INIgnoring NULLs in subquery lists
Use subqueries for dynamic filteringHardcoding long lists unnecessarily

πŸ“Œ Summary – Recap & Next Steps

The IN operator allows for cleaner, more maintainable filtering when working with multiple values. It’s ideal for status checks, lookups, and relationship-based queries.

πŸ” Key Takeaways:

  • Use IN for simple multi-value matches
  • Use NOT IN to exclude specific values
  • Subqueries let you create dynamic IN lists
  • Avoid NULLs with NOT IN for safe logic

βš™οΈ Real-World Relevance:
Used in filters, dropdown selections, security rules, and conditional logic for audit trails.

➑️ Next: Explore BETWEEN for filtering within numeric and date ranges.


❓ FAQ – SQL IN Operator

❓ What does SQL IN do?

βœ… Filters rows where a column matches one of the values in a list.

❓ Can I use IN with numbers and strings?

βœ… Yes. The values inside the list must match the column’s data type.

❓ Is IN better than multiple OR conditions?

βœ… It’s shorter, easier to read, and often performs better.

❓ Can I use IN with a subquery?

βœ… Yes, and it’s highly useful for dynamic conditions.

❓ What should I watch out for with NOT IN?

βœ… NULLs in the list may cause unexpected behavior. Use WHERE ... IS NOT NULL to clean subquery results.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🎯 SQL IN

Or Copy Link

CONTENTS
Scroll to Top