SQL Practice Logo

SQLPractice Online

Range & Interval Filtering: Examples

Module: Advanced Filtering

Date Range with BETWEEN

basic

Find orders from 2024

SELECT order_id, customer_id, order_date, total

FROM orders

WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'

ORDER BY order_date;

order_id | customer_id | order_date | total

101 | 1 | 2024-01-15 | 150.00

102 | 2 | 2024-03-20 | 200.00

103 | 1 | 2024-06-10 | 75.00

BETWEEN is inclusive - includes both 2024-01-01 and 2024-12-31. Simple syntax for date ranges.

All

Last 30 Days with INTERVAL

intermediate

Find orders from last 30 days

SELECT order_id, customer_id, order_date, total

FROM orders

WHERE order_date >= CURRENT_DATE - INTERVAL '30 days'

ORDER BY order_date DESC;

order_id | customer_id | order_date | total

105 | 3 | 2024-01-25 | 300.00

104 | 2 | 2024-01-20 | 125.00

103 | 1 | 2024-01-15 | 75.00

INTERVAL calculates relative dates. Dynamic filter adjusts automatically each day.

PostgreSQL

Price Range Filtering

basic

Find products between $10 and $100

SELECT product_id, product_name, price

FROM products

WHERE price BETWEEN 10 AND 100

ORDER BY price;

product_id | product_name | price

1 | Mouse | 25.00

2 | Keyboard | 75.00

3 | Webcam | 95.00

BETWEEN works for numeric ranges. Includes both 10 and 100 (inclusive).

All

Time Window Filtering