liveBook · Manning

Any operator

As explained in Learn SQL in a Month of Lunches

The ANY operator is an SQL quantified comparison operator that requires a comparison to hold for at least one value returned by a list or subquery.

When a subquery returns several values, the equality operator alone cannot compare a column with all of them. ANY lets a comparison succeed when it matches at least one value returned by the subquery. section 11.2 section 11.2.1

Find titles from order 1034

SELECT t.TitleName FROM title t WHERE TitleID = ANY ( SELECT TitleID FROM orderitem WHERE OrderID = 1034 ); Read this as: return title names whose TitleID matches any TitleID belonging to order 1034. The query returns the four titles in that order. section 11.2.1

ANY works with comparison operators such as =, >, >=, <, and <=. For equality-only matching, IN usually expresses the same intent more commonly: TitleID IN (subquery). Use ANY when the comparison itself matters. section 11.2.1

Do not use <> ANY to mean “matches none.” With several subquery values, almost every value differs from at least one result, so the condition can return every title. Use ALL for the corresponding comparison, or use NOT IN for equality-based exclusion. section 11.2.2

SOME is supported by many relational database systems as an alternative name for ANY. Subqueries can require additional processing and memory, so consider whether another query shape is necessary. section 11.2.1 section 11.2.2

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains multirow comparison, equality and inequality pitfalls, alternatives, and a subquery cost caution.
Examples
Demonstrates matching titles from order 1034 and contrasts = ANY with IN and <> ALL.
What it assumes
Assumes familiarity with SQL comparison operators, WHERE clauses, and subqueries.
What you can do after
Apply ANY in SQL WHERE clauses to compare columns with values returned by multirow subqueries.
How it connects
Connects ANY with subqueries, comparison operators, IN, SOME, and ALL.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.