SQL

SQL Any

SQL Any

This tutorial covers SQL Any operator.

The “any” operator is part of the logical operators.

It compares single column values with a scalar value. The “any” operator is equivalent to the “some” operator (check here).

For more information on operators here.

Here we use two tables. First is the table “student_info” from the previous tutorials, and the second is a table named “grade“, linked to the first table with a foreign key.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL
5PatrickDumont21
idsubjectNamegradepassedstudentID
1HistoryC11
2PhysicsA12
3HistoryF03
4BiologyA1NULL
5BiologyE15

Any

The operator “any” functions as a comparison.

It returns TRUE if any of the conditions are met.

Below is an example.

SELECT firstName, lastName FROM student_info
WHERE id = ANY (SELECT studentID FROM grade WHERE grade = 'A');

The above query displays all first and second student names that match grade A in the “grade” table.

firstNamelastname
PatrickCasey

Please note the 4th student does not show as the id cell is empty (NULL).

We can combine the above statement with the “in” logical operator.

This will allow multiple grade selections.

SELECT firstName, lastName FROM student_info
WHERE id = ANY (SELECT studentID FROM grade WHERE grade IN ('A', 'B', 'C'));
firstNamelastname
JohnSmith
PatrickCasey

Next: SQL All