SQL MIN OVER

The smallest value in the window, attached to every row — useful for 'how far above the floor am I.'

What & Why

MIN(column) OVER (PARTITION BY ...) finds each partition's smallest value and repeats it on every row in that group.

See How It Works

BUSINESS QUESTION

Marketing wants each campaign beside the minimum spend in its channel.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  id AS campaign_id,
  channel,
  spend,
  MIN(spend) OVER (PARTITION BY channel) AS channel_min_spend
FROM marketing.campaigns
ORDER BY channel, campaign_id;
RESULT — channel minimum beside campaigns
campaign_idchannelspendchannel_min_spend
2email45000.0045000.00
1google_ads55000.0055000.00
4google_ads60000.0055000.00
3linkedin50000.0050000.00

MIN OVER uses the lowest spend in each channel partition.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario