SQL IN Get Certified
Learn how to use the SQL IN operator to match a value against several possible values.
What is the IN Operator?
The IN operator is used with the WHERE clause to check whether a value matches any value in a list.
It is a convenient alternative to writing several OR conditions.
IN works with text, numbers, dates and subqueries. Every example below is self-contained SQLite, so each
Test My Code button runs on its own.
Basic IN Syntax
The column name goes before IN, followed by a list of possible values inside parentheses.
What this code does: Keeps the products whose name is either Laptop or Monitor. Tablet and Keyboard are not in the list, so they are skipped. The query returns 2 rows: Laptop and Monitor.
WITH products(name) AS (
VALUES
('Laptop'),
('Tablet'),
('Monitor'),
('Keyboard')
)
SELECT name
FROM products
WHERE name IN ('Laptop', 'Monitor');
IN with Text Values
Use IN to find rows where a text column matches one of several values.
What this code does: Returns the employees who work in IT or Finance. Text values go inside quotes. The query returns 3 rows: Meera (IT), Rohan (Finance) and Vikram (IT).
WITH employees(name, department) AS (
VALUES
('Aarav', 'Sales'),
('Meera', 'IT'),
('Rohan', 'Finance'),
('Anika', 'HR'),
('Vikram', 'IT')
)
SELECT name, department
FROM employees
WHERE department IN ('IT', 'Finance');
IN with Numbers
IN also compares numeric values. Numbers do not need quotation marks.
What this code does: Returns the products priced at exactly 500 or 900. Both the Keyboard and the Webcam cost 900, so both are included. The query returns 3 rows: Mouse (500), Keyboard (900) and Webcam (900).
WITH products(name, price) AS (
VALUES
('Mouse', 500),
('Keyboard', 900),
('Monitor', 1500),
('Webcam', 900)
)
SELECT name, price
FROM products
WHERE price IN (500, 900);
IN vs OR
IN makes a query shorter and easier to read when you check one column against several values.
What this code does: Returns the cities named Delhi, Mumbai or Pune with a single, compact condition. The query returns 3 rows: Delhi, Mumbai and Pune.
WITH cities(name) AS (
VALUES
('Delhi'),
('Mumbai'),
('Chennai'),
('Kolkata'),
('Pune')
)
SELECT name
FROM cities
WHERE name IN ('Delhi', 'Mumbai', 'Pune');
The Same Condition Using OR
The query above can also be written with OR. It needs the column name repeated for every value.
What this code does: Gives exactly the same result as the IN version, but with a longer condition. Each extra value would need another OR name = ... line. The query returns 3 rows: Delhi, Mumbai and Pune.
WITH cities(name) AS (
VALUES
('Delhi'),
('Mumbai'),
('Chennai'),
('Kolkata'),
('Pune')
)
SELECT name
FROM cities
WHERE name = 'Delhi'
OR name = 'Mumbai'
OR name = 'Pune';
NOT IN
NOT IN returns the rows whose value does not match any value in the list.
What this code does: Removes the Furniture and Stationery products and keeps everything else. The query returns 2 rows: Laptop (Electronics) and Camera (Electronics).
WITH products(name, category) AS (
VALUES
('Laptop', 'Electronics'),
('Desk', 'Furniture'),
('Chair', 'Furniture'),
('Camera', 'Electronics'),
('Notebook', 'Stationery')
)
SELECT name, category
FROM products
WHERE category NOT IN ('Furniture', 'Stationery');
IN with AND
Combine IN with other conditions using AND. Both must be true.
What this code does: A row must be in the IT or Finance department and earn more than 70000. Meera is in Finance but earns 68000, so she is left out. The query returns 2 rows: Aarav (IT, 75000) and Vikram (IT, 82000).
WITH employees(name, department, salary) AS (
VALUES
('Aarav', 'IT', 75000),
('Meera', 'Finance', 68000),
('Rohan', 'Sales', 55000),
('Anika', 'HR', 62000),
('Vikram', 'IT', 82000)
)
SELECT name, department, salary
FROM employees
WHERE department IN ('IT', 'Finance')
AND salary > 70000;
IN with OR
IN can also be combined with other conditions using OR. A row needs to satisfy only one of them.
What this code does: Returns every Electronics product, and also any product priced below 150, even if it is not Electronics. That is why the 120 Chair appears. The query returns 3 rows: Laptop (Electronics, 800), Chair (Furniture, 120) and Camera (Electronics, 600).
WITH products(name, category, price) AS (
VALUES
('Laptop', 'Electronics', 800),
('Chair', 'Furniture', 120),
('Camera', 'Electronics', 600),
('Desk', 'Furniture', 300)
)
SELECT name, category, price
FROM products
WHERE category IN ('Electronics')
OR price < 150;
IN with ORDER BY
Use ORDER BY to sort the rows that pass an IN condition.
What this code does: Keeps the Electronics and Furniture products, which here is every row, and sorts them from the highest price to the lowest. The query returns 5 rows: Laptop (Electronics, 800), Phone (Electronics, 700), Camera (Electronics, 600), Desk (Furniture, 300) and Chair (Furniture, 120).
WITH products(name, category, price) AS (
VALUES
('Laptop', 'Electronics', 800),
('Chair', 'Furniture', 120),
('Camera', 'Electronics', 600),
('Desk', 'Furniture', 300),
('Phone', 'Electronics', 700)
)
SELECT name, category, price
FROM products
WHERE category IN ('Electronics', 'Furniture')
ORDER BY price DESC;
IN with LIMIT
SQLite lets you combine IN with LIMIT to return only a set number of matching rows.
What this code does: Filters the students with grade A or B (four of them), sorts them by name, and keeps the first three. Rahul is cut off by LIMIT. The query returns 3 rows: Aman (A), Karan (A) and Neha (B).
WITH students(name, grade) AS (
VALUES
('Aman', 'A'),
('Neha', 'B'),
('Rahul', 'A'),
('Priya', 'C'),
('Karan', 'A')
)
SELECT name, grade
FROM students
WHERE grade IN ('A', 'B')
ORDER BY name
LIMIT 3;
IN with a Subquery
IN can compare a value with the list of results returned by a subquery.
What this code does: The inner query first finds the IDs of the IT and Sales departments (1 and 3). The outer query then returns the employees whose department_id is in that list. Meera works in Finance (2), so she is skipped. The query returns 3 rows: Aarav, Rohan and Anika.
WITH employees(name, department_id) AS (
VALUES
('Aarav', 1),
('Meera', 2),
('Rohan', 3),
('Anika', 1)
),
departments(id, department) AS (
VALUES
(1, 'IT'),
(2, 'Finance'),
(3, 'Sales')
)
SELECT name
FROM employees
WHERE department_id IN (
SELECT id
FROM departments
WHERE department IN ('IT', 'Sales')
);
IN with Dates
SQLite commonly stores dates as ISO text (YYYY-MM-DD), so you can use IN to match specific dates.
What this code does: Returns the events that fall on 22 or 24 August 2026. The workshop and the meeting are on other dates. The query returns 2 rows: Seminar (2026-08-22) and Conference (2026-08-24).
WITH events(event_name, event_date) AS (
VALUES
('Workshop', '2026-08-20'),
('Seminar', '2026-08-22'),
('Conference', '2026-08-24'),
('Meeting', '2026-08-25')
)
SELECT event_name, event_date
FROM events
WHERE event_date IN ('2026-08-22', '2026-08-24');
IN with Calculated Values
The expression tested by IN does not have to be a plain column. SQLite can compare calculated values too.
What this code does: Doubles each price and checks whether the result is 600, 1000 or 1400. That matches Mouse (300), Keyboard (500) and Webcam (700), while Monitor doubles to 3000. The query returns 3 rows: Keyboard (500), Mouse (300) and Webcam (700).
WITH products(name, price) AS (
VALUES
('Keyboard', 500),
('Mouse', 300),
('Monitor', 1500),
('Webcam', 700)
)
SELECT name, price
FROM products
WHERE price * 2 IN (600, 1000, 1400);
IN and NULL Values
SQL uses three-valued logic, so comparisons with NULL do not behave like ordinary comparisons. To find NULL values, use IS NULL instead of IN (NULL).
What this code does: Returns the employees whose department is missing. IS NULL is the correct way to test for NULL, because department IN (NULL) would match nothing. The query returns 1 row: Meera (NULL).
WITH employees(name, department) AS (
VALUES
('Aarav', 'IT'),
('Meera', NULL),
('Rohan', 'Sales')
)
SELECT name, department
FROM employees
WHERE department IS NULL;
The NOT IN and NULL Trap
If the list given to NOT IN contains a NULL, the condition can never be true. This is a common source of surprising empty results.
What this code does: You might expect Rohan to be returned, since Sales is not IT. But the list also contains NULL, and “Sales is not NULL” is unknown, not true, so the row is rejected. The query returns no rows.
WITH employees(name, department) AS (
VALUES
('Aarav', 'IT'),
('Meera', NULL),
('Rohan', 'Sales')
)
SELECT name, department
FROM employees
WHERE department NOT IN ('IT', NULL);
Common Uses of IN
department IN ('IT', 'HR')price IN (100, 200, 500)category NOT IN ('Used')id IN (SELECT id ...)Advantages of IN
- Makes multiple-value comparisons easier to read.
- Reduces the need for repeated
ORconditions. - Works with both text and numeric values.
- Can be combined with
AND,OR,ORDER BYandLIMIT. - Can be used with subqueries.
Best Practices
- Use
INwhen checking one value against several possible values. - Use
NOT INwhen you need to exclude a list of values. - Use
IS NULLwhen checking for NULL values. - Make sure a list or subquery used with
NOT INcannot return NULL. - Use a subquery with
INwhen the list of values comes from another query. - Keep the list of values short and easy to read.
The IN operator compares a value against several possible values. It keeps queries cleaner than repeated OR conditions and also works with subqueries.
Click Test My Code on any example above to run it live, or
Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.