PostgreSQL ALL Operator
Summary: in this tutorial, you will learn how to use the PostgreSQL ALL
operator to compare a value with a list of values returned by a subquery.
Overview of the PostgreSQL ALL operator
The PostgreSQL ALL
operator allows you to compare a value with all values in a set returned by a subquery.
Here’s the basic syntax of the ALL
operator:
In this syntax:
- The
ALL
operator must be preceded by a comparison operator such as equal (=), not equal (<>), greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=). - The
ALL
operator must be followed by a subquery which also must be surrounded by the parentheses.
If the subquery returns a non-empty result set, the ALL
operator works as follows:
value > ALL (subquery)
returns true if the value is greater than the biggest value returned by the subquery.value >= ALL (subquery)
returns true if the value is greater than or equal to the biggest value returned by the subquery.value < ALL (subquery)
returns true if the value is less than the smallest value returned by the subquery.value <= ALL (subquery)
returns true if the value is less than or equal to the smallest value returned by the subquery.value = ALL (subquery)
returns true if the value equals every value returned by the subquery.value != ALL (subquery)
returns true if the value does not equal any value returned by the subquery.
If the subquery returns no row, then the ALL
operator always evaluates to true.
PostgreSQL ALL operator examples
Let’s explore some examples of using the PostgreSQL ALL
operator.
Setting up a sample table
1) Using the ALL operator with the greater than operator (>) example
The following example uses the ALL
operator for employees who have salaries greater than all managers
Output:
The query returns one row with a salary of 75K greater than the highest salary of all managers (60K).
2) Using the ALL operator with the less than operator (<) example
The following example uses the ALL
operator for employees who have salaries less than all managers:
Output:
It returns all the employees whose salaries are less than the lowest salary of all managers which is 55K.
Summary
- Use the PostgreSQL
ALL
operator to compare a value with all values in a set of values returned by a subquery.