← Back to SQL, page 2

sql-0025 · Easy fresher

Write a query for all orders placed in February 2026, using a range that stays correct if times are added to the dates later.

Use a half-open range: order_date >= '2026-02-01' AND order_date < '2026-03-01'. It includes every moment of February and nothing from March, whatever the time precision. Avoid wrapping the column in a function such as strftime, which blocks index use.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
SELECT id, customer_id, order_date
FROM orders
WHERE order_date >= '2026-02-01'
  AND order_date <  '2026-03-01'
ORDER BY order_date;
idcustomer_idorder_date
10212026-02-11
10322026-02-14

Two choices in that query are deliberate.

The upper bound is the first day of the next month with <, not the last day of February with <=. If the column later stores '2026-02-28 18:30', the query still includes it. It also avoids working out how many days the month has.

The column is compared directly. Writing strftime('%Y-%m', order_date) = '2026-02' returns the same rows here:

sql
SELECT id, customer_id, order_date
FROM orders
WHERE strftime('%Y-%m', order_date) = '2026-02'
ORDER BY order_date;
idcustomer_idorder_date
10212026-02-11
10322026-02-14

But the function has to be computed for every row, so an index on order_date cannot be used to jump to February. With a direct range, the index can seek to the start and stop at the end. A condition the index can use this way is called sargable (from "search argument able").

ISO dates as text (YYYY-MM-DD) sort correctly as strings, which is why the text comparison works on SQLite. On PostgreSQL, use a date or timestamp column and the same range works with real date values.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

SELECT id, customer_id, order_date
FROM orders
WHERE order_date >= '2026-02-01'
  AND order_date <  '2026-03-01'
ORDER BY order_date;

SELECT id, customer_id, order_date
FROM orders
WHERE strftime('%Y-%m', order_date) = '2026-02'
ORDER BY order_date;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Half-open range: >= first day AND < first day of next month
  • Correct for dates with or without times
  • Comparing the bare column keeps the condition index-friendly
  • ISO text dates sort correctly in SQLite

Common mistakes

  • Using <= '2026-02-28' and losing rows with times or on leap years
  • Wrapping the column in a date function and forcing a full scan

Follow-ups

  • How would you get the same range for any month passed as a parameter?
  • What does sargable mean?

Tips

  • Mention index use when you explain why you avoided the function