Lesson 22 of 60 – IN Operator
37%

IN Operator in SQL

The IN operator is used to check whether a value matches any value in a specified list. It is useful when you want to search for multiple possible values in a column.

Note: The IN operator can make a query shorter and easier to read than using multiple OR conditions.

1. What is IN?

The IN operator allows you to specify multiple values inside a WHERE condition.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi', 'Mumbai');

This returns students whose city is Patna, Delhi, or Mumbai.

2. Basic IN Syntax

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

The values inside the parentheses are the values that SQL will compare with the selected column.

3. IN with Text Values

The IN operator can be used with text values such as city names, course names, departments, and statuses.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Gaya', 'Delhi');

This returns students from Patna, Gaya, or Delhi.

4. IN with Numbers

IN can also be used with numeric values.

SELECT *
FROM Students
WHERE age IN (18, 20, 22);

This returns students whose age is 18, 20, or 22.

5. IN with Course Names

IN is very useful when you want to search for students belonging to several courses.

SELECT name, course
FROM Students
WHERE course IN ('Python', 'Java', 'PHP');

This returns students enrolled in Python, Java, or PHP.

6. IN vs OR

The following query uses multiple OR conditions:

SELECT *
FROM Students
WHERE city = 'Patna'
OR city = 'Delhi'
OR city = 'Mumbai';

The same condition can be written using IN:

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi', 'Mumbai');

IN provides a shorter and easier way to write this type of condition.

7. IN with WHERE

IN is commonly used with the WHERE clause.

SELECT name, course
FROM Students
WHERE course IN ('Python', 'Java');

Only students studying Python or Java are returned.

8. IN with SELECT *

You can use IN with SELECT * to retrieve all columns from matching records.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi');

All columns of students from Patna or Delhi are returned.

9. IN with Specific Columns

You can select only the columns that you need.

SELECT name, mobile, city
FROM Students
WHERE city IN ('Patna', 'Gaya');

This displays the student's name, mobile number, and city.

10. IN with Student IDs

IN can be used to search for multiple student IDs.

SELECT *
FROM Students
WHERE student_id IN (101, 105, 110, 115);

This returns students whose IDs are present in the specified list.

11. IN with Fees

IN can be used with numeric columns such as fees.

SELECT *
FROM Students
WHERE fee IN (5000, 10000, 15000);

This returns records whose fee is exactly 5000, 10000, or 15000.

12. IN with Employee Departments

IN is useful when searching for employees from multiple departments.

SELECT name, department
FROM Employees
WHERE department IN ('IT', 'HR', 'Sales');

This returns employees from the IT, HR, or Sales departments.

13. IN with Product Categories

IN can be used in shopping or product management systems.

SELECT product_name, category
FROM Products
WHERE category IN ('Laptop', 'Mobile', 'Tablet');

This returns products belonging to the selected categories.

14. NOT IN

Use NOT IN when you want to exclude multiple specified values.

SELECT *
FROM Students
WHERE city NOT IN ('Patna', 'Delhi');

This returns students whose city is not Patna or Delhi.

15. NOT IN with Courses

NOT IN can be used to exclude particular courses.

SELECT *
FROM Students
WHERE course NOT IN ('Tally', 'PHP');

This returns students who are not enrolled in Tally or PHP.

16. IN with AND

IN can be combined with AND to apply additional conditions.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi')
AND age >= 18;

This returns students from Patna or Delhi who are at least 18 years old.

17. IN with OR

IN can also be combined with OR.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi')
OR course IN ('Python', 'Java');

This returns records that satisfy either condition.

18. IN with ORDER BY

IN can be combined with ORDER BY to sort the matching records.

SELECT name, city
FROM Students
WHERE city IN ('Patna', 'Delhi', 'Mumbai')
ORDER BY name ASC;

The matching students are sorted alphabetically by name.

19. IN with ORDER BY DESC

SELECT name, fee
FROM Students
WHERE course IN ('Python', 'Java')
ORDER BY fee DESC;

This returns Python and Java students with the highest fee first.

20. IN with LIMIT

In MySQL, LIMIT can be used to restrict the number of records returned.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi')
LIMIT 5;

This returns up to five matching records.

21. IN with DISTINCT

DISTINCT can be used with IN to display unique matching values.

SELECT DISTINCT city
FROM Students
WHERE city IN ('Patna', 'Delhi', 'Gaya');

This displays each matching city only once.

22. IN with Aggregate Functions

IN can be used with aggregate functions such as COUNT().

SELECT COUNT(*)
FROM Students
WHERE course IN ('Python', 'Java');

This counts students who are enrolled in Python or Java.

23. IN with Dates

IN can be used when you want to match specific dates.

SELECT *
FROM Students
WHERE admission_date IN
('2026-01-10', '2026-02-15', '2026-03-20');

This returns records having one of the specified admission dates.

24. IN with a Subquery

IN can also be used with a subquery. A subquery is a query written inside another SQL query.

SELECT *
FROM Students
WHERE course_id IN
(
    SELECT id
    FROM Courses
    WHERE fee > 10000
);

The inner query finds courses with fees greater than 10000, and the outer query finds students belonging to those courses.

25. IN with Multiple Conditions

You can use more than one IN condition in the same query.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi')
AND course IN ('Python', 'Java');

This finds students from the selected cities who are studying the selected courses.

26. IN with LIKE

IN and LIKE can be used together when you need different types of conditions.

SELECT *
FROM Students
WHERE course IN ('Python', 'Java')
AND name LIKE 'A%';

This finds Python or Java students whose names start with A.

27. Common Mistake with Text Values

A common mistake is forgetting quotation marks around text values.

Incorrect:

SELECT *
FROM Students
WHERE city IN (Patna, Delhi);

Correct:

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi');

Text values should normally be written inside quotes.

28. IN with NULL Values

NULL represents a missing or unknown value. IN should not be used as a replacement for checking NULL values.

SELECT *
FROM Students
WHERE city IN ('Patna', 'Delhi');

Rows where city is NULL do not match this IN condition.

To check for NULL values, use IS NULL instead.

SELECT *
FROM Students
WHERE city IS NULL;

29. Practical Example

Suppose an institute wants to find students from Patna or Delhi who are studying Python or Java.

SELECT name, city, course
FROM Students
WHERE city IN ('Patna', 'Delhi')
AND course IN ('Python', 'Java');

This query uses two IN conditions to filter the students.

30. Complete IN Example

SELECT name, age, course, city, fee
FROM Students
WHERE city IN ('Patna', 'Delhi', 'Gaya')
AND course IN ('Python', 'Java')
AND age BETWEEN 18 AND 30
ORDER BY fee DESC;

This query:

  • Finds students from Patna, Delhi, or Gaya.
  • Allows Python or Java courses.
  • Checks students between 18 and 30 years old.
  • Sorts the results by fee from highest to lowest.

📌 Key Points

  • IN is used to match a value against multiple specified values.
  • IN can make multiple OR conditions shorter and easier to read.
  • IN can be used with text values.
  • IN can be used with numeric values.
  • NOT IN is used to exclude specified values.
  • IN can be combined with AND and OR.
  • IN can be used with ORDER BY and LIMIT.
  • IN can be combined with DISTINCT and aggregate functions.
  • IN can be used with dates.
  • IN can also be used with subqueries.
  • NULL values should be checked using IS NULL or IS NOT NULL.
  • IN is especially useful when searching for several specific values.

🧠 Quick Quiz

Question: Which SQL operator is used to check whether a value matches any value from a specified list?