How To Use In In Sql in SQL
How to Use IN in SQL
The IN operator allows you to specify multiple values in a WHERE clause. It checks if a value matches any value in a list or subquery.
Syntax:
SELECT columnsFROM table_nameWHERE column_name IN (value1, value2, value3, ...);Or with a subquery:
SELECT columnsFROM table_nameWHERE column_name IN ( SELECT column_name FROM other_table WHERE condition);Example 1: Using IN with a list of values
Find employees who work in either 'Sales', 'Marketing', or 'IT' departments:
SELECT *FROM employeesWHERE department IN ('Sales', 'Marketing', 'IT');Example 2: Using IN with a subquery
Find employees who belong to departments that have more than 10 employees:
SELECT *FROM employeesWHERE department_id IN ( SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) > 10);Notes:
INis equivalent to multipleORconditions (e.g.,WHERE dept = 'Sales' OR dept = 'Marketing').You can use
NOT INto exclude values.INworks with numbers, strings, and dates.
If you want, I can help you write IN queries for your specific data!