SQL Practice Logo

SQLPractice Online

Range & Interval Filtering: Interview

Module: Advanced Filtering

Why use >= and < instead of BETWEEN for date ranges?

BETWEEN is inclusive and can include unwanted timestamps. For full day: use >= '2024-01-01' AND < '2024-01-02' to avoid missing midnight timestamps. Also better for index usage.

How do you filter for last 30 days dynamically?

Use INTERVAL: WHERE date_column >= CURRENT_DATE - INTERVAL '30 days'. This adjusts automatically each day without hardcoding dates.

Find orders from last 7 days

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

INTERVAL provides dynamic relative date filtering. Automatically adjusts each day.