AggregatePostgreSQL only

BOOL_OR

Returns true if a condition is true for at least one row.

Syntax

BOOL_OR(<condition>)

The “is there at least one?” aggregate function. Together with GROUP BY it answers that for each group: is there anyone in the department without a phone number. It skips NULL. Its mirror function is BOOL_AND: “all of them?”.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

Does anyone earn over 1300, and over 2000
SELECT BOOL_OR(salary > 1300) AS someone_above_1300,
BOOL_OR(salary > 2000) AS someone_above_2000
FROM employees;
someone_above_1300someone_above_2000
truefalse
Is there anyone in the department without a phone number
SELECT department,
BOOL_OR(phone IS NULL) AS someone_without_phone
FROM employees
GROUP BY department
ORDER BY department;
departmentsomeone_without_phone
backendtrue
designtrue
frontendfalse
Departments with someone who has no phone number
SELECT department
FROM employees
GROUP BY department
HAVING BOOL_OR(phone IS NULL)
ORDER BY department;
department
backend
design

Watch out

NULL values are skipped, and if the condition gives NULL for every row, the result is NULL too, not false: BOOL_OR(bonus > 100) in a group where nobody has a bonus.

If there are no rows, the result is NULL.

To simply check whether there's at least one row, you don't need a condition: EXISTS (…) or COUNT(*) > 0 is simpler.

Related functions