Sitemap
Varsha Vishwakarma

Varsha Vishwakarma

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

Correlated vs Non-Correlated Subqueries Explained with Real Interview Examples | SQL Interview Series #3

4 min readJul 8, 2026

--

Press enter or click to view image in full size

After learning the basics of SQL Subqueries, the next concept you’ll almost certainly encounter in interviews is Correlated vs Non-Correlated Subqueries.

This topic often confuses beginners because both involve nested queries, but they work very differently.

If you’re preparing for SQL, Data Engineering, or Backend Developer interviews, understanding the difference is essential.

In this article, we’ll explore both types of subqueries using practical examples, execution flow, interview questions, and real-world business scenarios.

What is a Non-Correlated Subquery?

A Non-Correlated Subquery is a subquery that can execute independently of the outer query.

It does not depend on any values from the outer query.

The database executes the inner query once, stores the result, and then the outer query uses that result.

Think of it as asking a question first and then using the answer later.

Example

Suppose we have an Employees table.

Employees Table

Table: Employees

Problem

Find employees whose salary is greater than the company’s average salary.

SQL Query

SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);

How SQL Executes This Query

Step 1

The database first executes the inner query.

SELECT AVG(Salary)
FROM Employees;

Result:

Average Salary = 57,500

Step 2

The outer query becomes:

SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary > 57500;

Result

Output

Notice that the subquery executed only once.

What is a Correlated Subquery?

A Correlated Subquery depends on values from the outer query.

Because of this dependency, the inner query executes once for every row processed by the outer query.

This makes correlated subqueries more powerful — but sometimes slower.

Example

Suppose every department has a different average salary.

We want to find employees earning more than the average salary within their own department.

Employees Table

Table: Employees

SQL Query

SELECT e.EmployeeName,
e.Department,
e.Salary
FROM Employees e
WHERE e.Salary >
(
SELECT AVG(Salary)
FROM Employees
WHERE Department = e.Department
);

Notice this line:

WHERE Department = e.Department

The subquery references e.Department from the outer query.

That’s why it’s called a Correlated Subquery.

Execution Flow

Imagine SQL processing the rows like this:

Employee: Alice

↓

Calculate average salary for IT

↓

Compare Alice’s salary

↓

Move to Charlie

↓

Calculate average salary for IT again

↓

Get Varsha Vishwakarma’s stories in your inbox

Join Medium for free to get updates from this writer.

Compare Charlie’s salary

↓

Move to Bob

↓

Calculate average salary for HR

↓

Compare Bob’s salary

The inner query keeps running repeatedly.

Key Differences

Non-Correlated Vs Correlated

Real-World Example

Imagine you’re working for an e-commerce company.

Non-Correlated Subquery

Find all products whose price is higher than the overall average product price.

The average price is calculated once.

Correlated Subquery

Find products priced higher than the average price within their own category.

Now the average changes for every category.

The subquery must execute repeatedly.

Performance Considerations

Many interviewers ask:

“Which is faster?”

Generally:

✅ Non-Correlated Subqueries are faster because the inner query executes only once.

Correlated Subqueries execute many times and can become expensive on large datasets.

However, modern database optimizers can sometimes rewrite correlated subqueries into more efficient execution plans.

Always analyze query performance rather than assuming one approach is universally faster.

Common Interview Questions

1. What is the biggest difference between correlated and non-correlated subqueries?

A correlated subquery depends on the outer query, while a non-correlated subquery does not.

2. Why are correlated subqueries slower?

Because the inner query may execute once for every row returned by the outer query.

3. Can correlated subqueries be replaced?

Sometimes.

Depending on the requirement, they can often be rewritten using JOINs or window functions for better performance.

4. Which one is asked more in interviews?

Both are common, but correlated subqueries are often used to assess deeper SQL understanding.

Common Mistakes

❌ Thinking every subquery is correlated.

❌ Forgetting that a correlated subquery references the outer query.

❌ Assuming correlated subqueries are always slower.

❌ Using correlated subqueries where a JOIN would be simpler.

Practice Questions

Question 1

Find employees earning more than the average salary of their department.

Question 2

Find customers who spent more than the average order value.

Question 3

Find products priced higher than the average price in their category.

Try solving these problems using both correlated and non-correlated subqueries where possible.

Key Takeaways

✔ A Non-Correlated Subquery executes independently.

✔ A Correlated Subquery depends on the outer query.

✔ Non-Correlated Subqueries usually execute once.

✔ Correlated Subqueries execute once for each outer row.

✔ Understanding execution flow is more important than memorizing definitions.

What’s Next?

In the next article of the SQL Interview Series, we’ll learn EXISTS vs IN, one of the most frequently asked SQL interview topics, with practical examples, performance comparisons, and real-world use cases.

Happy Learning! 🚀

--

--

Varsha Vishwakarma
Varsha Vishwakarma

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