SQL

SQL All

SQL All

This tutorial covers SQL All operator.

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

It compares single column values with a scalar value. The syntax of “all” operator is the same as the “any” operator.

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

All

The operator “all” functions as a comparison.

It returns TRUE if all 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');
firstNamelastname

The above statement outputs an empty outcome. The reason is that “all” requires all values to meet the condition. There are different grades, hence the empty result.

We can try a different approach to see the effect of the “all” operator.

Let’s select the first and last names of all students, if all ids after the second student’s id match the ids in the “grade” table.

SELECT firstName, lastName FROM student_info
WHERE id > ALL (SELECT studentID FROM grade WHERE studentID = 2);
firstNamelastname
SamAdams
MikeGarcia
PatrickDumont

Next: SQL Like