Sitemap
Varsha Vishwakarma

Varsha Vishwakarma

Data Engineer | Exploring data, solving problems, and sharing practical lessons on SQL, Power BI, and analytics.

EXISTS vs IN in SQL Explained with Real Examples | SQL Interview Series #4

7 min readJul 23, 2026

--

Press enter or click to view image in full size

Ever written a query that returned the correct results — but took forever to execute?

Or perhaps you’ve been asked in an interview:

“When would you use EXISTS instead of IN?”

At first glance, both seem to solve the same problem.

They both help us check whether matching values exist in another table.

So naturally, many beginners ask:

“If both produce the same result, why do we have two different operators?”

The answer lies in how SQL executes the query, not just what it returns.

And understanding that difference can save you from writing slow queries in production systems.

In this article, we’ll learn EXISTS vs IN using real business examples from Amazon, Swiggy, Netflix, and Banking systems. By the end, you’ll know not only which one to use but also why.

What You’ll Learn

In this article, we’ll cover:

  • What EXISTS is
  • What IN is
  • How they work internally
  • Differences between EXISTS and IN
  • Performance comparison
  • NULL behavior
  • Interview questions
  • Common mistakes
  • Best practices

Let’s begin.

A Real-Life Story

Imagine you work as a Data Engineer at Amazon.

The company wants to identify customers who have placed at least one order.

There are two tables:

Customers:

Table: Customers

Orders:

Table: Orders

Business wants this output:

Output

Notice something?

We don’t care how many orders Alice placed.

We only want to know whether at least one order exists.

This is exactly where EXISTS shines.

Understanding EXISTS

The EXISTS operator checks whether the subquery returns at least one row.

The moment SQL finds the first matching record, it stops searching.

Think of it like checking attendance.

Teacher asks:

“Is Rahul present?”

The monitor sees Rahul sitting in the first row.

Does the monitor continue checking every student?

No.

The answer is already found.

That’s how EXISTS works.

SQL Query

SELECT CustomerID,
Name
FROM Customers C
WHERE EXISTS
(
SELECT 1
FROM Orders O
WHERE O.CustomerID = C.CustomerID
);

Output

Output

Execution Flow of EXISTS

For every customer:

Customer 1

↓

Order found?

↓

Yes

↓

Return Alice

Customer 2

↓

Order found?

↓

No

↓

Skip

Customer 3

↓

Order found?

↓

Yes

↓

Return Charlie

Notice something important?

SQL stops searching after the first match.

It never scans unnecessary rows.

Understanding IN

Now let’s solve the same problem using IN.

SELECT CustomerID,
Name
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
);

Output

Exactly the same.

So what’s different?

IN first prepares a list of CustomerIDs.

1
1
3

Then SQL checks:

Is CustomerID = 1 in this list?

Is CustomerID = 2 in this list?

Is CustomerID = 3 in this list?

Real-Life Example (Netflix)

Netflix stores two tables.

Users

Table: Users

PremiumSubscriptions

Table: PremiumSubscriptions

Find premium users.

Using IN:

SELECT Name
FROM Users
WHERE UserID IN
(
SELECT UserID
FROM PremiumSubscriptions
);

Simple.

Readable.

Perfect.

Another Example (Swiggy)

Swiggy wants restaurants that received at least one order today.

Tables:

Restaurants

Table: Restaurants

Orders

Table: Orders

Using EXISTS

SELECT Name
FROM Restaurants R
WHERE EXISTS
(
SELECT 1
FROM Orders O
WHERE O.RestaurantID = R.RestaurantID
);

The database stops after finding the first order.

Get Varsha Vishwakarma’s stories in your inbox

Join Medium for free to get updates from this writer.

Restaurant 1

First order found.

Done.

No need to check remaining orders.

How SQL Executes EXISTS

Suppose Orders has

10 million rows.

Restaurant 1 appears in Row 15.

EXISTS finds Row 15.

Stops.

Execution complete.

How SQL Executes IN

SQL often prepares the complete list first.

Even if Restaurant 1 appears in Row 15, SQL may still continue processing depending on the optimizer and execution plan.

This is why EXISTS is usually preferred for large correlated lookups.

Performance Comparison

EXISTS vs IN

Banking Example

A bank wants customers who have at least one loan.

Customers

Table: Customers

Loans

Press enter or click to view image in full size
Table: Loans

Using EXISTS

SELECT Name
FROM Customers C
WHERE EXISTS
(
SELECT 1
FROM Loans L
WHERE L.CustomerID = C.CustomerID
);

