SQL MIN and MAX
MIN returns the smallest value and MAX the largest. They work on numbers, dates (earliest and latest) and text (alphabetical first and last).
Syntax
SELECT MIN(column), MAX(column) FROM table_name;
Try it yourself
Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.
min-max.sql · PostgreSQL
Loading editor…
Examples
1. Salary range
SELECT MIN(salary), MAX(salary) FROM employees;
Lowest and highest salary.
2. First and last order
SELECT MIN(order_date), MAX(order_date) FROM orders;
Works on dates.
Common mistakes
- Trying to get the name of the most expensive product with
SELECT name, MAX(price). Use ORDER BY ... LIMIT 1 or a window function.
Practice: test yourself
This challenge is from our interactive course. Write the query and press Check answer.
Challenge · 10 pts
Return the lowest and the highest employee salary, as two columns in one row.
challenge.sql · PostgreSQL
Loading editor…
FAQ
How do I find the row with the maximum value?
ORDER BY price DESC LIMIT 1, or a subquery: WHERE price = (SELECT MAX(price) FROM products).
Learn SQL step by step
65 hands-on exercises, streaks, a leaderboard and a certificate.