PostgreSQL BOOL_OR() Function
Summary: in this tutorial, you will learn about the PostgreSQL BOOL_OR()
function to aggregate boolean values across rows within a group.
Introduction to the PostgreSQL BOOL_OR() function
The BOOL_OR()
is an aggregate function that allows you to aggregate boolean values across rows within a group.
Here’s the syntax of the BOOL_OR()
function:
In this syntax, the expression
is the boolean expression to evaluate.
The BOOL_OR()
function returns true if at least one value in the group is true. If all values are false, the function returns false.
Please note that the BOOL_OR
function ignores NULL
s within the group.
PostgreSQL BOOL_OR() function examples
Let’s explore some examples of using the BOOL_OR()
function.
1) Setting up sample tables
First, create tables called teams
and members
:
Second, insert rows into the tables:
The teams
table:
The members
table:
2) Basic BOOL_OR() function example
The following example uses the BOOL_OR()
function to test if there are any active members in the members
table:
Output:
The BOOL_OR()
function returns true indicating that the members
table has active members.
2) Using BOOL_OR() function with GROUP BY clause
The following example uses the BOOL_OR()
function with the GROUP BY
clause to check if there are any active members in each team:
Output:
The output indicates that teams A and B have active members whereas team C does not have any active members.
3) Using BOOL_OR() function in HAVING clause
The following example uses the BOOL_OR()
function with the GROUP BY
and HAVING clauses to retrieve teams that have active members:
Output:
Summary
- Use the
BOOL_OR()
function to aggregate boolean values across rows within a group. - The
BOOL_OR()
function ignores NULLs in the group.