CodeBlanks Logo Icon CodeBlanks

MIN and MAX

Get the smallest and largest values.

MIN and MAX

MIN returns the smallest, MAX the largest value. Works on numbers, text and dates.

SELECT MIN(price), MAX(price) FROM products;

This query gives the lowest and highest price.

MIN(price), MAX(price) is selected from products.

MIN and MAX — details

MIN and MAX skip NULLs. For text they follow alphabetical order.

For example, MIN(created_at) gives the date of the first record and MAX(created_at) the last; MIN(name) returns the name that comes first alphabetically.

Practice MIN and MAX

Find the lowest price in products with MIN(price).

Starter code

query.sql
-- lowest price
SELECT

Sample solution

query.sql
SELECT MIN(price) FROM products;

Expected output

min
5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does MIN return?

  • The largest value
  • The smallest value Correct answer
  • The average
  • The total

Explanation: MIN returns the smallest value.

2 Add MIN

Question
SELECT ____(price) FROM products;
Answer
SELECT MIN(price) FROM products;

Explanation: MIN(price) returns the lowest price.

3 MIN and MAX: match the terms

Term Meaning
MIN smallest
MAX largest
NULL skip
text alphabet

All lessons in Intermediate