PostgreSQL MIN and MAX
min() and max() are aggregate functions: they look at many rows and return one value, the smallest or largest. They work on numbers, text (alphabetically), dates and most other sortable types.
The cheapest and the most expensive book
SELECT min(price) AS cheapest, max(price) AS most_expensive
FROM books; cheapest | most_expensive
----------+----------------
7.50 | 44.00
(1 row)
Dates and text
On dates, min is the earliest and max the latest. On text, they follow the sort order:
SELECT min(order_date) AS first_order,
max(order_date) AS last_order,
max(order_date) - min(order_date) AS days_between,
min(last_name) AS first_alphabetically,
max(last_name) AS last_alphabetically
FROM orders, customers
WHERE customers.id = orders.customer_id; first_order | last_order | days_between | first_alphabetically | last_alphabetically
-------------+------------+--------------+----------------------+---------------------
2024-01-05 | 2024-12-20 | 350 | Brown | Wright
(1 row)
Subtracting two dates gives the number of days between them.
With a WHERE clause
The WHERE clause is applied first, and the aggregate only sees the remaining rows:
SELECT min(price) AS cheapest_programming_book
FROM books
WHERE category_id = 4;
SELECT max(salary) AS top_bookseller_salary
FROM employees
WHERE job_title = 'Bookseller'; cheapest_programming_book
---------------------------
35.00
(1 row)
top_bookseller_salary
-----------------------
3100.00
(1 row)
Per group
Combined with GROUP BY (covered in its own lesson), you get one minimum and maximum per group:
SELECT category_id, min(price) AS lowest, max(price) AS highest, count(*) AS books
FROM books
GROUP BY category_id
ORDER BY category_id; category_id | lowest | highest | books
-------------+--------+---------+-------
1 | 12.50 | 17.80 | 5
2 | 16.75 | 21.00 | 3
3 | 11.99 | 13.49 | 3
4 | 35.00 | 44.00 | 4
5 | 19.50 | 27.00 | 3
6 | 7.50 | 8.99 | 2
(6 rows)
Finding the row, not just the value
A common beginner mistake is to expect max() to return the row. This query is an error, because PostgreSQL doesn't know which title you mean:
SELECT title, max(price) FROM books;ERROR: column "books.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT title, max(price) FROM books;
^
To get the whole row, compare with a subquery or simply sort and take the first row:
SELECT title, price FROM books WHERE price = (SELECT max(price) FROM books);
SELECT title, price FROM books ORDER BY price DESC LIMIT 1; title | price
----------------------+-------
Postgres in Practice | 44.00
(1 row)
title | price
----------------------+-------
Postgres in Practice | 44.00
(1 row)
The first form returns all books that share the top price; the second returns exactly one.
NULLs and empty sets
Aggregates ignore NULLs. If there are no rows at all (or only NULLs), min and max return NULL:
SELECT min(birth_year) AS oldest_birth_year, max(birth_year) AS youngest_birth_year FROM authors;
SELECT max(price) AS no_such_books FROM books WHERE category_id = 7; oldest_birth_year | youngest_birth_year
-------------------+---------------------
1959 | 1990
(1 row)
no_such_books
---------------
(1 row)
Tip:
greatest(a, b, c)andleast(a, b, c)are the row-wise cousins ofmaxandmin: they compare several values in the same row instead of one column across rows.
Test yourself
What does SELECT max(order_date) FROM orders; return?
- The oldest order date
- The most recent order date
- The number of orders
- Every date
Why does SELECT title, max(price) FROM books; fail?
- max() doesn't work on numeric
- title must appear in GROUP BY or an aggregate
- titles are text
- You need DISTINCT
What does min(price) return when no rows match the WHERE clause?
- 0
- An error
- NULL
- The minimum of the whole table