Skip to main content
Intermediatesql query Verified

What are subqueries in SQL? Explain correlated vs non-correlated subqueries with examples.

ReportedSource: GeeksforGeeks interview experience: Cognizant GenC 2024

Quick answer

Subquery = query inside another. Non-correlated runs once (salary > company average). Correlated references the outer row (salary > own department's average); often rewritten as a JOIN or window function for speed.

Constraints

  • Input: the setup SQL (CREATE TABLE employees plus INSERT statements).
  • Output columns in order: name, salary, department_id.
  • A department with one employee has average equal to that salary, so nobody there qualifies. Employees with NULL salary never qualify.

Sample I/O

in: CREATE TABLE employees (id INTEGER, name TEXT, salary INTEGER, department_id INTEGER); INSERT INTO employees VALUES (1,'Ada',100,1),(2,'Bob',200,1),(3,'Cy',300,1),(4,'Dee',50,2),(5,'Eli',70,2);

out: Cy 300 1 Eli 70 2

in: CREATE TABLE employees (id INTEGER, name TEXT, salary INTEGER, department_id INTEGER); INSERT INTO employees VALUES (1,'A',100,1),(2,'B',100,1);

out:

in: CREATE TABLE employees (id INTEGER, name TEXT, salary INTEGER, department_id INTEGER); INSERT INTO employees VALUES (1,'Zoe',500,1),(2,'Amy',500,1),(3,'Low',10,1);

out: Amy 500 1 Zoe 500 1

sql
Loading editor…
Result

Sign up to run your code in the sandbox.

Sign up to run code and get an AI review

You can read and edit the challenge freely — running it in the sandbox and getting AI feedback need a free account.

Sign up free

Hints

Reference solution

A subquery is a query nested inside another statement (in WHERE, FROM or SELECT).

Non-correlated subquery runs once, independently of the outer query.

query.sql
-- employees earning more than the company average
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Correlated subquery refers to the outer row, so it is evaluated per outer row (logically).

query.sql
-- employees earning more than their own department's average
SELECT e.name, e.salary, e.department_id
FROM employees e
WHERE e.salary > (SELECT AVG(salary)
                  FROM employees x
                  WHERE x.department_id = e.department_id);

Other forms. IN (subquery), EXISTS (subquery) (stops at the first match, good for existence checks), derived tables in FROM, scalar subqueries in SELECT.

Performance. Correlated subqueries can be slow on large tables; rewrite with a JOIN to a grouped derived table, or a window function: AVG(salary) OVER (PARTITION BY department_id). Optimisers often rewrite them automatically, but check with EXPLAIN.

See the concept: Join fan-out

Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Discussion