EVERY
EVERY is a SQL function that verifies whether all records in a set satisfy a specified condition. If they do, the function returns TRUE. If at least one record does not meet the condition, it returns FALSE. EVERY is typically used with boolean conditions.
EVERY(subquery)
- subquery: This is an SQL statement that yields a result set, to be used by the EVERY function. It should output a single column of Boolean type or a column that can be implicitly converted to Booleans. Depending on the values in this column - TRUE, FALSE or NULL - the EVERY function will return a result accordingly.
Example
Output
Explanation
In the example provided, the EVERY
function is used to check if the age of every employee in the Employees
table is greater than 20. The function returns 0, indicating that not every employee’s age is greater than 20.
EVERY(expression) FILTER (WHERE condition)
- expression: This refers to the specific condition or comparison result that the EVERY function will evaluate across all rows in the group. It represents the field in which the condition will be checked.
- filter (where condition): This is an optional clause that allows you to specify a condition to filter the rows used in the aggregation process. The EVERY function will only consider rows where this condition is true.
Example
Output
Explanation
The “EVERY” function in PostgreSQL returns ‘true’ if all input values are ‘true’. In the provided example, the function checks the ‘is_enabled’ column in ‘temp_table’. Since one of the rows contains ‘false’, the ‘EVERY’ function returns ‘false’.
EVERY(expression) OVER ([query_partition_clause] [order_by_clause] [windowing_clause])
- expression: The expression is a logical or computational statement that can either be a subquery, column, or a set of columns upon which the function is to be applied.
- query_partition_clause: The partition by clause is optional. It separates the input into different partitions or groups which share the same values in a specified set of columns. If this clause is not included in the function, SQL considers the whole result set as a single group.
- order_by_clause: The optional order by clause determines the order of data in a partition. It orders rows based on column values.
- windowing_clause: This is a also an optional parameter. The windowing clause further breaks down the sorted rows into sliding or anchored windows within each partition. Default windows restrict operations to the current row if the clause is not defined.
Example
Output
Explanation
In this SQL snippet, the EVERY function checks whether all the rows in ‘COLA’ are greater than zero. If all values satisfy the criteria, it returns TRUE. Otherwise, it returns FALSE. In this case, since ‘-1’ is part of ‘COLA’ and is not greater than zero, the function returns FALSE.