EXISTS
EXISTS is true when its subquery returns at least one row. It is a good way to ask "does this have related rows?", and NOT EXISTS asks the opposite. It stops as soon as one row is found.
Example
SELECT name
FROM students s
WHERE EXISTS (SELECT 1 FROM enrolments e WHERE e.student_id = s.id)
ORDER BY name;Result
name Ada Amina Kofi Tunde Zainab
Example
SELECT name
FROM students s
WHERE NOT EXISTS (SELECT 1 FROM enrolments e WHERE e.student_id = s.id)
ORDER BY name;Result
name Esi Sipho Wanjiru
Exercise
Test whether the subquery has rows.
WHERE (SELECT 1 FROM enrolments)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.