SQL COUNT / COUNT(*)

Counts rows — COUNT(*) counts every row regardless of what's in it.

What & Why

COUNT(*) counts every row in the result, full stop — it doesn't look at any particular column's values, so a row full of NULLs still counts as one row.

See How It Works

BUSINESS QUESTION

How many campaigns are there in total?

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 count accumulate, row by rowRow 1 of 4
COUNT(*)
rowcampaignchannelspendcount(*)
1Spring Launchgoogle_ads55,000+1
2Retention Webinaremail45,000WAIT
3Finance Retargetinglinkedin50,000WAIT
4Enterprise Searchgoogle_ads60,000WAIT
Running resultCOUNT(*) = 1

Spring Launch contributes one complete row, raising COUNT(*) to 1.

EXAMPLE QUERY
SELECT COUNT(*) AS total_campaigns
FROM marketing.campaigns;
Now You Try

Practice this concept

Marketing wants the total number of campaign rows currently stored, including rows whose optional fields are NULL.

Available schema
marketing

Prefix tables with marketing.table_name.

total_campaigns
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario