SQL IN Operator Explained with Practical Examples
Learn how SQL IN and NOT IN work with practical examples, subqueries, common mistakes, and safe filtering patterns.
SQL, or Structured Query Language, is a standard programming language specifically for managing data in relational databases. One of its powerful operators is the IN operator. This article will provide an in-depth look into the SQL IN operator, its uses, common mistakes, and more.
What the SQL IN Operator Does
The IN operator lets you filter results against a list of acceptable values, shortening your WHERE clause by avoiding multiple OR conditions. Always enclose the comma-separated list of values in parentheses.
Consider a sample table Students:
| ID | Name | Age | Grade |
|---|---|---|---|
| 1 | Alice | 23 | A |
| 2 | Bob | 25 | B |
| 3 | Charlie | 23 | A |
| 4 | David | 24 | C |
To select students with grades A or B:
SELECT Name
FROM Students
WHERE Grade IN ('A', 'B');
Result:
| Name |
|---|
| Alice |
| Bob |
| Charlie |
Explanation:
In this example, the query retrieves students who have grades A or B.
IN vs =: When to Use Each
Use the IN operator when you need to match against multiple potential values, but stick to the = operator for single-value comparisons as it is more readable.
| Aspect | = Operator | IN Operator |
|---|---|---|
| Values | Single | Multiple |
| Syntax | column = value | column IN (value1, value2, ...) |
| Use Case | Comparing against one value | Comparing against a list of values |
Using IN with Strings
Strings can easily be used with the IN operator by enclosing them in single quotes.
Example:
To select students named Alice or David:
SELECT Name
FROM Students
WHERE Name IN ('Alice', 'David');
Result:
| Name |
|---|
| Alice |
| David |
Explanation:
Here, the query retrieves only the students named Alice and David.
Using IN with Numbers
The IN operator can also handle numerical values.
Example:
To select students with ID 1 or 4:
SELECT Name
FROM Students
WHERE ID IN (1, 4);
Result:
| Name |
|---|
| Alice |
| David |
Explanation:
This query fetches students whose IDs are either 1 or 4.
Using IN with Dates
For date values, ensure they are formatted correctly.
Example:
Consider a table Orders:
| OrderID | Product | OrderDate |
|---|---|---|
| 1 | Apple | 2023-01-10 |
| 2 | Banana | 2023-02-15 |
| 3 | Cherry | 2023-01-10 |
To select orders made on 2023-01-10:
SELECT Product
FROM Orders
WHERE OrderDate IN ('2023-01-10');
Result:
| Product |
|---|
| Apple |
| Cherry |
Explanation:
This query retrieves products ordered on 2023-01-10.
Using IN with Columns
You can compare columns using the IN operator.
Example:
To select students with the same age as Alice:
SELECT Name
FROM Students
WHERE Age IN (SELECT Age FROM Students WHERE Name = 'Alice');
Result:
| Name |
|---|
| Alice |
| Charlie |
Explanation:
Both Alice and Charlie share the same age.
Using IN with Subqueries
Subqueries can return a list of values for the IN operator.
Example:
To select students not in grade A:
SELECT Name
FROM Students
WHERE Grade NOT IN (SELECT Grade FROM Students WHERE Grade = 'A');
Result:
| Name |
|---|
| Bob |
| David |
Explanation:
This query retrieves students who aren't in grade A.
NOT IN Operator
The NOT IN operator fetches rows that don't match values in a list.
Example:
To select students not named Alice or David:
SELECT Name
FROM Students
WHERE Name NOT IN ('Alice', 'David');
Result:
| Name |
|---|
| Bob |
| Charlie |
Explanation:
Here, Bob and Charlie are the students not named Alice or David.
Applying IN in SELECT, UPDATE Statements
SELECT
We've seen the use of IN in the SELECT statement in previous examples.
UPDATE
You can use IN in the UPDATE statement to modify multiple rows.
Example:
To update grades of Alice and David to D:
UPDATE Students
SET Grade = 'D'
WHERE Name IN ('Alice', 'David');
Explanation:
After this, both Alice and David will have a grade of D.
Practice IN and NOT IN in DbSchema
Before running list-based filters on production tables, test your predicates with a quick SELECT preview in DbSchema. It helps you validate value lists and spot unexpected matches before you move to UPDATE or DELETE.
Common Mistakes
- Forgetting the Parentheses: Always enclose values in parentheses.
- Mixing Data Types: Ensure consistent data types within the IN list.
- Overusing: For single value comparison, the = operator is more readable.
Frequently Asked Questions
- Is IN case-sensitive? - It depends on the database. Some databases are case-sensitive, while others aren't.
- Can IN be used with JOIN? - Yes, you can combine JOIN and IN in a query.
- Is there a limit to values in IN? - Some databases may have a limit. However, excessively long lists can decrease performance.
Practice Questions
- Write a query to find products not sold on 2023-02-15.
- Update student grades to F for ages 23 and 25.
- Fetch students with grades other than A and B.
- Retrieve orders placed on 2023-01-10 and 2023-02-15.
Conclusion
The SQL IN operator is a versatile and powerful tool in database querying, offering a concise way to filter results based on a list of values. Whether you're working with strings, numbers, dates, or even subqueries, understanding the IN operator can significantly improve your queries. This comprehensive guide aimed to provide a clear, detailed, and practical overview of its applications, ensuring that both beginners and experienced database enthusiasts can harness its full potential. As always, hands-on practice is the key to mastering any concept, so make use of the practice questions and real-world scenarios to refine your skills.
Happy data exploring!
Want to try these queries against a real schema? Download DbSchema and run them in its SQL Editor, which is included in the free Community Edition.
Try your IN lists against a real schema
DbSchema reverse-engineers your database, runs SELECT previews in its SQL Editor and shows the matching rows in a result grid — the free Community Edition includes both.

