SQL Practice Logo

SQLPractice Online

Range & Interval Filtering: Functions

Module: Advanced Filtering

-- Date range with BETWEEN

SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

-- Date range with comparison (preferred)

SELECT * FROM orders WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';

-- Relative date with INTERVAL (PostgreSQL)

SELECT * FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '30 days';

-- Last N days

SELECT * FROM events WHERE event_date >= CURRENT_DATE - INTERVAL '7 days';

-- Numeric range

SELECT * FROM products WHERE price BETWEEN 10 AND 100;

-- Time range

SELECT * FROM logs WHERE created_at >= NOW() - INTERVAL '1 hour';

BETWEEN is inclusive (includes both endpoints)

INTERVAL syntax varies by database

CURRENT_DATE for date-only, NOW() for timestamp

Use >= and < for date ranges (not BETWEEN)

Numeric ranges work same as date ranges

Time zones matter for timestamp filtering

INTERVAL '30 days', NOW(), CURRENT_DATE

INTERVAL 30 DAY, NOW(), CURDATE()

DATEADD(day, -30, GETDATE())

Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.

WHERE

Filters rows before projection and sorting. It decides which rows continue through the query pipeline.

SELECT ... FROM table WHERE condition;

Most performance issues start with a weak WHERE clause or a missing supporting index.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

<, >, <=, >=

Range comparison operators for less-than, greater-than, and inclusive boundary checks.

salary >= 80000

AND

Requires every condition in the boolean expression to evaluate to TRUE.

condition_a AND condition_b

AND has higher precedence than OR.

BETWEEN

Checks whether a value falls inside an inclusive lower/upper range.

order_total BETWEEN 100 AND 500