SQL BOOL_AND function
Returns true only when every input row that is not NULL evaluates to true.
- Категория
- Aggregates
- Returns
boolean- In this category
- 13 of 52
Синтаксис
BOOL_AND(boolean) | BOOL_AND(boolean) FILTER (WHERE ...)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Да | Supported under this name. |
| MySQL | Not built in | use MIN(col = 1) or SUM(col = 1) = COUNT(col) |
| SQLite | Not built in | use MIN(col) over 0/1 values |
| Oracle | Different name or syntax | 23ai+ spells it BOOLEAN_AND_AGG; older releases use MIN over a 0/1 CASE |
Примеры
PostgreSQL
SELECT bool_and(active) FROM users WHERE tenant_id = 42;
PostgreSQL
SELECT project_id, bool_and(done) FILTER (WHERE assigned) FROM tasks GROUP BY project_id;
MySQL
SELECT project_id, MIN(done = 1) AS all_done FROM tasks GROUP BY project_id;
Полезно знать
- NULL inputs are skipped entirely, not treated as false; a group where the non-NULL rows are all true still yields true.
- An empty group or an all-NULL group returns NULL, not false; wrap with COALESCE(BOOL_AND(x), true) if you want the vacuously-true reading.
- SQL Server, MySQL and SQLite have no BOOL_AND; MIN(status = 1) or SUM(status = 1) = COUNT(status) reproduces it with 0/1 values. Oracle 23ai has it as BOOLEAN_AND_AGG.
- MySQL's BIT_AND is bitwise, not logical; do not use it as a substitute for booleans.
Questions about BOOL_AND
What does BOOL_AND return for NULL values?
NULLs are ignored. Only rows that are not NULL count, so the result is true when every non-NULL row is true.
How do I write BOOL_AND in MySQL or SQL Server?
Use MIN(condition) where the condition yields 1 or 0: MIN(status = 1) in MySQL, MIN(CASE WHEN status = 1 THEN 1 WHEN status IS NOT NULL THEN 0 END) in SQL Server. A result of 1 means every row passed. A plain ELSE 0 would count NULL rows as failures, which BOOL_AND does not.
Is BOOL_AND the same as EVERY?
Yes. EVERY is the standard SQL name; PostgreSQL accepts both spellings, and Oracle 23ai accepts EVERY alongside BOOLEAN_AND_AGG.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.