Correlated Subquery

SQL · SQL Subqueries

Theory

A correlated subquery refers to a column of the outer query, so its result depends on the current outer row. In WHERE salary > (SELECT AVG(salary) FROM employees i WHERE i.dept = e.dept), the inner AVG is computed for the department of each employee e. Logically it re-runs once per outer row: Asha is compared with the IT average, Ravi with the HR average, and so on.

This makes it a natural fit for "compare each row with its own group" questions, and for EXISTS / NOT EXISTS checks. Aliases are essential to show which table each column belongs to; a missing alias can silently correlate to the wrong table. Run naively, it costs one inner query per outer row, although modern optimisers often rewrite it into a join. When performance matters, a JOIN to a grouped subquery or a window function like AVG(salary) OVER (PARTITION BY dept) is usually faster.

Why we use it

Some comparisons are relative to each row's own context: above the department average, latest order per customer, price higher than the category average. A correlated subquery expresses this directly.

When to use it

Use correlated subqueries for per-row comparisons and EXISTS checks, especially on small or well-indexed data. For large tables, prefer window functions or a join to a pre-aggregated table.

How it works

  1. Take an outer row (employee e)
  2. Pass e.dept into the inner query
  3. Inner query computes the average for that dept
  4. Is e.salary greater than that average? Yes: Keep the outer row. No: Discard it.
  5. Move to the next outer row and repeat
  6. Return kept rows

Syntax

SELECT columns FROM t1
WHERE column = (SELECT ... FROM t2 WHERE t2.id = t1.id);

Example

SELECT name FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic