SQL Min and Max

MIN() and MAX() return the smallest and largest values in a column. They are commonly used in reporting and analytics.

On this page

SQL MIN and MAX Functions

The MIN() and MAX() functions return the smallest and largest values in a column. They are commonly used in reporting and data analysis.

Basic Syntax

SELECT MIN(column_name)
FROM table_name;
SELECT MAX(column_name)
FROM table_name;

Example: Lowest and Highest Price

SELECT MIN(price) AS lowest_price,
       MAX(price) AS highest_price
FROM products;

Example: Earliest and Latest Date

MIN() and MAX() also work with date values.

SELECT MIN(order_date) AS first_order,
       MAX(order_date) AS latest_order
FROM orders;

Using MIN and MAX with WHERE

You can filter rows before calculating the result.

SELECT MAX(price) AS highest_electronics_price
FROM products
WHERE category = 'Electronics';

Using MIN and MAX with GROUP BY

To get the minimum or maximum value per group, combine with GROUP BY.

SELECT category,
       MIN(price) AS lowest_price,
       MAX(price) AS highest_price
FROM products
GROUP BY category;

Text Columns

When used on text columns, MIN() and MAX() return values based on alphabetical order (collation rules).

SELECT MIN(name) AS first_name_alphabetically,
       MAX(name) AS last_name_alphabetically
FROM customers;

NULL Behavior

MIN() and MAX() ignore NULL values. If all values are NULL, the result will be NULL.

Performance Note

Indexes on a column can significantly improve performance when using MIN() or MAX(), especially on large tables.

Next Step

Continue with SQL COUNT to learn how to count rows and non-NULL values.