IN in SQL

📋 The IN operator in SQL is used to check whether a value matches any value in a specified list. It provides a simpler and more readable alternative to writing multiple OR conditions. The IN operator is commonly used with the WHERE clause to filter records based on multiple possible values.

📖 What is the IN Operator?

The IN operator compares a column value against a list of values. If the column matches any value in the list, the row is included in the result set. This makes queries shorter, easier to read, and easier to maintain.

Information

The IN operator can be used with a list of constant values or with the results returned by a subquery.

📝 Basic Syntax

General Syntax

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

📊 Sample Table

Consider the following Students table:

StudentIDNameDepartmentCityMarks
101AliceComputer ScienceChennai92
102BobMathematicsCoimbatore85
103CharliePhysicsMadurai78
104DavidComputer ScienceChennai95
105EvaMathematicsSalem88

đŸŽ¯ Using IN with Multiple Values

Retrieve students who belong to either the Computer Science or Physics department.

IN Example

SELECT *
FROM Students
WHERE Department IN (
    'Computer Science',
    'Physics'
);

This query returns all students whose department matches either value in the list.

âš–ī¸ IN vs Multiple OR Conditions

The IN operator is equivalent to multiple OR conditions but is shorter and easier to read.

Using OR

SELECT *
FROM Students
WHERE Department = 'Computer Science'
OR Department = 'Physics'
OR Department = 'Mathematics';

Using IN

SELECT *
FROM Students
WHERE Department IN (
    'Computer Science',
    'Physics',
    'Mathematics'
);

Tip

When checking several possible values for the same column, IN is usually more readable than multiple OR conditions.

🔗 IN with Numeric Values

The IN operator also works with numeric data.

Numeric IN Example

SELECT *
FROM Students
WHERE StudentID IN (101, 103, 105);

đŸšĢ Using NOT IN

The NOT IN operator retrieves rows whose values are not present in the specified list.

NOT IN Example

SELECT *
FROM Students
WHERE Department NOT IN (
    'Computer Science',
    'Physics'
);

This query returns students whose department is neither Computer Science nor Physics.

📈 IN with ORDER BY

After filtering rows using IN, the results can be sorted using the ORDER BY clause.

IN with ORDER BY

SELECT Name,
       Department,
       Marks
FROM Students
WHERE Department IN (
    'Computer Science',
    'Mathematics'
)
ORDER BY Marks DESC;

🔍 IN with Subqueries

The IN operator can compare values against the results of another query instead of a fixed list.

IN with a Subquery

SELECT Name,
       Department
FROM Students
WHERE Department IN (
    SELECT DepartmentName
    FROM Departments
    WHERE IsActive = TRUE
);

In this example, the inner query returns a list of active departments, and the outer query retrieves students who belong to those departments.

📊 Common Uses of IN

Use CaseExample
Filter by multiple text values Department IN ('CS', 'IT')
Filter by numeric values StudentID IN (101, 102)
Exclude values Department NOT IN ('Physics')
Compare with subquery results Department IN (SELECT ...)

âš ī¸ IN and NULL Values

Be careful when using NOT IN with lists or subqueries that may contain NULL values. Depending on the data, this can produce unexpected results because comparisons involving NULL evaluate to unknown.

Important

When using NOT IN with a subquery, ensure the subquery does not return NULL values, or consider alternative approaches such as NOT EXISTS when appropriate.

đŸ’ŧ Real-World Example

A university administrator wants to generate a report containing students from the Computer Science, Mathematics, and Physics departments.

Real-World Query

SELECT StudentID,
       Name,
       Department,
       Marks
FROM Students
WHERE Department IN (
    'Computer Science',
    'Mathematics',
    'Physics'
)
ORDER BY Department,
         Marks DESC;

This query retrieves students from the selected departments and organizes the results by department and marks.

âš ī¸ Best Practices

Best Practice

Use IN instead of multiple OR conditions when checking the same column against several values. Keep value lists organized for readability, use subqueries with IN only when appropriate, and be cautious when using NOT IN if NULL values may be present.

🚀 Key Points to Remember

  • 📌 IN checks whether a value exists in a list.
  • 📌 It provides a cleaner alternative to multiple OR conditions.
  • 📌 IN works with text, numbers, dates, and subqueries.
  • 📌 NOT IN excludes values found in the specified list.
  • 📌 Be careful when using NOT IN with NULL values.
  • 📌 IN improves the readability and maintainability of SQL queries.
>>"The IN operator simplifies SQL by replacing long lists of OR conditions with a single, readable expression."

Summary

✅ The IN operator is a powerful SQL feature for filtering records against multiple values or the results of a subquery. It improves query readability, reduces repetitive conditions, and helps create cleaner, more maintainable SQL statements.