PopSQL

How to Use Filter to Have Multiple Counts in PostgreSQL

Using filter is useful when you want to do multiple counts on a table:

select
  count(1), -- Count all users
  count(1) filter (where gender = 'male'), -- Count male users
  count(1) filter (where beta is true) -- Count beta users
  count(1) filter (where active is true and beta is false) -- Count active non-beta users
from users
database icon
SQL editing that just rocks
PopSQL and PostgreSQL, better together