Day 54 – SQL ANY Keyword
SQL ANY keyword is used in conjunction with a comparison operator to evaluate a condition against multiple values in a subquery or a list of values.
The syntax for using the ANY keyword is as follows:
SELECT column_name(s) FROM table_name WHERE column_name operator ANY (subquery or list of values);
column_name is the name of the column in the table that you want to evaluate,
operator is the comparison operator (such as
LIKE, etc.), and
subquery or list of values is the set of values against which you want to compare.
For example, the following SQL statement selects all the customers from the “Customers” table whose customer IDs match any of the values in the subquery:
SELECT * FROM Customers WHERE CustomerID = ANY (SELECT CustomerID FROM Orders WHERE OrderDate = '2022-01-01');
In this case, the subquery returns a list of customer IDs for the orders that were placed on January 1st, 2022, and the ANY keyword is used to compare the CustomerID column with this list of values. If the CustomerID matches any of the values in the list, the row is returned.