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
Better SQL for the people
Get more done with PopSQL and PostgreSQL