Function or operator · Grouping and calculating

MIN

MIN returns the smallest non-null value of an expression. To get the matching row you need a different query and a decision about ties.

MIN example

SQL
SELECT MIN(date) AS first_order
FROM orders;

Dates are stored in ISO format AAAA-MM-DD, which SQLite can compare chronologically as text. With other formats, convert the value or use a proper date type before looking for the minimum.

What to look for: If the dates are 2026-01-05, 2026-01-12 and 2026-02-01, MIN(date) returns 2026-01-05. It does not pick the other columns of that row for you.

Check the result with small data

Dates are written as YYYY-MM-DD.

Self-contained query · SQLite
WITH purchases(id, date) AS (
  VALUES (1, '2026-10-04'), (2, '2026-09-29'), (3, '2026-10-07')
)
SELECT MIN(date) AS first_date FROM purchases;
Query result
first_date
2026-09-29

In this sortable format, MIN finds the earliest date. It does not tell you which purchase has that date.

Check your understanding: What would you get with MAX(date)?

Show answer

2026-10-07, the most recent of the three dates.

What changes from case to case

One minimum row with a tiebreaker
SELECT id, name, price
FROM products
WHERE stock > 0
ORDER BY price ASC, id ASC
LIMIT 1;

The ordering returns one specific row and uses id as a tiebreaker. Another option is to compare price with a subquery that calculates MIN(price); that form returns every tied row.

Common mix-ups with MIN

Selecting columns that do not belong to the minimum

A query such as SELECT name, MIN(price) FROM products is not portable and can attach an arbitrary name to the minimum. Calculate the value first, or use a deterministic order to fetch the row.

A minimum does not by itself identify the winning row

Aggregation returns one value per group. If you want to know the lowest price in a category, MIN(price) answers that directly. If you also need the product with that price, there is a decision still to make: two products may be tied. Showing an ungrouped name next to the minimum is not a portable way to pick one.

To keep every winner, compare each product with the minimum of its category. To pick exactly one, define an ordering with a stable tiebreaker and take the first row using a strategy that suits your engine. Do not mix up those questions: a special-offers report may need all the cheapest products, while another process may need a single identifier.

The aggregate ignores null prices. A group with no known price produces a null minimum; an ungrouped query over an empty set can also return one row with a null minimum. That does not mean the lowest price is zero. If you aggregate text, the minimum depends on the engine's comparison rules and collation, so alphanumeric codes should not automatically be read as quantities.

Technical questions: PostgreSQL: aggregate functions (opens in a new tab).