SQL IN Operator Practice Questions with Solutions

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 OR conditions
  • 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

idnameagecoursecitymarks
101Rahul21PythonDelhi88
102Amit22JavaNoida91
103Neha20SQLDelhi95
104Priya23PythonGurgaon84
105Rohit21JavaFaridabad90
106Ankit22PythonDelhi82
107Sneha21SQLNoida89

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

idnamecitycoursemarks
101RahulDelhiPython88
102AmitNoidaJava91
103NehaDelhiSQL95
106AnkitDelhiPython82
107SnehaNoidaSQL89

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

idnamecoursecitymarks
102AmitJavaNoida91
103NehaSQLDelhi95
105RohitJavaFaridabad90
107SnehaSQLNoida89

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

idnamemarks
101Rahul88
103Neha95
105Rohit90

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

idnamecoursecitymarks
101RahulPythonDelhi88
102AmitJavaNoida91
103NehaSQLDelhi95
104PriyaPythonGurgaon84
105RohitJavaFaridabad90
106AnkitPythonDelhi82
107SnehaSQLNoida89

SQL Solution

SELECT *

FROM students

WHERE id IN (101, 104, 107);

Sample Output

idnamecoursecitymarks
101RahulPythonDelhi88
104PriyaPythonGurgaon84
107SnehaSQLNoida89

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

idnamecitycoursemarks
101RahulDelhiPython88
102AmitNoidaJava91
103NehaDelhiSQL95
105RohitFaridabadJava90
106AnkitDelhiPython82
107SnehaNoidaSQL89

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

idnamecoursecitymarks
101RahulPythonDelhi88
102AmitJavaNoida91
103NehaSQLDelhi95
104PriyaPythonGurgaon84
105RohitJavaFaridabad90
106AnkitPythonDelhi82
107SnehaSQLNoida89

SQL Solution

SELECT *

FROM students

WHERE course IN ('Java', 'SQL')
AND marks > 90;

Sample Output

idnamecoursecitymarks
102AmitJavaNoida91
103NehaSQLDelhi95

Explanation

SQL performs the following operations:

  1. Checks whether the course is Java or SQL.
  2. Filters students whose marks are greater than 90.
  3. 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

idnamecitymarks
103NehaDelhi95
102AmitNoida91
107SnehaNoida89
101RahulDelhi88
106AnkitDelhi82

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

idnamemarks
104Priya84
106Ankit82
107Sneha89

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

idnamecoursecity
101RahulPythonDelhi
102AmitJavaNoida
104PriyaPythonGurgaon
105RohitJavaFaridabad
106AnkitPythonDelhi

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

idnamecitycoursemarks
101RahulDelhiPython88
106AnkitDelhiPython82

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

idnamecoursecitymarks
101RahulPythonDelhi88
102AmitJavaNoida91
103NehaSQLDelhi95
104PriyaPythonGurgaon84
105RohitJavaFaridabad90
106AnkitPythonDelhi82
107SnehaSQLNoida89

SQL Solution

SELECT *

FROM students

WHERE course IN ('Java', 'SQL')
AND marks > 90;

Sample Output

idnamecoursecitymarks
102AmitJavaNoida91
103NehaSQLDelhi95

Explanation

SQL performs the following operations:

  1. Checks whether the course is Java or SQL.
  2. Filters students whose marks are greater than 90.
  3. 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

idnamecitymarks
103NehaDelhi95
102AmitNoida91
107SnehaNoida89
101RahulDelhi88
106AnkitDelhi82

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

idnamemarks
104Priya84
106Ankit82
107Sneha89

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

idnamecoursecity
101RahulPythonDelhi
102AmitJavaNoida
104PriyaPythonGurgaon
105RohitJavaFaridabad
106AnkitPythonDelhi

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

idnamecitycoursemarks
101RahulDelhiPython88
106AnkitDelhiPython82

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

idnamecoursecitymarks
101RahulPythonDelhi88
102AmitJavaNoida91
103NehaSQLDelhi95
104PriyaPythonGurgaon84
105RohitJavaFaridabad90
106AnkitPythonDelhi82
107SnehaSQLNoida89

SQL Solution

SELECT *

FROM students

WHERE course IN ('Python', 'SQL')

ORDER BY marks DESC;

Sample Output

idnamecoursemarks
103NehaSQL95
107SnehaSQL89
101RahulPython88
104PriyaPython84
106AnkitPython82

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

idnamecity
101RahulDelhi
102AmitNoida

Explanation

The query:

  1. Filters students from Delhi and Noida.
  2. 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

idnamecoursecity
102AmitJavaNoida
103NehaSQLDelhi
105RohitJavaFaridabad
107SnehaSQLNoida

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

idnamecoursecity
101RahulPythonDelhi
102AmitJavaNoida
103NehaSQLDelhi
104PriyaPythonGurgaon
105RohitJavaFaridabad
106AnkitPythonDelhi
107SnehaSQLNoida

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 IN Operator
  • Replacing Multiple OR Conditions
  • Using IN with Text Values
  • Using IN with Numeric Values
  • IN with WHERE
  • IN with ORDER BY
  • IN with DISTINCT
  • IN with LIMIT
  • NOT IN
  • Real-world SQL Examples

The IN operator is widely used in filtering reports, dashboards, search systems, and business applications.


Key Takeaways

  • The IN operator checks whether a value exists in a specified list.
  • It replaces multiple OR conditions with cleaner syntax.
  • IN works with both text and numeric values.
  • IN is commonly combined with the WHERE clause.
  • It can also be used with ORDER BY, LIMIT, and DISTINCT.
  • NOT IN is used to exclude values from a list.
  • The IN operator improves query readability and maintenance.
  • It is widely used in reporting and analytics.
  • IN does 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.

Scroll to Top