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
Syntax
bool_and(expression) | bool_or(expression)
Parameters
| Parameter | Description |
|---|---|
expression |
A boolean expression. |
Return value
boolean, or NULL for no values.
Examples
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.
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.
Related
COUNT()· Count rows: count(*) counts every row, count(x) skips NULLs, and count(DISTINCT x) counts unique values. Returns bigint.FILTER· Restrict which rows one aggregate sees with FILTER (WHERE …), so one query can count several conditions.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial