Multiple table expressions
WITH current_staff AS ( SELECT * FROM person WHERE active AND ( leaving_date IS NULL OR leaving_date >= CURRENT_DATE ) AND (start_date <= CURRENT_DATE) ), country_headcount AS ( SELECT o.country, count(*) AS headcount FROM current_staff s JOIN office o ON (s.office_id = o.id) GROUP BY 1 ) SELECT country, headcount FROM country_headcount ORDER BY headcount DESC;