The PostgreSQL GREATEST function returns the largest value from a list of expressions, and LEAST returns the smallest. GREATEST(12, 45, 7) returns 45.
The catch: PostgreSQL skips NULL arguments. MySQL, MariaDB and Oracle return NULL instead.
Six examples follow, from columns in one row to clamping and an UPDATE. Tested on PostgreSQL 18.6 for Windows, with psql in VS Code.
PostgreSQL GREATEST and LEAST Function Syntax
Here is the syntax:
GREATEST(value [, ...])
LEAST(value [, ...])
| Argument | Required | What it means |
|---|---|---|
| value | One or more | Any expression. All of them must convert to one common type. |
Return value: the common type of the arguments. NULLs are ignored; you get NULL only when every argument is NULL.
Availability: every supported PostgreSQL version. The NULL rule deviates from the SQL standard.
When to Use the GREATEST Function in PostgreSQL
Use GREATEST or LEAST when you need to:
- pick the best quarter from four sales columns
- find the latest of several dates
- keep a value between a floor and a ceiling
Across rows, use the PostgreSQL MIN aggregate. Run this once:
DROP TABLE IF EXISTS reps, accounts;
CREATE TABLE reps (
rep_id int PRIMARY KEY,
rep text,
q1 numeric(10,2), q2 numeric(10,2),
q3 numeric(10,2), q4 numeric(10,2)
);
INSERT INTO reps VALUES
(1, 'Hannah Brooks', 48200, 51750, 46900, 55300),
(2, 'Tyler Reed', 61400, 58900, NULL, 63250),
(3, 'Grace Miller', 39800, 42100, 44600, 41950),
(4, 'Marcus Bennett', NULL, NULL, NULL, NULL);
CREATE TABLE accounts (
account_id int PRIMARY KEY,
customer text,
last_order date,
last_login date,
last_ticket date,
last_seen date
);
INSERT INTO accounts VALUES
(101, 'Olivia Carter', '2026-03-14', '2026-05-02', '2026-04-20', NULL),
(103, 'Chloe Ramirez', '2026-01-27', '2026-01-30', '2026-07-15', NULL),
(104, 'Noah Sullivan', NULL, '2026-04-05', NULL, NULL);
Example 1: Find the Largest Value Across Columns with GREATEST
Let’s start with the reps table. I want each rep’s best and worst quarter:
SELECT rep,
GREATEST(q1, q2, q3, q4) AS best_quarter,
LEAST(q1, q2, q3, q4) AS worst_quarter
FROM reps
WHERE rep_id IN (1, 3);
Output:
rep | best_quarter | worst_quarter
---------------+--------------+---------------
Hannah Brooks | 55300.00 | 46900.00
Grace Miller | 44600.00 | 39800.00
(2 rows)

For Hannah Brooks, this returns 55300.00 and 46900.00. Each row stands alone, so there’s no GROUP BY.
Example 2: How Does PostgreSQL GREATEST Handle NULL?
Tyler Reed has a NULL Q3; Marcus Bennett has no numbers. Say you need both:
SELECT rep,
GREATEST(q1, q2, q3, q4) AS best_quarter,
COALESCE(GREATEST(q1, q2, q3, q4), 0) AS best_or_zero
FROM reps
WHERE rep_id IN (2, 4);
Output:
rep | best_quarter | best_or_zero
----------------+--------------+--------------
Tyler Reed | 63250.00 | 63250.00
Marcus Bennett | | 0
(2 rows)

Tyler gets 63250.00 because the NULL Q3 is skipped. Marcus gets NULL, which COALESCE turns into 0.
Example 3: Get the Latest Date with GREATEST
Now the accounts table. I want each customer’s latest activity date, newest first:
SELECT customer,
GREATEST(last_order, last_login, last_ticket) AS last_activity
FROM accounts
ORDER BY last_activity DESC;
Output:
customer | last_activity
---------------+---------------
Chloe Ramirez | 2026-07-15
Olivia Carter | 2026-05-02
Noah Sullivan | 2026-04-05
(3 rows)

