EXISTS
EXISTS (subquery) is true when the subquery returns at least one row, and false when it returns none. It answers yes-or-no questions about related rows: "has this student made any payment?", "does this course have anyone enrolled?". It doesn't care what the subquery returns, only whether it returns anything, so it stops looking at the first match. Its partner NOT EXISTS finds rows with no match, and it's the safe way to do that: unlike NOT IN, a NULL can't break it. This lesson covers both, and finishes with the chapter's checkpoint.
In One Minute
WHERE EXISTS (SELECT 1 FROM t WHERE t.key = outer.key)keeps rows that have a match.WHERE NOT EXISTS (...)keeps rows that have no match: an anti-join.- What the subquery selects doesn't matter;
SELECT 1is the usual habit. EXISTSis almost always correlated: it refers to the outer row.NOT EXISTShandlesNULLcorrectly;NOT INreturns nothing if the list contains aNULL.
EXISTS
SELECT columns
FROM outer_table AS o
WHERE EXISTS (
SELECT 1 FROM other_table AS x WHERE x.key = o.key
);Students who have made at least one payment:
SELECT s.first_name
FROM students AS s
WHERE EXISTS (
SELECT 1 FROM payments AS p WHERE p.student_id = s.student_id
)
ORDER BY s.student_id; first_name
------------
Srihitha
Navika
(2 rows)For each student, the subquery looks for a payment with that student_id. Srihitha has five, but she appears once: EXISTS only asks "is there at least one?", so unlike a join, it never repeats the outer row. Advitha has none, so EXISTS is false for her. SELECT 1 is a convention; SELECT * or SELECT p.payment_id would behave identically, because the values are never used.
NOT EXISTS: Rows with No Match
NOT EXISTS keeps the outer rows for which the subquery finds nothing. Students who have never paid:
SELECT s.first_name
FROM students AS s
WHERE NOT EXISTS (
SELECT 1 FROM payments AS p WHERE p.student_id = s.student_id
); first_name
------------
Advitha
(1 row)Courses nobody has enrolled in work the same way, giving Git Basics. This is the anti-join you met in LEFT JOIN, written more directly: NOT EXISTS says exactly what it means.
Why NOT EXISTS Beats NOT IN
NOT IN breaks when the list contains a NULL; NOT EXISTS doesn't. Which students mentor nobody? Every mentor_id is a student who mentors someone, so "mentors nobody" means "not in the list of mentor ids":
SELECT first_name
FROM students
WHERE student_id NOT IN (SELECT mentor_id FROM students); first_name
------------
(0 rows)No rows, yet Navika mentors nobody. The list of mentor ids is 101, 102 and NULL (Srihitha has no mentor). For Navika, 103 NOT IN (101, 102, NULL) means 103 <> 101 AND 103 <> 102 AND 103 <> NULL, and the last part is unknown, so the whole condition is unknown and her row is dropped. The same question with NOT EXISTS:
SELECT s.first_name
FROM students AS s
WHERE NOT EXISTS (
SELECT 1 FROM students AS m WHERE m.mentor_id = s.student_id
); first_name
------------
Navika
(1 row)Navika, correctly. NOT EXISTS asks whether any row matches, and a NULL mentor_id simply doesn't match anyone, so it can't spoil the answer.
IN and EXISTS, without NOT, give the same results even with NULLs, so either works for "has a match". PostgreSQL usually runs EXISTS, IN and the equivalent join with the same plan.
Common Mistakes
Forgetting the correlation. WHERE EXISTS (SELECT 1 FROM payments) is true for every student as long as any payment exists. Connect the subquery to the outer row.
NOT IN with a nullable column. It silently returns nothing. Use NOT EXISTS.
Expecting EXISTS to return the matched columns. It only filters. Join if you need columns from the other table.
Interview Corner
What does EXISTS do?
It returns true if its subquery returns at least one row and false otherwise. It's typically correlated to the outer row and stops at the first match.
What's the difference between NOT IN and NOT EXISTS?
They agree when there are no NULLs. If the NOT IN list contains a NULL, NOT IN returns no rows, because each comparison with NULL is unknown. NOT EXISTS isn't affected, so it's the safe choice.
Does it matter what an EXISTS subquery selects?
No. Only whether it returns a row matters; SELECT 1 is the common convention.
Practice
Challenge (chapter checkpoint): List the students who enrolled in a course but never made any payment, written two ways: once with NOT EXISTS, once with a LEFT JOIN anti-join. Both should return the same student. Start from enrollments, and show each student once.
Show solution
SELECT DISTINCT e.student_id
FROM enrollments AS e
WHERE NOT EXISTS (
SELECT 1 FROM payments AS p WHERE p.student_id = e.student_id
)
ORDER BY e.student_id;
SELECT DISTINCT e.student_id
FROM enrollments AS e
LEFT JOIN payments AS p ON p.student_id = e.student_id
WHERE p.payment_id IS NULL
ORDER BY e.student_id; student_id
------------
102
(1 row)
student_id
------------
102
(1 row)Both return 102, Advitha, who has two enrollments and no payments. DISTINCT shows her once, because she has two enrollment rows. The NOT EXISTS version states the question most directly.
Key Takeaways
| You want to | Write |
|---|---|
| Rows with a match | WHERE EXISTS (SELECT 1 FROM payments p WHERE p.student_id = s.student_id) |
| Rows with no match | WHERE NOT EXISTS (SELECT 1 FROM payments p WHERE p.student_id = s.student_id) |
Was this tutorial helpful?