MYSQL Tutorial



MySQL IN


MySQL IN Operator – Check Multiple Values Easily

The IN operator allows you to specify multiple values in a WHERE clause. It is a shorthand for multiple OR conditions and makes queries cleaner and easier to read.

🔹 Syntax

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

🔹 Examples

1. Select employees who work in departments 1, 3, or 5:

SELECT * FROM employees
WHERE department_id IN (1, 3, 5);
  

Explanation: Returns rows where department_id is 1, 3, or 5.

2. Select products that are in specific categories:

SELECT * FROM products
WHERE category IN ('Books', 'Electronics', 'Clothing');
  

Explanation: Returns products whose category matches any of the listed values.

3. Select customers from a list of specific cities:

SELECT * FROM customers
WHERE city IN ('Mumbai', 'Delhi', 'Bangalore');
  

Explanation: Returns customers located in Mumbai, Delhi, or Bangalore.

🔹 Using NOT IN

You can use NOT IN to exclude multiple values:

SELECT * FROM employees
WHERE department_id NOT IN (2, 4);
  

Explanation: Selects employees who are NOT in department 2 or 4.

🔹 Practice Example

Find orders where the status is either 'Pending', 'Processing', or 'On Hold'.

SELECT * FROM orders
WHERE status IN ('Pending', 'Processing', 'On Hold');
  

🔹 Summary

  • IN checks if a value exists in a list of values.
  • Makes your query shorter and more readable than multiple OR conditions.
  • NOT IN excludes listed values.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review