PostgreSQL 18 reference · Aggregate Functions

PostgreSQL BOOL_AND() Function

Return true if every non-NULL value in a group is true. bool_or() returns true if at least one is.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

bool_and(expression)  |  bool_or(expression)

Parameters

ParameterDescription
expression A boolean expression.

Return value

boolean, or NULL for no values.

Examples

Every and any
SELECT customer_id, bool_and(status = 'delivered') AS all_delivered, bool_or(status = 'pending') AS any_pending
FROM orders
GROUP BY customer_id
ORDER BY customer_id
LIMIT 3;

Output

 customer_id | all_delivered | any_pending
-------------+---------------+-------------
           1 | f             | t
           2 | f             | t
           3 | f             | f
(3 rows)

Run this on your computer — see “Get Started” for setup.

One condition for a group
SELECT bool_and(stock > 0) AS all_in_stock FROM products WHERE category_id = 2;

Output

 all_in_stock
--------------
 t
(1 row)

Run this on your computer — see “Get Started” for setup.

Was this page helpful?