count(*) vs count(id)

From: Matt Zagrabelny <mzagrabe(at)d(dot)umn(dot)edu>
To: "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: count(*) vs count(id)
Date: 2021-02-01 23:53:08
Message-ID: CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Greetings,

Is there a preferred method of counting rows?

count(*) vs count(field_name)

I have used count(*) for a long time and am hitting an inconsistency. At
least it is an inconsistency to me (I'm sure there is an explanation)...

INCORRECT OUTPUT (not desired results)
$ select calendar.entry, count(*) from generate_series('2020-01-01'::date,
'2021-01-10'::date, '1 day'::interval) as calendar(entry) left join
call_records on calendar.entry = call_records.timestamp::date group by
calendar.entry;
[...]
2020-08-30 00:00:00-05 │ 1
2020-08-31 00:00:00-05 │ 1
[...]

CORRECT OUTPUT (desired results)
$ select calendar.entry, count(id) from
generate_series('2020-01-01'::date, '2021-01-10'::date, '1 day'::interval)
as calendar(entry) left join call_records on calendar.entry =
call_records.timestamp::date group by calendar.entry;
[...]
2020-08-30 00:00:00-05 │ 0
2020-08-31 00:00:00-05 │ 0
[...]

What am I missing between count(*) and count(id)?

Thanks for any help!

-m

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2021-02-01 23:55:05 Re: count(*) vs count(id)
Previous Message Adrian Klaver 2021-02-01 23:18:44 Re: permission denied for large object 200936761