SQL SUM

Adds up every value in a column into a single total.

What & Why

SUM(column) adds every value in that column together, across all rows in the result.

See How It Works

BUSINESS QUESTION

Total campaign spend across the campaign portfolio.

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
Watch the aggregate accumulateRow 1 of 4
SUM(spend)
rowcampaignchannelspendstatusrunning sum
1Spring Launchgoogle_ads55,000ADD55,000
2Retention Webinaremail45,000WAIT
3Finance Retargetinglinkedin50,000WAIT
4Enterprise Searchgoogle_ads60,000WAIT
Running resultSUM(spend) = 55,000

55,000 is added; the running sum becomes 55,000.

EXAMPLE QUERY
SELECT SUM(spend) AS total_campaign_spend
FROM marketing.campaigns;
Now You Try

Practice this concept

Marketing wants total email recipients by campaign.

Available schema
marketing

Prefix tables with marketing.table_name.

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

Sign up free to try it on a real business scenario