Function or operator · Text, numbers and dates

LEAST and GREATEST in SQL: picking the smallest or largest value

LEAST picks the smallest and GREATEST the largest of several values in the same row. MIN and MAX, as aggregates, compare values across several rows.

Example

Imagine a promotion that cannot give more than 30 pounds off, even if the raw calculation comes out higher.

PostgreSQL / MySQL / SQL Server 2022+
WITH offers AS (
  SELECT 'A' AS name, 12 AS calculated_discount
  UNION ALL SELECT 'B', 35
  UNION ALL SELECT 'C', 27
)
SELECT
  name,
  LEAST(calculated_discount, 30) AS applied_discount
FROM offers
ORDER BY name;

What changes from case to case

SQLite uses min() and max() with several arguments

SQLite does not provide LEAST/GREATEST under those names. Its scalar functions min(X,Y,...) and max(X,Y,...) play the same role when given two or more arguments.

Equivalent run in SQLite 3.46.1
WITH offers AS (
  SELECT 'A' AS name, 12 AS calculated_discount
  UNION ALL SELECT 'B', 35
  UNION ALL SELECT 'C', 27
)
SELECT
  name,
  min(calculated_discount, 30) AS applied_discount
FROM offers
ORDER BY name;

The key difference: NULL

Do not carry your intuition from one engine to another. PostgreSQL and SQL Server document that LEAST/GREATEST ignore NULL arguments as long as at least one value is not null. MySQL documents that GREATEST() returns NULL if any argument is NULL; SQLite's multi-argument max(X,Y,...) also returns NULL if any argument is null.

Consequence: GREATEST(10, NULL, 20) is not portable as a semantically identical expression. If null handling matters, normalise the nulls explicitly with CASE or COALESCE according to what you really want to ask.

Types matter too

The arguments must be comparable under the engine's rules. Mixing text and numbers can trigger implicit conversions or raise an error. For a maintainable query, compare values from the same domain and use CAST when the conversion is deliberate.

Applying a per-row limit without changing what it means

These expressions compare arguments within a single row. They are used to put a ceiling on a discount or a floor under a measurement. They do not summarise all the rows the way MIN(column_name) or MAX(column_name) aggregates do. To keep a known discount between 0 and 30, you can combine a maximum with zero and a minimum with 30; first decide what should happen when the discount is missing.

The test below includes a negative discount, a valid one and one above the ceiling. The expected result is 0, 12 and 30. Applying limits can tidy up what you display, but it does not prove the original calculation was right. If a negative value signals a refund or an error that needs investigating, converting it without keeping the original would hide information.

Null handling differs between engines, and a substitution can change a business policy. An unknown discount should never accidentally become the largest discount available. Keep types compatible, state the rule for missing values and test a row with a null separately. The SQLite version uses multi-argument functions; adding or removing arguments can switch them between scalar mode and aggregate mode.

Self-contained case to check in SQLite

This version runs in SQLite and keeps known discounts between zero and thirty.

Data and query · SQLite
WITH offers(id, discount) AS (VALUES (1, -5), (2, 12), (3, 35))
SELECT id, discount, min(max(discount, 0), 30) AS applied_discount
FROM offers ORDER BY id;
Verified result of the self-contained case
iddiscountapplied_discount
1-50
21212
33530

max with two arguments applies the floor to each row and min with two arguments applies the ceiling. The original discount is kept so the calculation can be reviewed.

Check your interpretation: Why is this not the same as SELECT MAX(discount) over all the offers?

Show answer

Because MAX(discount) as an aggregate summarises every row into a single value, 35. Here max and min with several arguments compare values within each row and keep all three offers.

Technical questions: SQLite: scalar functions (opens in a new tab).