IN and BETWEEN Operators – SQL Tutorial for Beginners
SQL IN and BETWEEN operators tutorial with examples
The IN and BETWEEN operators in SQL help you filter data efficiently based on multiple values or a specific range. These operators make your queries cleaner, faster, and easier to understand.
In this SQL tutorial, you will learn:
What IN operator is
What BETWEEN operator is
Syntax and usage
Real-world examples
Best practices
The IN operator allows you to filter data by matching a value against multiple possible values.
Instead of writing:
WHERE city = 'Delhi' OR city = 'Mumbai' OR city = 'Pune'
You can simply use:
WHERE city IN ('Delhi', 'Mumbai', 'Pune')
SELECT column1, column2
FROM table_name
WHERE column_name IN (value1, value2, value3);
SELECT * FROM employees
WHERE city IN ('Delhi', 'Mumbai', 'Bengaluru');
SELECT * FROM products
WHERE category IN ('Electronics', 'Mobile', 'Laptop');
SELECT * FROM users
WHERE id NOT IN (1, 3, 7);
The BETWEEN operator is used to filter data within a range of values.
Numbers
Dates
Text (alphabetical range)
SELECT column1, column2
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT * FROM employees
WHERE salary BETWEEN 30000 AND 60000;
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';
SELECT * FROM students
WHERE name BETWEEN 'A' AND 'M';
SELECT * FROM sales
WHERE amount NOT BETWEEN 500 AND 1000;
You can use both operators together.
SELECT * FROM products
WHERE category IN ('Mobile', 'Laptop')
AND price BETWEEN 20000 AND 50000;
SELECT * FROM users
WHERE age BETWEEN 18 AND 30;
SELECT * FROM orders
WHERE status IN ('Pending', 'Shipped');
SELECT * FROM sales
WHERE sale_date BETWEEN '2024-04-01' AND '2024-06-30';
✔ Use IN for matching multiple fixed values
✔ Use BETWEEN for numeric/date ranges
✔ Always verify date formats (YYYY-MM-DD)
✔ Use parentheses when combining with AND/OR
✔ Avoid NOT IN with NULL values (use NOT EXISTS instead)
In this tutorial, you learned:
How to use the IN operator to match multiple values
How to use the BETWEEN operator to filter ranges
Real-world examples for both operators
Best practices for writing clean SQL queries
IN and BETWEEN make SQL filtering simpler, faster, and more readable.