Class 40 of 58 • Advance SQL Analytics

SQL Subqueries: Scalar, Multi-Row & Correlated Nested Queries

In advanced database development, answering complex business questions frequently requires multi-stage evaluations (e.g., "Find all students whose GPA is higher than the average GPA of their specific department"). A Subquery (or Nested Query) is a SELECT query embedded inside another SQL statement.

Nested Subqueries vs Modern Common Table Expressions (CTE)

Figure 4.1: Structural comparison between nested subqueries and modular Common Table Expressions (CTEs).


1. Scalar Subqueries (Returns Single 1×1 Value)

A scalar subquery evaluates to a single value. It can be placed in SELECT, WHERE, or HAVING clauses:

-- Find all students scoring above the university average GPA
SELECT student_id, first_name, gpa
FROM students
WHERE gpa > (
    SELECT AVG(gpa) 
    FROM students
);

2. Multi-Row Subqueries (IN, ANY, ALL)

When a subquery returns a column of multiple values, use operators like IN, ANY, or ALL:

-- Find all students enrolled in active Science or IT departments
SELECT student_id, first_name, course
FROM students
WHERE dept_id IN (
    SELECT dept_id 
    FROM departments 
    WHERE dept_name IN ('Engineering', 'Computer Science')
);

3. Correlated Subqueries (Row-by-Row Evaluation)

A Correlated Subquery references columns from the outer query. It cannot run independently; the database engine re-evaluates the inner subquery for every candidate row of the outer table:

-- Find students whose GPA exceeds the average of THEIR specific course
SELECT s1.first_name, s1.course, s1.gpa
FROM students s1
WHERE s1.gpa > (
    SELECT AVG(s2.gpa)
    FROM students s2
    WHERE s2.course = s1.course  -- Correlated outer link!
);

4. The EXISTS & NOT EXISTS Operators

EXISTS tests for the existence of rows returned by a subquery. It returns TRUE immediately upon encountering the first matching record (short-circuiting):

-- Find all departments that have at least one active student
SELECT d.dept_name
FROM departments d
WHERE EXISTS (
    SELECT 1 
    FROM students s 
    WHERE s.dept_id = d.dept_id
);

Next, let us learn how to replace deeply nested subqueries with clean, readable SQL Common Table Expressions (CTE)!


Frequently Asked Questions (FAQ)

A Non-Correlated subquery is completely independent of the outer query; it executes once, and its result is used by the outer query. A Correlated subquery references columns from the outer query, executing repeatedly once for every single candidate row evaluated by the outer query (O(N) or O(N^2) complexity).

EXISTS stops scanning as soon as it finds the very first matching row (short-circuit evaluation). IN typically evaluates the entire subquery result set before comparing.

A Scalar subquery returns exactly one single value (one row and one column), allowing it to be used wherever a single literal constant is permitted (e.g., in SELECT lists or WHERE comparisons).

Next Step in Your SQL Database Learning Path:

Proceed to the next class in sequence to build production mastery of relational database architecture.

Continue to Class 41: Common Table Expressions → Previous: ← UNION & UNION ALL | Full Syllabus


Related SQL Masterclass Lessons & Recommended Next Topics

Common Table Expressions (CTE) →

Modern, highly readable alternatives to deeply nested subqueries.

IN & EXISTS Subqueries →

Evaluating subquery membership and short-circuit optimizations.

Subqueries vs Joins →

Rewriting correlated subqueries as high-performance inner joins.

Window Functions vs Subqueries →

Eliminating subqueries in SELECT lists with window partitions.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!