SQL AVG

The sum divided by the count — but the count only includes non-NULL rows, which matters more than it sounds.

What & Why

AVG(column) computes the mean — sum divided by count. Critically, both the sum and the count ignore NULL values entirely, which is covered in depth in the final lesson of this series.

See How It Works

BUSINESS QUESTION

Average 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 AVG divide the sum by the countStep 1 of 2
SUM(spend) = 55,000 + 45,000 + 50,000 + 60,000
idcampaignspendsum contribution
1Spring Launch55,000.00+ 55,000.00
2Retention Webinar45,000.00+ 45,000.00
3Finance Retargeting50,000.00+ 50,000.00
4Enterprise Search60,000.00+ 60,000.00
SUM(spend)210,000

First, AVG adds the four non-NULL spend values.

EXAMPLE QUERY
SELECT AVG(spend) AS avg_spend
FROM marketing.campaigns;
Now You Try

Practice this concept

Marketing wants average lead score by source.

Available schema
marketing

Prefix tables with marketing.table_name.

sourceavg_lead_score
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario