IN, BETWEEN, EXISTS: Examples
Module: Advanced Filtering
IN with Literal Values
basic
Find orders with specific statuses
SELECT order_id, customer_id, status, total
FROM orders
WHERE status IN ('pending', 'processing', 'shipped')
ORDER BY order_id;
order_id | customer_id | status | total
101 | 1 | pending | 150.00
102 | 2 | shipped | 200.00
103 | 1 | processing | 75.00
IN checks if status matches any value in the list. Cleaner than multiple OR conditions.
All
BETWEEN for Range Filtering
basic
Find products in price range $10-$50
SELECT product_id, product_name, price
FROM products
WHERE price BETWEEN 10 AND 50
ORDER BY price;
product_id | product_name | price
1 | Widget A | 15.00
2 | Widget B | 25.00
3 | Widget C | 45.00
BETWEEN is inclusive (includes 10 and 50). Cleaner than >= AND <= syntax.
All
EXISTS for Related Data
intermediate
Find customers who have placed orders
SELECT c.customer_id, c.name, c.email
FROM customers c
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)
ORDER BY c.name;
customer_id | name | email
1 | Alice | alice@email.com
2 | Bob | bob@email.com
EXISTS returns true if subquery finds any matching orders. More efficient than IN for large datasets.
All