Interview Q&A / SQL & DBMS

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What is the difference between a correlated and a non-correlated subquery?

SHORT ANSWER

A non-correlated subquery runs once and does not refer to the outer query. A correlated subquery refers to a column of the outer row, so it is evaluated once for each outer row, at least logically.

Explain it simply

Non-correlated is like asking, "What is the class topper's mark?" once and then comparing everyone to that number. Correlated is like asking, for each student, "What is the average mark in this student's own section?" The inner question changes with every outer row.

Example

Non-correlated, runs once:

sql
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Correlated, depends on the outer row e:

sql
SELECT e.name, e.salary
FROM employees e
WHERE e.salary > (
  SELECT AVG(salary)
  FROM employees
  WHERE dept_id = e.dept_id
);

The second query returns employees who earn more than the average of their own department. The inner query cannot run alone because e.dept_id is undefined without the outer query.

Optimizers often rewrite correlated subqueries into joins, so they are not always slow, but a poorly indexed correlated subquery on a big table can be.

What to say in the interview

"A correlated subquery references the outer query and is conceptually re-run per outer row. A non-correlated one runs once. For large data I check the plan or rewrite correlated logic as a join or window function."

Common mistakes

  • Assuming correlated subqueries are always slow, or always fine.

#sql#subquery#correlated-subquery

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.