Return the largest or smallest value from a list of two or more expressions. Evaluate multiple columns or values in a single call without nested CASE expressions.
Return the greatest or least value from a list of arguments. Useful for calculating caps and floors in application logic. In some databases (MySQL), NULL arguments cause the whole result to be NULL -- verify behavior or wrap in COALESCE.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | 3.23 | GREATEST(v1, v2, ...) and LEAST(v1, v2, ...) support any number of arguments. If any argument is NULL, the result is NULL. Arguments are compared after implicit type conversion — mixing types triggers coercion rules. Works with numbers, strings, and dates. |
| PostgreSQL | ✓ Supported | 6.0 | GREATEST and LEAST support any number of arguments and any comparable type. Unlike most SQL functions, PostgreSQL's GREATEST/LEAST ignore NULLs and return a non-NULL result if at least one argument is non-NULL. This differs from MySQL and Oracle, where any NULL input produces a NULL result. |
| SQL Server | ✓ Supported | 2022 | GREATEST() and LEAST() were added in SQL Server 2022. Unlike MySQL/Oracle, SQL Server's implementation ignores NULLs (matching PostgreSQL behavior). For SQL Server 2019 and earlier, use a VALUES subquery with aggregation or nested IIF/CASE expressions as a workaround. |
| Oracle | ✓ Supported | 7 | GREATEST and LEAST supported since Oracle 7. If any argument is NULL, the result is NULL (same as MySQL, opposite of PostgreSQL). Arguments must be compatible types. Works with NUMBER, VARCHAR2, DATE, and TIMESTAMP. |
| SQLite | ✗ Not Supported | — | SQLite has no GREATEST or LEAST functions. Workaround for two values: use a CASE expression. For more values, use a subquery with MAX/MIN over a VALUES list (requires SQLite 3.8.3+ for VALUES in subqueries). |
The critical portability trap is NULL handling: MySQL and Oracle return NULL if any argument is NULL; PostgreSQL and SQL Server 2022 ignore NULLs and return the greatest/least non-NULL value. When porting between engines, wrap arguments in COALESCE/NVL/ISNULL to normalize NULL behavior. SQL Server only gained GREATEST/LEAST in 2022 — the VALUES subquery pattern (SELECT MAX(v) FROM (VALUES (a),(b),(c)) t(v)) is the cleanest pre-2022 workaround and is also the correct SQLite workaround. SQLite is the only engine in the compatibility matrix with no support.
GREATEST(v1, v2, ...) and LEAST(v1, v2, ...) support any number of arguments. If any argument is NULL, the result is NULL. Arguments are compared after implicit type conversion — mixing types triggers coercion rules. Works with numbers, strings, and dates.
GREATEST and LEAST support any number of arguments and any comparable type. Unlike most SQL functions, PostgreSQL's GREATEST/LEAST ignore NULLs and return a non-NULL result if at least one argument is non-NULL. This differs from MySQL and Oracle, where any NULL input produces a NULL result.
GREATEST() and LEAST() were added in SQL Server 2022. Unlike MySQL/Oracle, SQL Server's implementation ignores NULLs (matching PostgreSQL behavior). For SQL Server 2019 and earlier, use a VALUES subquery with aggregation or nested IIF/CASE expressions as a workaround.
GREATEST and LEAST supported since Oracle 7. If any argument is NULL, the result is NULL (same as MySQL, opposite of PostgreSQL). Arguments must be compatible types. Works with NUMBER, VARCHAR2, DATE, and TIMESTAMP.
SQLite has no GREATEST or LEAST functions. Workaround for two values: use a CASE expression. For more values, use a subquery with MAX/MIN over a VALUES list (requires SQLite 3.8.3+ for VALUES in subqueries).