← Back to SQL, page 16

sql-0315 · Easy fresher

What is MIN('2026-12-01', '2026-02-15') in SQLite, and is it the earlier date?

It is '2026-02-15', which is the earlier date, because ISO-8601 text sorts in date order. The scalar min() with two arguments compares the strings. With non-ISO formats such as '15/02/2026' the string order would no longer match date order.

Full answer
sql
SELECT min('2026-12-01', '2026-02-15') AS iso_min, min('01/12/2026', '15/02/2026') AS dmy_min;
iso_mindmy_min
2026-02-1501/12/2026

For ISO dates, string order equals date order, so min and max work. The day-first version returns '01/12/2026' (1 December), which is the later date, because '0' sorts before '1'.

SQLite's two-argument min() and max() are scalar functions; PostgreSQL uses LEAST and GREATEST for the same purpose. This is one more reason to store dates only in ISO form: every comparison, sort and min/max then behaves like a date operation without conversion.

sql
SELECT min('2026-12-01', '2026-02-15') AS iso_min, min('01/12/2026', '15/02/2026') AS dmy_min;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ISO date text sorts in date order
  • Scalar min(a, b) compares values in SQLite
  • Non-ISO date text breaks the ordering
  • PostgreSQL uses LEAST and GREATEST

Common mistakes

  • Comparing day-first date strings
  • Using MIN(a, b) syntax in PostgreSQL

Follow-ups

  • What do LEAST and GREATEST do with NULL?
  • How would you convert day-first dates?

Tips

  • Point out the format dependency