SQL select only rows with max value on a column

Posted by Kyle Hankinson April 14, 2023


Short answer

The whole row (or rows) with the highest value in a column:

SELECT *
FROM sales
WHERE amount = (SELECT MAX(amount) FROM sales);

This returns every row that ties for the maximum. If you want exactly one row, sort and take the first:

SELECT * FROM sales ORDER BY amount DESC LIMIT 1;                 -- MySQL, PostgreSQL, SQLite
SELECT TOP 1 * FROM sales ORDER BY amount DESC;                   -- SQL Server
SELECT * FROM sales ORDER BY amount DESC FETCH FIRST 1 ROW ONLY;  -- Oracle 12c+, PostgreSQL, SQL Server 2012+ (with OFFSET 0 ROWS)

The row with the max value per group (for example each product's biggest sale) is the version most people need, and it is covered below.

Note that SELECT *, MAX(amount) FROM sales does not work: MAX() returns the maximum, but the other columns are not guaranteed to come from that row. PostgreSQL and SQL Server reject the query, and MySQL either rejects it (with ONLY_FULL_GROUP_BY, the default) or returns values from an arbitrary row.

The max row per group

Table sales has product_id, sale_date and amount. For each product, return the row with its largest amount.

Option 1: ROW_NUMBER() (MySQL 8+, PostgreSQL, SQL Server, Oracle, SQLite 3.25+)

SELECT product_id, sale_date, amount
FROM (
    SELECT s.*,
           ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY amount DESC) AS rn
    FROM sales s
) ranked
WHERE rn = 1;

ROW_NUMBER() gives exactly one row per product, choosing arbitrarily between ties. Use RANK() instead to keep every tied row, or add a tie-breaker to the ORDER BY (for example amount DESC, sale_date DESC).

Option 2: join on the grouped max (any version, including MySQL 5.7)

SELECT s.*
FROM sales s
JOIN (
    SELECT product_id, MAX(amount) AS max_amount
    FROM sales
    GROUP BY product_id
) m ON m.product_id = s.product_id
   AND m.max_amount = s.amount;

This keeps ties. An index on (product_id, amount) makes both the inner GROUP BY and the join fast (see how database indexes work).

Option 3: PostgreSQL DISTINCT ON

PostgreSQL has a shorter form for one row per group:

SELECT DISTINCT ON (product_id) *
FROM sales
ORDER BY product_id, amount DESC;

Which to use

Need Query
Every row tied for the overall max WHERE amount = (SELECT MAX(amount) FROM sales)
One row with the overall max ORDER BY amount DESC with LIMIT 1 / TOP 1 / FETCH FIRST 1 ROW ONLY
One row per group ROW_NUMBER() ... WHERE rn = 1, or DISTINCT ON in PostgreSQL
All tied rows per group RANK(), or the join on MAX() grouped by the key
Only the max value itself SELECT MAX(amount) FROM sales (add GROUP BY for per group)

For the min instead of the max, swap MAX for MIN and DESC for ASC.

NULLs

MAX() ignores NULLs. With ORDER BY amount DESC, PostgreSQL and Oracle sort NULLs first by default, so the "top" row can be a NULL. Add WHERE amount IS NOT NULL, or NULLS LAST in PostgreSQL and Oracle. MySQL, SQL Server and SQLite sort NULLs last in descending order.


About the author — Kyle Hankinson is the founder and sole developer of SQLPro Studio and the Hankinsoft Development suite of database tools. He has been building native macOS and iOS applications since 2010, and now ships SQLPro on Windows and Android as well.

Try SQLPro Studio — A powerful database manager for MySQL, PostgreSQL, Microsoft SQL Server, SQLite, Oracle, and Snowflake. Available on macOS, iOS, Android, and Windows.

Download Free Trial View Pricing Compare