Introduction – My Journey with NOT EXISTS vs NOT IN
I remember the first time I faced an SQL interview question about NOT EXISTS
vs NOT IN
— I froze. It sounded like such a small detail, but the more I dug into it, the more I realized how crucial it was. Why do so many experienced developers prefer NOT EXISTS
over NOT IN
? It’s not just about performance — it’s about avoiding subtle, hard-to-catch bugs that can cost you hours of debugging.
Let me walk you through exactly why NOT EXISTS
is often the better choice, and how understanding the difference could save you in your next SQL interview or real-world project.
NOT EXISTS vs NOT IN – The Problem with NULL Values
Let’s start with the tricky part — NULL handling. If you’ve worked with SQL long enough, you know that NULL
values are a constant source of frustration.
To make this clear, let’s create two simple tables — STUDENTS and ATTENDANCE — and see how NULL
values can break a NOT IN
query.
Create the STUDENTS table:
CREATE TABLE students (
student_id NUMBER PRIMARY KEY,
student_name VARCHAR2(50)
);
Create the ATTENDANCE table:
CREATE TABLE attendance (
student_id NUMBER
);
Insert sample data into STUDENTS:
INSERT ALL
INTO students VALUES (1, 'John')
INTO students VALUES (2, 'Priya')
INTO students VALUES (3, 'Rahul')
INTO students VALUES (4, 'Ananya')
INTO students VALUES (5, 'Meera')
SELECT * FROM DUAL;
Insert sample data into ATTENDANCE:
👉 Adding a NULL
value to simulate the issue with NOT IN
:
INSERT ALL
INTO attendance VALUES (1)
INTO attendance VALUES (3)
INTO attendance VALUES (NULL)
SELECT * FROM DUAL;
What Happens with NOT IN?
Let’s say you want to find the students who haven’t attended any classes. A NOT IN
query might seem like the obvious solution:
SELECT student_name
FROM students
WHERE student_id NOT IN (SELECT student_id FROM attendance);
👉 Output: No rows returned ❌
Why? Because NOT IN
treats any comparison with NULL
as unknown, which means the entire query fails. Even though student_id = NULL
is logically undefined, SQL interprets it as “false” and returns no results.
NOT EXISTS vs NOT IN – Fixing It with NOT EXISTS
Now, let’s switch to NOT EXISTS
and see the magic:
SELECT student_name
FROM students s
WHERE NOT EXISTS (
SELECT 1
FROM attendance a
WHERE a.student_id = s.student_id
);
👉 Output: ✅ Correct list of students who haven’t attended!
✅ Why does it work?
NOT EXISTS
doesn’t care aboutNULL
values — it simply checks for the existence of a row.- Since
NULL
values aren’t treated as matches, the query returns the expected result.
NOT EXISTS vs NOT IN: Performance Differences – Speed Matters!
The other major reason to prefer NOT EXISTS
is performance.
When you use NOT IN
, the database often performs a full table scan on the subquery, which can slow things down — especially if the subquery returns a large result set.
On the other hand, NOT EXISTS
allows the database engine to leverage indexing and join optimizations. In many cases, the database will short-circuit the check as soon as it finds a matching row — making it faster and more efficient.
Example:
Let’s say your attendance
table has 1 million rows.
NOT IN
might force the database to scan all 1 million rows.NOT EXISTS
might only need to check a fraction of those rows, thanks to indexing and optimizations.
Pro Tip:
If the subquery is large or you’re working with complex joins, NOT EXISTS
will almost always outperform NOT IN
.
NOT EXISTS vs NOT IN – Best Practices – When to Use What
So, should you abandon NOT IN
entirely? Not necessarily. Here’s a quick rule of thumb:
✅ Use NOT EXISTS
when:
- The subquery might return
NULL
values. - Performance is critical, especially with large datasets.
✅ Use NOT IN
when:
- You’re 100% sure that the subquery won’t return
NULL
values. - The dataset is small, and you prefer cleaner syntax.
NOT EXISTS vs NOT IN – My Takeaway
Switching from NOT IN
to NOT EXISTS
was a game-changer for me. I stopped getting those weird empty result sets, and my queries started running faster — especially with large datasets. If you’ve ever scratched your head wondering why your NOT IN
query isn’t working, give NOT EXISTS
a shot.
And here’s a tip — understanding this difference is one of those subtle things that can give you an edge in a tough SQL interview question. Interviewers love asking about it because it tests both your technical knowledge and problem-solving skills.
Want to Level Up Your SQL Skills?
If you found this helpful and want to advance your SQL knowledge, attend my upcoming SQL training! I’ll cover everything from the basics to advanced concepts, including real-world examples and practical exercises. Spots are limited, so register now to secure your place!
👉 Click here to register for my SQL training
NOT EXISTS vs NOT IN – Final Thoughts
Mastering the difference between NOT EXISTS
and NOT IN
will make you a more confident SQL developer — and could help you ace that next interview. If you found this helpful, drop a comment or share this post with someone preparing for an SQL interview! 😊