MIN
MIN(expr)
Returns the smallest value of an expression in a group of rows – the lowest number, the oldest date or the first string in alphabetical order. NULL is skipped.
Parameters
- expr – a number, a date, a string or a column.
Examples
Examples with the customers, products and orders tables use the sample database from the SQL basics series.
SELECT MIN(price) AS cheapest, MIN(name) AS first_alphabetically FROM products;
+----------+----------------------+ | cheapest | first_alphabetically | +----------+----------------------+ | 2.40 | Cane sugar | +----------+----------------------+
The first order of each customer:
SELECT customer_id, MIN(order_date) AS first_order FROM orders GROUP BY customer_id ORDER BY customer_id;
+-------------+-------------+ | customer_id | first_order | +-------------+-------------+ | 1 | 2026-01-05 | | 2 | 2026-01-12 | | 3 | 2026-02-03 | | 4 | 2026-03-01 | +-------------+-------------+
The whole row with the lowest price in a category – MIN() in a subquery:
SELECT p.category, p.name, p.price FROM products p WHERE p.price = (SELECT MIN(price) FROM products WHERE category = p.category) ORDER BY p.category;
+-------------+----------------+-------+ | category | name | price | +-------------+----------------+-------+ | Accessories | Coffee grinder | 49.00 | | Drinks | Green tea | 4.90 | | Food | Cane sugar | 2.40 | | Tableware | White mug | 6.50 | +-------------+----------------+-------+
Things to watch out for
- SELECT category, name, MIN(price) … GROUP BY category does not return the name of the cheapest product – the name column is not in the group. MySQL 8 rejects such a query with ONLY_FULL_GROUP_BY, MariaDB returns the name of any row. Use a subquery as in the example, or window functions.
- Strings are compared according to the collation of the column (utf8mb4_unicode_ci in the sample database).