SQL ANY Keyword
The ANY keyword in SQL is used to compare a value to any value in a subquery. It returns TRUE if at least one of the values returned by the subquery satisfies the condition.
SQL ANY Syntax
SQL
x
1
SELECT column1, column2, ...
2
FROM table_name
3
WHERE column operator ANY (SELECT column FROM table_name WHERE condition);
SQL ANY Example with Prescriptions Table
This query selects medication names from the Prescriptions
table where the dosage
matches any dosage used in prescriptions with the instruction 'Take once daily at bedtime.'
. This query returns all medications where the dosage equals any dosage found in prescriptions that include the given instruction.
SQL
1
1
SELECT medication_name
2
FROM Prescriptions
3
WHERE dosage = ANY (
4
SELECT dosage
5
FROM Prescriptions
6
WHERE instructions = 'Take once daily at bedtime.'
7
);
Note: This query will not work in our playground because the
ANY
keyword is not supported in SQLite, which is the engine used by TechKubo.