Writing and Using Subqueries

Introduction

A subquery is simply a SELECT statement nested inside another SQL statement. Instead of running one query, writing down the result, and then typing a second query that uses that result, you let the database do both steps at once. The inner query runs and hands its result to the outer query, which uses it as a value, a list of values, or even a temporary table.

Where you place the subquery determines what it does. In the WHERE clause, a subquery supplies values to filter on, often with operators such as =, IN, >, or ALL/ANY. In the FROM clause, it acts as a derived table — a result set you can join to or filter like any other table. In the SELECT clause, a scalar subquery returns exactly one value per row, useful for adding a computed column such as a count or an average. You will also meet correlated subqueries, which reference a column from the outer query and therefore re-evaluate for each outer row, and the EXISTS / NOT EXISTS operators, which test whether a subquery returns any rows at all.

As you watch the videos below, pay attention to three things: where the subquery sits in the statement, how many rows and columns it is allowed to return in that position, and whether it depends on the outer query (correlated) or can run on its own (non-correlated). Try pausing and predicting the result before the presenter reveals it, and notice when a subquery could have been written more simply as a JOIN — that judgment is part of good practice.

Video 1: When to Use a Subquery in SQL

Database Star · 8:50 · 59,026 views · 4y ago · captions — Watch on YouTube

About this video

Database Star explains what a subquery is and, more importantly, when reaching for one makes sense. The video works through a straightforward query and then transforms it into a subquery version, so you can see the before-and-after side by side. Expect discussion of the reasoning behind choosing a subquery over an alternative formulation.

Why this video

It frames the 'What Is a Subquery?' and 'Best Practices' subtopics directly, and Database Star is a well-established SQL teaching channel known for clear, accurate explanations aimed at learners.

Video 2: Subquery in SQL || SQL Subquery tutorial with examples

Coding Glitz · 18:38 · 147,622 views · 4y ago · captions — Watch on YouTube

About this video

A longer tutorial that walks through SQL subqueries step by step with worked examples, including the distinction between single-row and multi-row subqueries. The extended runtime allows the presenter to demonstrate several query patterns rather than just one, which helps you see how subquery results feed the outer statement.

Why this video

The single-row versus multi-row distinction is exactly what governs which operators you can use in the WHERE clause, making this a practical companion to the placement subtopics and to writing subqueries you can run yourself.

Video 3: SQL EXISTS

Learn SQL Fast: Dr. Cecelia Allison · 3:02 · 1,326 views · 3y ago · captions — Watch on YouTube

About this video

A short, focused demonstration of the SQL EXISTS operator in SQL Server, showing how EXISTS tests whether a subquery returns any rows rather than comparing values. At three minutes it is a targeted reference clip rather than a full lesson, so use it alongside the longer videos.

Why this video

EXISTS and NOT EXISTS are their own subtopic and are easy to confuse with IN; this concise clip isolates the operator so the behavior is clear, and it naturally illustrates the correlated subquery pattern that EXISTS usually relies on.

How These Videos Support the Learning Outcomes

Together these three videos target MO5 most directly: students see subqueries defined, placed in different clauses, and evaluated for whether they return one row or many, which is the core skill of composing a subquery to solve a multi-step retrieval problem in a single statement. Because subqueries in the WHERE clause depend on comparison and membership operators such as =, IN, and EXISTS, the videos also reinforce the filtering skills in MO2, and the comparisons between subquery and non-subquery formulations connect back to the JOIN decisions in MO1. Viewing them in order — concept and purpose, worked examples, then the EXISTS special case — builds from understanding to construction.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.