T-SQL Tutorial

T-SQL Any operator


The ANY subquery is one that is categorized as a comparison operator and similar to the meaning in English, it is used to determine when certain data satisfies a criteria compared to what the ANY condition specifies.

When using the ANY subquery in SQL Server database, it means that a comparison must be made and for the condition to satisfy the outer query, the value that introduces your subquery must be greater than at least one of the values listed in the returned results of the subquery.

Cities table:

CITY_IDNAMESTATE
1New YorkNew York
2Los AngelesCalifornia
3ChicagoIllinois
4San AntonioTexas
5San DiegoCalifornia

States table:

STATE_IDNAME
1Arizona
2California
3Texas
4Michigan

Any Example:

Find the cities that have the any state in the states table using = any.
SELECT * FROM cities c
WHERE c.state = ANY (SELECT s.name FROM states s );

Result:

CITY_IDNAMESTATE
2Los AngelesCalifornia
4San AntonioTexas
5San DiegoCalifornia