Lesson 19 +10 XP

MIN and MAX

MIN and MAX

MIN() and MAX() return the smallest and largest value in a column.

MIN example

SELECT MIN(Price) AS SmallestPrice
FROM Products;

Returns the cheapest product price.

MAX example

SELECT MAX(Price) AS LargestPrice
FROM Products;

Returns the most expensive product price.

Works on text and dates too

  • On text: alphabetical order is used.
  • On dates: earliest / latest date.
SELECT MIN(OrderDate) FROM Orders;  -- earliest order

With WHERE

SELECT MAX(Price) FROM Products
WHERE CategoryID = 1;

The most expensive product in category 1.

Use an alias for clarity

Always give the result a readable name:

SELECT MIN(Price) AS Cheapest
FROM Products;

TL;DR

  • MIN returns the smallest value.
  • MAX returns the largest value.
  • Works on numbers, text, and dates.
  • Aliases make the result clearer.