How to Use Filter to Have Multiple Counts in PostgreSQL

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

  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
Shared queries and folders ✅ Version history ✅ One-click connection to PostgreSQL ✅
Get more done, together, with PopSQL and PostgreSQL