SQL ALL Keyword
The ALL
keyword is used with a comparison operator to return TRUE
if the comparison is true for all values returned by a subquery.
SQL ALL Syntax
The operator
can be: =
, !=
, >
, <
, >=
, <=
SQL
x
1
SELECT column_name(s)
2
FROM table_name
3
WHERE expression operator ALL (subquery);
SQL ALL Example
This query attempts to select patients whose patient_id
matches all the patient IDs from the Appointments
table with a specific doctor_id
(e.g. 1):
SQL
1
1
SELECT first_name, last_name
2
FROM Patients
3
WHERE patient_id = ALL (
4
SELECT patient_id
5
FROM Appointments
6
WHERE doctor_id = 1
7
);
Important: The
ALL
keyword with subqueries causes a syntax error in our playground environment.
You will see:near "ALL": syntax error
.
This feature is not supported, and you should use alternative approaches such asIN
,JOIN
, orNOT EXISTS
where applicable.