PostgreSQL ALL Operator
PostgreSQL ALL Operator
The ALL operator in PostgreSQL is used to compare a value with every value returned by a subquery or array.
The condition is TRUE only if it satisfies the comparison for all values.
🔹 Think of
ALLas “AND logic” over multiple values.
1️⃣ Basic ALL Syntax
With a subquery
With an array
2️⃣ Simple ALL Example (Subquery)
Find products whose price is greater than all products in category mobile.
✔ Returns products priced higher than the most expensive mobile.
3️⃣ ALL with Comparison Operators
| Operator | Meaning with ALL |
|---|---|
= ALL |
Equal to every value |
> ALL |
Greater than maximum |
< ALL |
Less than minimum |
>= ALL |
≥ maximum |
<= ALL |
≤ minimum |
!= ALL |
Not equal to any value |
Example:
✔ Excludes employees with any HR salary value.
4️⃣ ALL with Array Example
✔ Returns users older than 25.
5️⃣ ALL vs ANY (Key Difference)
| ALL | ANY |
|---|---|
| Must satisfy all values | Satisfy at least one |
| AND logic | OR logic |
| More restrictive | Less restrictive |
Example:
6️⃣ ALL vs IN
✔ IN is preferred for equality checks.
7️⃣ ALL with UPDATE
8️⃣ Edge Cases (Important!)
Empty Subquery
✔ This difference is critical in logic-heavy queries.
9️⃣ Common Mistakes
❌ Confusing ALL with ANY
✔ Remember: ALL = every value
❌ Using ALL for existence checks
✔ Use EXISTS instead
🔟 Best Practices
✔ Use ALL when comparing against extremes (max/min)
✔ Use ANY or IN for less strict logic
✔ Index subquery columns for performance
✔ Always test edge cases
