SQL EXISTS Keyword
The EXISTS keyword is used to check whether a subquery returns any rows. It returns TRUE if the subquery produces one or more results.
SQL EXISTS Syntax
SQL
x
1
SELECT column1
2
FROM table1
3
WHERE EXISTS (
4
SELECT column2
5
FROM table2
6
WHERE condition
7
);
SQL EXISTS Example
This example lists all doctors who have at least one appointment in the Appointments table.
SQL
1
1
SELECT first_name, last_name
2
FROM Doctors
3
WHERE EXISTS (
4
SELECT appointment_id
5
FROM Appointments
6
WHERE Appointments.doctor_id = Doctors.doctor_id
7
);
