The WHERE with IN list helps you find rows where a column matches any value from a list. It makes checking many values easy and fast.
WHERE with IN list in SQL
SELECT column1, column2, ... FROM table_name WHERE column_name IN (value1, value2, value3, ...);
The list inside IN is a set of values to match.
Values can be numbers or strings (strings need quotes).
SELECT * FROM employees WHERE department_id IN (1, 2, 3);
SELECT name FROM products WHERE category IN ('Books', 'Toys');
SELECT * FROM orders WHERE status IN ('Pending');
SELECT * FROM customers WHERE city IN ();
This creates a simple employees table, adds four employees, then selects those in departments 1, 3, or 5.
CREATE TABLE employees ( id INT, name VARCHAR(50), department_id INT ); INSERT INTO employees (id, name, department_id) VALUES (1, 'Alice', 1), (2, 'Bob', 2), (3, 'Charlie', 3), (4, 'Diana', 4); -- Find employees in departments 1, 3, or 5 SELECT id, name, department_id FROM employees WHERE department_id IN (1, 3, 5);
The IN list makes queries shorter and easier than many OR conditions.
Using a large list in IN can slow down the query; consider joins or temporary tables for big lists.
Remember that IN checks for exact matches; it does not work like LIKE or partial matches.
WHERE with IN list filters rows by matching a column to any value in a list.
It is easier and cleaner than writing many OR conditions.
Use it when you have a fixed set of values to check against.
Practice
WHERE column_name IN (value1, value2, value3) do?Solution
Step 1: Understand the IN clause purpose
TheINclause checks if a column's value matches any value inside the parentheses list.Step 2: Identify the effect on row selection
Rows with the column value equal to any listed value are selected; others are ignored.Final Answer:
It selects rows wherecolumn_namematches any value in the list. -> Option AQuick Check:
WHERE with IN filters rows by listed values [OK]
- Thinking IN updates or deletes rows
- Confusing IN with creating tables
- Assuming IN matches all values at once
status is either 'active', 'pending', or 'closed'?Solution
Step 1: Check correct use of IN syntax
The correct syntax iscolumn_name IN (value1, value2, ...)without an equals sign.Step 2: Identify correct punctuation and parentheses
Values must be inside parentheses and separated by commas.Final Answer:
SELECT * FROM orders WHERE status IN ('active', 'pending', 'closed'); -> Option BQuick Check:
IN syntax = column IN (values) [OK]
- Using '=' before IN
- Missing commas between values
- Omitting parentheses around values
employees with column department, what rows will this query return?SELECT * FROM employees WHERE department IN ('HR', 'Sales');Solution
Step 1: Understand the IN filter on department
The query filters rows wheredepartmentmatches any value in ('HR', 'Sales').Step 2: Determine which rows match
Only rows with department 'HR' or 'Sales' are returned; others are excluded.Final Answer:
Rows where department is 'HR' or 'Sales'. -> Option AQuick Check:
IN filters rows matching listed values [OK]
- Thinking it returns rows not in the list
- Assuming it returns only one value
- Confusing IN with NOT IN
SELECT * FROM products WHERE category IN 'Electronics', 'Toys', 'Books';
Solution
Step 1: Check syntax of IN clause
The list of values after IN must be enclosed in parentheses.Step 2: Identify missing parentheses
The query has values listed without parentheses, causing syntax error.Final Answer:
Missing parentheses around the list of values. -> Option CQuick Check:
IN requires parentheses around values [OK]
- Omitting parentheses around values
- Using IN with equals sign
- Forgetting commas between values
orders table whose country is either 'USA', 'Canada', or 'Mexico', but exclude those with status 'cancelled'. Which query correctly uses WHERE with IN and excludes cancelled orders?Solution
Step 1: Use IN correctly for country filter
Thecountrycolumn must be checked withIN ('USA', 'Canada', 'Mexico')without equals sign.Step 2: Exclude cancelled orders with AND condition
UseAND status != 'cancelled'to exclude those rows.Final Answer:
SELECT * FROM orders WHERE country IN ('USA', 'Canada', 'Mexico') AND status != 'cancelled'; -> Option DQuick Check:
IN filters list, AND excludes cancelled [OK]
- Using OR instead of AND causing wrong results
- Adding '=' before IN
- Omitting parentheses around IN values
Ready to prove it?
Pass the test and this pattern moves unknown
