SQL

BETWEEN — Range Filtering

admin by @admin ADMIN
4d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`BETWEEN a AND b` is inclusive on both ends. Cleaner than `col >= a AND col <= b`. For dates, watch for end-of-day vs midnight boundaries.
SQL
Raw
-- Numeric range (inclusive)
SELECT * FROM orders WHERE amount BETWEEN 100 AND 500;
-- equivalent to: amount >= 100 AND amount <= 500

-- Date range — be explicit about times!
SELECT * FROM orders
WHERE  created_at BETWEEN '2025-01-01' AND '2025-01-31';
-- ⚠️ This MISSES anything on Jan 31 after 00:00:00.

-- Correct way for "the month of January"
SELECT * FROM orders
WHERE  created_at >= '2025-01-01'
   AND created_at <  '2025-02-01';   -- half-open interval is safer

-- NOT BETWEEN excludes the range (inclusive endpoints)
SELECT * FROM products WHERE price NOT BETWEEN 0 AND 9.99;
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.