SQL LEAST

The mirror of GREATEST — returns the smallest value out of a list, useful for applying a ceiling instead of a floor.

What & Why

LEAST(value1, value2, ...) returns the smallest of its arguments — the same row-by-row comparison as GREATEST, just the opposite direction. Where GREATEST enforces a floor, LEAST enforces a ceiling.

See How It Works

BUSINESS QUESTION

Marketing wants reported clicks capped at the number of opens.

idcampaign_idsent_atrecipientsopensclicksunsubscribesbounces
20112024-01-20 15:00:00+001200540180924
20222024-02-15 16:30:00+0080042096512
20332024-03-18 13:00:00+0015006102251431
20442024-04-08 14:00:00+0000000
EXAMPLE QUERY
SELECT
  id AS send_id,
  opens,
  clicks,
  LEAST(clicks, opens) AS valid_clicks
FROM marketing.email_sends
ORDER BY send_id;
RESULT — exact output from the displayed Queryflo rows
send_idopensclicksvalid_clicks
201540180180
2024209696
203610225225
204000

The valid click count is the smaller value on every email-send row.

Now You Try

Practice this concept

Marketing wants reported clicks capped at the number of opens.

Available schema
marketing

Prefix tables with marketing.table_name.

send_idopensclicksvalid_clicks
marketing.email_sends
ColumnType
idinteger
campaign_idinteger
sent_attimestamp with time zone
recipientsinteger
opensinteger
clicksinteger
unsubscribesinteger
bouncesinteger
legacy_send_grouptext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario