HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL MIN() and MAX() Functions

MIN() and MAX() are aggregate functions — they work across a set of rows and return a single summary value. MIN() returns the smallest value in a column, and MAX() returns the largest. They work with numbers, dates, and even text.


Syntax

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

Sample Table

All examples on this page use the following products table:

idnamecategorypricestock
1LaptopElectronics55000.0012
2PhoneElectronics25000.0030
3T-ShirtClothing800.00100
4HeadphonesElectronics3500.0045
5JeansClothing2200.0060
6TabletElectronics35000.0018
7JacketClothing4500.0025

MIN() — Find the Lowest Value

Find the cheapest product price in the table:

SELECT MIN(price) AS lowest_price FROM products;
lowest_price
800.00

MAX() — Find the Highest Value

Find the most expensive product price:

SELECT MAX(price) AS highest_price FROM products;
highest_price
55000.00

Using Both in One Query

You can use MIN() and MAX() together to get the full price range in one query:

SELECT
    MIN(price) AS lowest_price,
    MAX(price) AS highest_price,
    MAX(price) - MIN(price) AS price_range
FROM products;
lowest_pricehighest_priceprice_range
800.0055000.0054200.00

MIN() and MAX() with WHERE

Narrow the calculation to a specific subset of rows using WHERE:

-- Cheapest Electronics product
SELECT MIN(price) AS min_electronics
FROM products
WHERE category = 'Electronics';
-- Returns: 3500.00

-- Most expensive Clothing item
SELECT MAX(price) AS max_clothing
FROM products
WHERE category = 'Clothing';
-- Returns: 4500.00

MIN() and MAX() on Dates

These functions work perfectly on date columns — MIN() returns the earliest date and MAX() returns the most recent:

-- First and last order dates
SELECT
    MIN(order_date) AS first_order,
    MAX(order_date) AS latest_order
FROM orders;

MIN() and MAX() on Text

On text columns, MIN() returns the value that comes first alphabetically and MAX() returns the last:

SELECT MIN(name) AS first_name, MAX(name) AS last_name
FROM products;
-- MIN returns: Headphones
-- MAX returns: Tablet

MIN() and MAX() with GROUP BY

To get the min and max per group — for example, per category — combine with GROUP BY:

SELECT category, MIN(price) AS cheapest, MAX(price) AS most_expensive
FROM products
GROUP BY category;
categorycheapestmost_expensive
Electronics3500.0055000.00
Clothing800.004500.00
Note: MIN() and MAX() both ignore NULL values — rows where the column is NULL are simply skipped in the calculation.

Key Points to Remember

  • MIN() returns the smallest value in a column; MAX() returns the largest.
  • Both functions work on numbers, dates, and text columns.
  • Use an alias (AS) to give the result a meaningful column name.
  • Add WHERE to limit the calculation to specific rows.
  • Combine with GROUP BY to get min/max values per group.
  • Both functions ignore NULL values automatically.
What's next? The next lesson covers COUNT — how to count the number of rows or non-null values in a column.