Chloe Ramirez comes back first with 07/15/2026, from her support ticket. I have a separate guide on comparing dates in PostgreSQL.
Example 4: Fix the GREATEST Types Cannot Be Matched Error
Next, the type rule. I want to mix a date and an integer:
\set ON_ERROR_STOP off
SELECT GREATEST(last_order, 30) FROM accounts;
SELECT GREATEST(last_order, DATE '2026-04-01') AS fixed
FROM accounts WHERE account_id = 101;
Output:
psql:greatest-type-errors.sql:3: ERROR: GREATEST types date and integer cannot be matched
LINE 1: SELECT GREATEST(last_order, 30) FROM accounts;
^
fixed
------------
2026-04-01
(1 row)

Compare a date with a date and it returns 2026-04-01. Check PostgreSQL data types when a cast isn’t obvious.
Example 5: Clamp a Value Between a Minimum and Maximum
The goal is a 5% Q4 bonus kept between $2,200 and $3,000:
SELECT rep,
ROUND(GREATEST(LEAST(q4 * 0.05, 3000), 2200), 2) AS bonus
FROM reps
WHERE rep_id < 4;
Output:
rep | bonus
---------------+---------
Hannah Brooks | 2765.00
Tyler Reed | 3000.00
Grace Miller | 2200.00
(3 rows)
Hannah Brooks gets $2,765.00, inside the range. Tyler is capped at $3,000 and Grace is raised to $2,200. ROUND keeps cents.
Example 6: Use GREATEST in an UPDATE Statement
Last, you want to store the latest date in last_seen:
UPDATE accounts
SET last_seen = GREATEST(last_order, last_login, last_ticket);
SELECT customer, last_seen FROM accounts
WHERE account_id = 104;
Output:
customer | last_seen
---------------+------------
Noah Sullivan | 2026-04-05
(1 row)
Noah Sullivan gets 2026-04-05, his only date. Run the SELECT again and you should see no NULLs. More in the PostgreSQL UPDATE guide.
GREATEST vs MAX in PostgreSQL: Which Should You Use?
| Feature | GREATEST / LEAST | MAX / MIN |
|---|---|---|
| Compares | Values in one row | One column across rows |
MAX(q1) returned 61400.00; MAX(GREATEST(q1, q2, q3, q4)) returned 63250.00. My rule of thumb: GREATEST across columns, MAX down a column.
Tips & Common Mistakes
- Mixed types stop with
GREATEST types date and integer cannot be matched. - MariaDB 13 returned NULL for
GREATEST(1, NULL, 3); PostgreSQL returns 3. GREATEST(1, 2.5)returned 2.5 as numeric.- Text follows collation:
GREATEST('apple', 'Banana')returned Banana under en-US, apple under COLLATE “C”.
Wrapping Up
In PostgreSQL, GREATEST(col1, col2, ...) is the call you’ll use most. Copy Example 3 for dates and Example 5 for clamping.
Next step: browse the complete list of PostgreSQL functions or read the PostgreSQL 18 GREATEST and LEAST docs.
Keep going with these PostgreSQL posts:
Frequently Asked Questions
How do I make GREATEST return NULL when any argument is NULL?
Add a check: CASE WHEN q1 IS NULL OR q2 IS NULL THEN NULL ELSE GREATEST(q1, q2) END. That copies the MySQL and Oracle behavior.
How do I get the max of two values in PostgreSQL?
Use GREATEST(a, b). CASE WHEN a >= b THEN a ELSE b END also works, but returns b when a is NULL.
How do I find which column holds the greatest value?
GREATEST returns only the value. Match it back: CASE GREATEST(q1, q2) WHEN q1 THEN ‘q1’ ELSE ‘q2’ END.

Kumar Saurabh is a freelance software engineer who builds web and mobile apps with Next.js, React, Flutter and Python. From 2021 to 2024, Saurabh worked as an IT analyst and developer at TSInfo Technologies, writing technical articles on SQL and PostgreSQL. Saurabh writes the PostgreSQL, MySQL, MariaDB and MongoDB tutorials on DatabaseFAQs.com and holds a Master of Computer Applications (MCA) from Chandigarh University.