SQL IN and BETWEEN
Chapter 18 of 40 · Estimated read time: 7 min
IN checks whether a value matches any item in a list. BETWEEN checks whether a value falls within a range. Both are shorthand for chained OR conditions — cleaner to write and easier to read.
The IN Operator
WHERE column IN (value1, value2, value3, ...)
-- Equivalent to:
WHERE column = value1 OR column = value2 OR column = value3
Example 1 — Products in Specific Categories
SELECT product_name, category, price
FROM products
WHERE category IN ('Electronics', 'Furniture')
ORDER BY category, price;| product_name | category | price |
|---|---|---|
| HDMI Cable | Electronics | 299 |
| Wireless Mouse | Electronics | 899 |
| USB-C Hub | Electronics | 1499 |
| Desk Lamp | Furniture | 1199 |
| Ergonomic Chair | Furniture | 8999 |
IN ('Electronics', 'Furniture') is identical to = 'Electronics' OR category = 'Furniture' — just shorter and easier to extend.Example 2 — Specific Customers by ID
SELECT customer_name, city
FROM customers
WHERE customer_id IN ('C01', 'C03', 'C05');| customer_name | city |
|---|---|
| Amit Verma | Noida |
| Rohan Mehta | Mumbai |
| Arjun Singh | Bengaluru |
NOT IN — Exclude a List
-- All products that are NOT Electronics
SELECT product_name, category
FROM products
WHERE category NOT IN ('Electronics');| product_name | category |
|---|---|
| Notebook A5 | Stationery |
| Ballpoint Pens | Stationery |
| Desk Lamp | Furniture |
| Ergonomic Chair | Furniture |
Watch out: NOT IN with NULLs. If the list contains a NULL value, NOT IN returns no rows at all — because
value != NULL is UNKNOWN in SQL. Always ensure your list has no NULLs when using NOT IN, or use NOT EXISTS instead.IN with a Subquery
-- Customers who have placed at least one order SELECT customer_name, city FROM customers WHERE customer_id IN ( SELECT DISTINCT customer_id FROM orders );
All 5 customers have orders in our dataset
| customer_name | city |
|---|---|
| Amit Verma | Noida |
| Priya Sharma | Delhi |
| Rohan Mehta | Mumbai |
| Sneha Gupta | Noida |
| Arjun Singh | Bengaluru |
The BETWEEN Operator
WHERE column BETWEEN low_value AND high_value
-- Equivalent to:
WHERE column >= low_value AND column <= high_value
BETWEEN is inclusive on both ends.
BETWEEN 500 AND 1500 includes rows where the value is exactly 500 or exactly 1500.Example 3 — Products in a Price Range
SELECT product_name, price FROM products WHERE price BETWEEN 500 AND 1500 ORDER BY price;
Includes 500 and 1500 (both boundaries inclusive)
| product_name | price |
|---|---|
| Wireless Mouse | 899 |
| Desk Lamp | 1199 |
| USB-C Hub | 1499 |
Example 4 — Orders in a Date Range
-- Orders placed between Oct 3 and Oct 6 (inclusive) SELECT order_id, customer_id, order_date, total_amount FROM orders WHERE order_date BETWEEN '2026-10-03' AND '2026-10-06' ORDER BY order_date;
| order_id | customer_id | order_date | total_amount |
|---|---|---|---|
| 1003 | C01 | 2026-10-03 | 495 |
| 1004 | C03 | 2026-10-04 | 8999 |
| 1005 | C04 | 2026-10-05 | 2398 |
| 1006 | C05 | 2026-10-06 | 299 |
NOT BETWEEN — Exclude a Range
-- Products priced outside the ₹100–₹1000 range SELECT product_name, price FROM products WHERE price NOT BETWEEN 100 AND 1000 ORDER BY price;
| product_name | price |
|---|---|
| Ballpoint Pens | 49 |
| Notebook A5 | 99 |
| Desk Lamp | 1199 |
| USB-C Hub | 1499 |
| Ergonomic Chair | 8999 |
✏️ Practice Exercises
1. Find all customers from Noida or Delhi using IN.
2. Find products priced between ₹200 and ₹900 (inclusive).
3. Find orders placed on October 1st, 2nd, or 8th using IN on order_date.
Show Answers
-- 1.
SELECT customer_name, city FROM customers
WHERE city IN ('Noida', 'Delhi');
-- 2.
SELECT product_name, price FROM products
WHERE price BETWEEN 200 AND 900
ORDER BY price;
-- 3.
SELECT order_id, order_date, total_amount FROM orders
WHERE order_date IN ('2026-10-01', '2026-10-02', '2026-10-08');