Need to find the highest, lowest, oldest, youngest, cheapest, most expensive? MIN and MAX have you covered.
MAX — The Highest Value
-- Highest score
SELECT MAX(score) AS highest_score FROM students;
-- Oldest student
SELECT MAX(age) AS oldest_age FROM students;
MIN — The Lowest Value
-- Lowest score
SELECT MIN(score) AS lowest_score FROM students;
-- Youngest student
SELECT MIN(age) AS youngest_age FROM students;
Getting the Row with MIN/MAX
A common question beginners ask is: "How do I get the full row of the student with the highest score?" MIN and MAX alone only give you the value, not the row. Here is one way to do it:
-- Get the student(s) with the highest score
SELECT * FROM students
WHERE score = (SELECT MAX(score) FROM students);
That is a subquery inside the WHERE clause. Do not worry if it looks confusing — we will cover subqueries properly later. For now, just know that this pattern exists.