// conditional reference

SQL ALL operator

True when the comparison holds for every element of the list or subquery.

Категория
Условные конструкции
Returns
boolean
In this category
15 of 43

Синтаксис

x > ALL (subquery) | x <> ALL (subquery)

Поддержка движка

ДвижокSupportЗаметки
SQL Server Different name or syntax subqueries only
PostgreSQL Да subqueries and arrays
MySQL Different name or syntax subqueries only
SQLite Not built in no ALL; use NOT EXISTS or a MAX/MIN subquery
Oracle Да subqueries and literal lists

Примеры

PostgreSQL

SELECT * FROM products WHERE price > ALL (SELECT price FROM products WHERE category = 'budget');

Oracle

SELECT * FROM products WHERE price > ALL (SELECT price FROM products WHERE category = 'budget');

Полезно знать

  • x > ALL (subquery) equals x > (SELECT MAX(...)) only when the subquery returns at least one row and no NULLs. On an empty subquery ALL is TRUE while the MAX comparison is UNKNOWN.
  • x <> ALL (list) is the same as NOT IN: one NULL in the subquery empties your result set.
  • An empty subquery makes ALL vacuously true (and ANY false), so a broken subquery can silently pass every row.

Questions about ALL

ALL vs MAX?

x > ALL (SELECT y FROM t) matches x > (SELECT MAX(y) FROM t) when t has rows and y has no NULLs. On an empty t the ALL form keeps every row and the MAX form keeps none.

What does ALL return for an empty subquery?

TRUE. Vacuously, every (zero) comparison passed. ANY returns FALSE for the same input.

Why did <> ALL return nothing?

A NULL in the subquery: NOT IN's trap. Filter the NULLs or use NOT EXISTS.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

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.