SQL =ANY operator
True when the comparison holds for at least one element of the list, array or subquery.
- Категория
- Условные конструкции
- Returns
boolean- In this category
- 14 of 43
Синтаксис
x = ANY (subquery) | x = ANY (ARRAY[...]) | x = SOME (...)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different name or syntax | subqueries only: x = ANY (SELECT ...) |
| PostgreSQL | Да | = ANY works on arrays and subqueries |
| MySQL | Different name or syntax | subqueries only: x = ANY (SELECT ...) |
| SQLite | Not built in | no ANY/SOME/ALL; use IN or EXISTS |
| Oracle | Да | subqueries and literal lists: x = ANY (1, 2) |
Примеры
PostgreSQL
SELECT * FROM orders WHERE id = ANY(ARRAY[1, 3, 7]);
Oracle
SELECT * FROM orders WHERE id = ANY (SELECT id FROM big_orders);
Полезно знать
- x <> ANY (list) is almost never what you want: it is true when at least one element differs, which is nearly always. You probably meant NOT IN or <> ALL.
- = ANY handles NULL members exactly like IN: a match is TRUE, but no match with a NULL present is UNKNOWN rather than FALSE.
- In PostgreSQL, IN (list) is exactly = ANY (list); the array form also accepts one bound parameter for a whole array.
- SQL Server and MySQL accept ANY, SOME and ALL only in front of a subquery; x = ANY (1, 2) is a syntax error there, so use IN for literal lists.
Questions about ANY
ANY vs IN?
x IN (subquery) is defined as x = ANY (subquery) on every engine that has ANY. ANY also works with other operators (> ANY, < ANY), and in PostgreSQL with arrays.
Why does x <> ANY (1, 2) surprise me?
It is true whenever x differs from at least one element, so x = 1 still passes. For 'different from every element' use <> ALL or NOT IN.
What does =ANY do with NULLs in the list?
One matching element is enough: TRUE OR UNKNOWN is TRUE, so the row is kept.
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: SQL Server, PostgreSQL, MySQL, 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.