The SQL IN operator is used to check whether a value matches any value from a list. Instead of writing multiple OR conditions, you can use the IN operator to make your SQL queries shorter, cleaner, and easier to understand. SQL IN Operator practice questions with solutions help to understand the concepts.
For example, suppose you want to display students who belong to Delhi, Noida, or Gurgaon.
Without IN, you would write:
SELECT *
FROM students
WHERE city = 'Delhi'
OR city = 'Noida'
OR city = 'Gurgaon';
Using the IN operator, the same query becomes:
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida', 'Gurgaon');
As the number of values increases, the IN operator becomes much easier to read and maintain.
The IN operator is widely used in:
- Student Management Systems
- Employee Databases
- Banking Applications
- Hospital Management Systems
- E-commerce Websites
- Inventory Management
- Business Intelligence Reports
- CRM Software
What is the SQL IN Operator?
The IN operator checks whether a column value exists in a specified list.
Basic Syntax
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, value3);
Example
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida');
The query returns students who belong to either Delhi or Noida.
Why Use the IN Operator?
The IN operator helps you:
- Replace multiple
ORconditions - Write cleaner SQL queries
- Improve query readability
- Reduce typing
- Make SQL easier to maintain
- Handle multiple search values efficiently
Sample Table Used Throughout This Chapter
students
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 106 | Ankit | 22 | Python | Delhi | 82 |
| 107 | Sneha | 21 | SQL | Noida | 89 |
1. SQL Query to Display Students from Delhi and Noida
Problem Statement
Write an SQL query to display students who belong to Delhi or Noida.
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida');
Sample Output
| id | name | city | course | marks |
|---|---|---|---|---|
| 101 | Rahul | Delhi | Python | 88 |
| 102 | Amit | Noida | Java | 91 |
| 103 | Neha | Delhi | SQL | 95 |
| 106 | Ankit | Delhi | Python | 82 |
| 107 | Sneha | Noida | SQL | 89 |
Explanation
The IN operator checks whether the city value is present in the specified list.
Equivalent query using OR:
WHERE city='Delhi'
OR city='Noida'
The IN operator makes the query much cleaner.
Concepts Covered
- IN Operator
- Multiple Values
- Alternative to OR
2. SQL Query to Display Java and SQL Students
Problem Statement
Write an SQL query to display students enrolled in Java or SQL courses.
SQL Solution
SELECT *
FROM students
WHERE course IN ('Java', 'SQL');
Sample Output
| id | name | course | city | marks |
|---|---|---|---|---|
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 105 | Rohit | Java | Faridabad | 90 |
| 107 | Sneha | SQL | Noida | 89 |
Explanation
The query searches for students whose course is either:
- Java
- SQL
If the value matches any item in the list, the row is returned.
Concepts Covered
- IN
- Text Filtering
- Multiple Conditions
3. SQL Query to Display Students with Marks 88, 90, and 95
Problem Statement
Write an SQL query to display students whose marks are 88, 90, or 95.
SQL Solution
SELECT *
FROM students
WHERE marks IN (88, 90, 95);
Sample Output
| id | name | marks |
|---|---|---|
| 101 | Rahul | 88 |
| 103 | Neha | 95 |
| 105 | Rohit | 90 |
Explanation
The IN operator also works with numeric values.
Instead of writing:
WHERE marks = 88
OR marks = 90
OR marks = 95
You can simply write:
WHERE marks IN (88, 90, 95)
This improves readability and simplifies query maintenance.
Concepts Covered
- IN Operator
- Numeric Values
- Query Simplification
4. SQL Query Using IN with Student IDs
Problem Statement
Write an SQL query to display students whose Student IDs are 101, 104, and 107.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE id IN (101, 104, 107);
Sample Output
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 104 | Priya | Python | Gurgaon | 84 |
| 107 | Sneha | SQL | Noida | 89 |
Explanation
The IN operator checks whether the id exists in the given list.
Only the matching student records are returned.
This type of query is useful for:
- Searching specific records
- Bulk retrieval
- Report generation
Concepts Covered
- IN Operator
- Numeric Filtering
- Multiple Values
5. SQL Query Using IN with Multiple Cities
Problem Statement
Write an SQL query to display students who belong to Delhi, Noida, or Faridabad.
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida', 'Faridabad');
Sample Output
| id | name | city | course | marks |
|---|---|---|---|---|
| 101 | Rahul | Delhi | Python | 88 |
| 102 | Amit | Noida | Java | 91 |
| 103 | Neha | Delhi | SQL | 95 |
| 105 | Rohit | Faridabad | Java | 90 |
| 106 | Ankit | Delhi | Python | 82 |
| 107 | Sneha | Noida | SQL | 89 |
Explanation
The query checks whether the city value matches any value in the specified list.
Instead of writing multiple OR conditions, the IN operator keeps the query short and readable.
Equivalent query:
SELECT *
FROM students
WHERE city = 'Delhi'
OR city = 'Noida'
OR city = 'Faridabad';
Using IN is the preferred approach when checking multiple values.
Concepts Covered
- IN Operator
- Text Filtering
- Alternative to OR
- Query Optimization
6. SQL IN with WHERE Clause
Problem Statement
Write an SQL query to display students whose course is Java or SQL and whose marks are greater than 90.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE course IN ('Java', 'SQL')
AND marks > 90;
Sample Output
| id | name | course | city | marks |
|---|---|---|---|---|
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
Explanation
SQL performs the following operations:
- Checks whether the course is Java or SQL.
- Filters students whose marks are greater than 90.
- Displays only records satisfying both conditions.
Concepts Covered
- IN Operator
- WHERE
- AND
- Multiple Conditions
7. SQL IN with ORDER BY
Problem Statement
Write an SQL query to display students from Delhi and Noida, sorted by marks in descending order.
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
ORDER BY marks DESC;
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 103 | Neha | Delhi | 95 |
| 102 | Amit | Noida | 91 |
| 107 | Sneha | Noida | 89 |
| 101 | Rahul | Delhi | 88 |
| 106 | Ankit | Delhi | 82 |
Explanation
The query first filters students from Delhi and Noida.
Then, it sorts the filtered records from highest marks to lowest marks.
Concepts Covered
- IN
- ORDER BY
- DESC
- Sorting Filtered Data
8. SQL Query Using IN with Marks
Problem Statement
Write an SQL query to display students whose marks are 82, 84, or 89.
SQL Solution
SELECT *
FROM students
WHERE marks IN (82, 84, 89);
Sample Output
| id | name | marks |
|---|---|---|
| 104 | Priya | 84 |
| 106 | Ankit | 82 |
| 107 | Sneha | 89 |
Explanation
The IN operator checks whether the marks value matches any number in the given list.
This is much cleaner than writing several OR conditions.
Concepts Covered
- IN
- Numeric Filtering
- Multiple Values
9. SQL Query Using IN with Multiple Courses
Problem Statement
Write an SQL query to display students enrolled in Python or Java courses.
SQL Solution
SELECT *
FROM students
WHERE course IN ('Python', 'Java');
Sample Output
| id | name | course | city |
|---|---|---|---|
| 101 | Rahul | Python | Delhi |
| 102 | Amit | Java | Noida |
| 104 | Priya | Python | Gurgaon |
| 105 | Rohit | Java | Faridabad |
| 106 | Ankit | Python | Delhi |
Explanation
The query returns all students whose course matches either Python or Java.
The IN operator simplifies searching for multiple values.
Concepts Covered
- IN
- Text Filtering
- Multiple Values
10. SQL IN with AND Condition
Problem Statement
Write an SQL query to display students who:
- Belong to Delhi or Noida
- Study Python
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
AND course = 'Python';
Sample Output
| id | name | city | course | marks |
|---|---|---|---|---|
| 101 | Rahul | Delhi | Python | 88 |
| 106 | Ankit | Delhi | Python | 82 |
Explanation
The query combines:
IN→ to filter multiple cities.AND→ to include only Python students.
Only records satisfying both conditions are displayed.
Concepts Covered
6. SQL IN with WHERE Clause
Problem Statement
Write an SQL query to display students whose course is Java or SQL and whose marks are greater than 90.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE course IN ('Java', 'SQL')
AND marks > 90;
Sample Output
| id | name | course | city | marks |
|---|---|---|---|---|
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
Explanation
SQL performs the following operations:
- Checks whether the course is Java or SQL.
- Filters students whose marks are greater than 90.
- Displays only records satisfying both conditions.
Concepts Covered
- IN Operator
- WHERE
- AND
- Multiple Conditions
7. SQL IN with ORDER BY
Problem Statement
Write an SQL query to display students from Delhi and Noida, sorted by marks in descending order.
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
ORDER BY marks DESC;
Sample Output
| id | name | city | marks |
|---|---|---|---|
| 103 | Neha | Delhi | 95 |
| 102 | Amit | Noida | 91 |
| 107 | Sneha | Noida | 89 |
| 101 | Rahul | Delhi | 88 |
| 106 | Ankit | Delhi | 82 |
Explanation
The query first filters students from Delhi and Noida.
Then, it sorts the filtered records from highest marks to lowest marks.
Concepts Covered
- IN
- ORDER BY
- DESC
- Sorting Filtered Data
8. SQL Query Using IN with Marks
Problem Statement
Write an SQL query to display students whose marks are 82, 84, or 89.
SQL Solution
SELECT *
FROM students
WHERE marks IN (82, 84, 89);
Sample Output
| id | name | marks |
|---|---|---|
| 104 | Priya | 84 |
| 106 | Ankit | 82 |
| 107 | Sneha | 89 |
Explanation
The IN operator checks whether the marks value matches any number in the given list.
This is much cleaner than writing several OR conditions.
Concepts Covered
- IN
- Numeric Filtering
- Multiple Values
9. SQL Query Using IN with Multiple Courses
Problem Statement
Write an SQL query to display students enrolled in Python or Java courses.
SQL Solution
SELECT *
FROM students
WHERE course IN ('Python', 'Java');
Sample Output
| id | name | course | city |
|---|---|---|---|
| 101 | Rahul | Python | Delhi |
| 102 | Amit | Java | Noida |
| 104 | Priya | Python | Gurgaon |
| 105 | Rohit | Java | Faridabad |
| 106 | Ankit | Python | Delhi |
Explanation
The query returns all students whose course matches either Python or Java.
The IN operator simplifies searching for multiple values.
Concepts Covered
- IN
- Text Filtering
- Multiple Values
10. SQL IN with AND Condition
Problem Statement
Write an SQL query to display students who:
- Belong to Delhi or Noida
- Study Python
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
AND course = 'Python';
Sample Output
| id | name | city | course | marks |
|---|---|---|---|---|
| 101 | Rahul | Delhi | Python | 88 |
| 106 | Ankit | Delhi | Python | 82 |
Explanation
The query combines:
IN→ to filter multiple cities.AND→ to include only Python students.
Only records satisfying both conditions are displayed.
Concepts Covered
- IN
- WHERE
- AND
- Multiple Conditions
11. SQL IN with ORDER BY DESC
Problem Statement
Write an SQL query to display students enrolled in Python or SQL courses, sorted by marks in descending order.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT *
FROM students
WHERE course IN ('Python', 'SQL')
ORDER BY marks DESC;
Sample Output
| id | name | course | marks |
|---|---|---|---|
| 103 | Neha | SQL | 95 |
| 107 | Sneha | SQL | 89 |
| 101 | Rahul | Python | 88 |
| 104 | Priya | Python | 84 |
| 106 | Ankit | Python | 82 |
Explanation
The query first filters students enrolled in Python or SQL.
After filtering, SQL sorts the results by marks in descending order.
This type of query is commonly used to generate ranked reports for selected categories.
Concepts Covered
- IN
- ORDER BY
- DESC
- Filtering
12. SQL IN with DISTINCT
Problem Statement
Write an SQL query to display all unique cities where students are enrolled in Python or Java courses.
SQL Solution
SELECT DISTINCT city
FROM students
WHERE course IN ('Python', 'Java');
Sample Output
| city |
|---|
| Delhi |
| Noida |
| Gurgaon |
| Faridabad |
Explanation
The query first filters students studying Python or Java.
Then, the DISTINCT keyword removes duplicate city names.
This query is useful for identifying locations where selected courses are available.
Concepts Covered
- DISTINCT
- IN
- Unique Records
13. SQL IN with LIMIT
Problem Statement
Write an SQL query to display the first two students from Delhi and Noida.
SQL Solution
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
LIMIT 2;
Sample Output
| id | name | city |
|---|---|---|
| 101 | Rahul | Delhi |
| 102 | Amit | Noida |
Explanation
The query:
- Filters students from Delhi and Noida.
- Returns only the first two matching records.
This is useful when previewing filtered data or implementing pagination.
Concepts Covered
- IN
- LIMIT
- Data Preview
14. SQL NOT IN Basics
Problem Statement
Write an SQL query to display students not enrolled in the Python course.
SQL Solution
SELECT *
FROM students
WHERE course NOT IN ('Python');
Sample Output
| id | name | course | city |
|---|---|---|---|
| 102 | Amit | Java | Noida |
| 103 | Neha | SQL | Delhi |
| 105 | Rohit | Java | Faridabad |
| 107 | Sneha | SQL | Noida |
Explanation
The NOT IN operator returns rows whose values are not present in the specified list.
It is the opposite of the IN operator.
Concepts Covered
- NOT IN
- Excluding Records
- Multiple Values
15. Real-World SQL IN Example
Problem Statement
An online learning platform wants to display students enrolled in Python, Java, or SQL courses.
Write an SQL query to retrieve these records.
SQL Solution
SELECT *
FROM students
WHERE course IN ('Python', 'Java', 'SQL');
Sample Output
| id | name | course | city |
|---|---|---|---|
| 101 | Rahul | Python | Delhi |
| 102 | Amit | Java | Noida |
| 103 | Neha | SQL | Delhi |
| 104 | Priya | Python | Gurgaon |
| 105 | Rohit | Java | Faridabad |
| 106 | Ankit | Python | Delhi |
| 107 | Sneha | SQL | Noida |
Explanation
This query retrieves students enrolled in any of the specified courses.
Real-world uses include:
- Course Management Systems
- Student Dashboards
- Employee Skill Reports
- Product Category Filters
- CRM Applications
The IN operator makes the query more readable and scalable compared to using multiple OR conditions.
Concepts Covered
- IN Operator
- Multiple Values
- Real-world SQL Queries
- Query Optimization
Chapter Summary
In this chapter, you learned how to use the SQL IN operator to filter records by matching multiple values in a single query. Instead of writing several OR conditions, the IN operator provides a cleaner, shorter, and more readable solution.
You also explored how to combine the IN operator with other SQL clauses such as WHERE, ORDER BY, LIMIT, DISTINCT, and NOT IN to build more practical queries.
Throughout this chapter, you covered:
- Introduction to the
INOperator - Replacing Multiple
ORConditions - Using
INwith Text Values - Using
INwith Numeric Values INwithWHEREINwithORDER BYINwithDISTINCTINwithLIMITNOT IN- Real-world SQL Examples
The IN operator is widely used in filtering reports, dashboards, search systems, and business applications.
Key Takeaways
- The
INoperator checks whether a value exists in a specified list. - It replaces multiple
ORconditions with cleaner syntax. INworks with both text and numeric values.INis commonly combined with theWHEREclause.- It can also be used with
ORDER BY,LIMIT, andDISTINCT. NOT INis used to exclude values from a list.- The
INoperator improves query readability and maintenance. - It is widely used in reporting and analytics.
INdoes not modify the original table data.- It is one of the most commonly asked SQL interview topics.
Frequently Asked Questions (FAQs)
1. What is the SQL IN operator?
The IN operator checks whether a value matches any value in a specified list.
Example:
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida');
2. Why should I use IN instead of multiple OR conditions?
Using IN makes queries:
- Shorter
- Easier to read
- Easier to maintain
Instead of:
WHERE city = 'Delhi'
OR city = 'Noida'
OR city = 'Gurgaon'
You can write:
WHERE city IN ('Delhi', 'Noida', 'Gurgaon');
3. Can IN be used with numbers?
Yes.
Example:
SELECT *
FROM students
WHERE marks IN (88, 90, 95);
The IN operator works with numeric values as well as text values.
4. Can IN be combined with WHERE?
Yes.
Example:
SELECT *
FROM students
WHERE course IN ('Python', 'Java');
The WHERE clause filters rows using the values provided in the IN list.
5. What is NOT IN?
NOT IN returns records whose values are not present in the specified list.
Example:
SELECT *
FROM students
WHERE course NOT IN ('Python');
This query returns all students who are not enrolled in the Python course.
6. Can IN be combined with ORDER BY?
Yes.
Example:
SELECT *
FROM students
WHERE city IN ('Delhi', 'Noida')
ORDER BY marks DESC;
SQL filters the rows first and then sorts the results.
7. Where is the IN operator used in real-world applications?
The IN operator is commonly used in:
- Banking Applications
- Hospital Management Systems
- CRM Software
- Student Portals
- Employee Management
- Inventory Systems
- Business Intelligence Dashboards
- Sales Reports
- Product Filtering
- E-commerce Platforms
8. Can IN be used with subqueries?
Yes.
Example:
SELECT name
FROM students
WHERE course_id IN
(
SELECT course_id
FROM courses
WHERE duration > 6
);
This allows SQL to compare values returned by another query.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
