Lesson 19 +10 XP

MIN and MAX

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

MIN example


sql

### SELECT MIN(Price) AS SmallestPrice

### FROM Products;

Returns the cheapest product price.

MAX example


sql

### 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.

sql
SELECT MIN(OrderDate) FROM Orders; - earliest order

With WHERE


sql

### 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:


sql

### 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.