As soon as LoanID 501 is found, SQL returns Amit.

LoanID 502 is irrelevant.

EXISTS with Correlated Subquery

One of the biggest strengths of EXISTS is that it works naturally with correlated subqueries.

SELECT *
FROM Employees E
WHERE EXISTS
(
SELECT 1
FROM Attendance A
WHERE A.EmployeeID = E.EmployeeID
);

The inner query depends on the outer query.

This is exactly what correlated subqueries are designed for.

What About NULL?

Here’s where interviews become interesting.

Suppose Orders contains

Table: Orders

Now write

SELECT *
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
);

NULL introduces three-valued logic.

Depending on the query, comparisons involving NULL can produce unexpected results.

EXISTS doesn’t compare values directly.

It simply checks whether matching rows exist.

That’s why EXISTS avoids many NULL-related surprises.

EXISTS vs IN — Quick Decision Guide

Use EXISTS when:

  • Working with large tables
  • Checking whether at least one row exists
  • Using correlated subqueries
  • Optimizing performance

Use IN when:

  • Comparing against a small lookup table
  • Writing simple readable queries
  • The subquery returns relatively few values
  • Performance isn’t a concern

Common Interview Questions

Q1. Which is faster?

Answer:

There is no universal winner.

Modern query optimizers often rewrite both queries into similar execution plans.

However,

EXISTS generally performs better for large correlated searches because it stops after finding the first match.

Q2. Why do developers write SELECT 1 inside EXISTS?

WHERE EXISTS
(
SELECT 1
FROM Orders
)

Because EXISTS only checks whether rows exist.

It ignores the selected columns.

You can even write

SELECT *

and the result remains the same.

Using SELECT 1 simply makes the intent clear.

Q3. Can EXISTS return duplicate rows?

No.

EXISTS only returns rows from the outer query.

Duplicates depend on the outer table — not the subquery.

Q4. Can IN use multiple columns?

Yes, in some database systems using row constructors, but support varies by SQL dialect. In interviews, it’s safer to discuss single-column IN unless asked specifically about composite comparisons.

Common Mistakes

Mistake 1

Using EXISTS when a JOIN is actually needed.

Need customer names and order amounts?

Use JOIN.

Not EXISTS.

Mistake 2

Using IN on huge datasets unnecessarily.

This may increase memory usage and execution time depending on the optimizer.

Mistake 3

Ignoring indexes.

EXISTS performs best when the searched column is indexed.

Mistake 4

Confusing EXISTS with COUNT.

Instead of:

SELECT COUNT(*)

to check if data exists,

simply use EXISTS.

It avoids unnecessary counting.

Best Practices

  • Use EXISTS when checking for the existence of related records in large datasets.
  • Use IN for small lookup lists where readability matters.
  • Create indexes on columns used in subqueries.
  • Understand execution plans instead of assuming one operator is always faster.
  • Be mindful of NULL values, especially when using NOT IN.

Key Takeaways

  • EXISTS stops searching after finding the first matching row.
  • IN compares values against a list returned by the subquery.
  • Both can produce the same result but may perform differently depending on the execution plan and data volume.
  • EXISTS is generally preferred for large correlated subqueries.
  • IN is clean, readable, and often ideal for small lookup datasets.
  • Understanding the execution plan matters more than memorizing “EXISTS is always faster.”

Conclusion

At first glance, EXISTS and IN appear to be interchangeable. In many cases, they even return identical results. But writing efficient SQL isn’t just about getting the correct output — it’s about understanding how the database processes your query.

As your datasets grow from hundreds of rows to millions, these small choices can have a noticeable impact on performance. Knowing when to use EXISTS and when IN is sufficient is a skill that separates someone who can write SQL from someone who truly understands how SQL works.

In Part 5, we’ll explore Common Table Expressions (CTEs) — one of the cleanest and most powerful SQL features for simplifying complex queries, improving readability, and replacing deeply nested subqueries. We’ll cover real-world examples, recursive CTEs, interview questions, common mistakes, and performance considerations to help you write cleaner, more maintainable SQL.

Until then, keep practicing, stay curious, and remember: great SQL isn’t just about getting the right answer — it’s about writing queries that are clear, efficient, and easy to maintain.

If you found this article helpful, consider sharing it with your friends or colleagues, and don’t forget to follow this SQL Interview Series so you never miss the upcoming articles.

Happy Learning, and I’ll see you in Part 5! 🚀

--

--