Skip to content

FILTER

SELECT
COUNT(*) FILTER (WHERE age > 18) AS "number_of_adults",
COUNT(*) FILTER (WHERE age <= 18) AS "number_of_children"
FROM
people;
number_of_adults | number_of_children
------------------+--------------------
25 | 10

The example code counts the number of adults (individuals older than 18 years) and children (individuals 18 years or younger) in the table named people. The FILTER clause specifies the condition based on which the rows are counted.