Pragmatic Works Nerd News

SQL Inception with Subqueries

Written by Austin Libal | Aug 09, 2026

In this tutorial, Austin Libal, a trainer at Pragmatic Works, walks through the essential concept of subqueries in SQL, demonstrating practical applications and examples. This guide is perfect for anyone looking to refine their SQL skills and understand how subqueries can be used to enhance query functionality.

 

What is a SQL Subquery?

A subquery is a query embedded within another query. It allows you to retrieve data from one or more tables and use the result set temporarily within the outer query. There are several key ways subqueries can be utilized:

  • As a filter in the WHERE clause.
  • In JOIN conditions to combine data.
  • To generate computed columns or summary values in a SELECT statement.

Using Subqueries in SQL Server Management Studio

To demonstrate subqueries in action, Austin uses SQL Server Management Studio (SSMS) and the AdventureWorks 2016 database. The example illustrates filtering customers who have made purchases.

Here’s how the subquery works in this case:

  1. Start by selecting from the sales.customer table to retrieve customer data.
  2. Instead of hard-coding a list of customer IDs, use a subquery to find which customers have made purchases. This subquery selects the customer_id from the sales.sales_order_header table, effectively providing the list of customers who have made purchases.

When you run this query, the result will filter customers who have placed orders, removing the need for manually specifying customer IDs.

Advanced Example: Nested Subqueries

In addition to basic subqueries, Austin also demonstrates how to use nested subqueries to filter products ordered in a specific year.

The steps include:

  1. Start by selecting products from the production.products table.
  2. Next, use a subquery to filter products that have been sold, selecting from the sales.sales_order_detail table.
  3. To refine the data further, a second subquery filters sales by the year using the YEAR() function on the order_date field from the sales.sales_order_header table.

This example showcases how multiple subqueries can be used to drill down into very specific data, such as determining which products were sold in a particular year.

Why Use Subqueries?

Subqueries are a powerful tool in SQL for breaking down complex queries into manageable components. They allow for:

  • Dynamic filtering without the need to manually hard-code values.
  • Improved readability and maintainability of queries by separating logic.
  • More flexible data retrieval from multiple related tables.

Subqueries vs. Other SQL Techniques

In future videos, Austin plans to compare subqueries with other SQL techniques like Common Table Expressions (CTEs) and temporary tables, discussing when and why to use each method for optimal query performance.

Stay Tuned for More Training!

For more SQL tips and tricks, be sure to check out Pragmatic Works’ on-demand learning platform, where you can access extensive content on T-SQL and many other topics.

Don't forget to like, subscribe, and stay tuned for upcoming tutorials that will help you master SQL and other database skills.

Don't forget to check out the Pragmatic Works' on-demand learning platform for more insightful content and training sessions on SQL and other Microsoft applications. Be sure to subscribe to the Pragmatic Works YouTube channel to stay up-to-date on the latest tips and tricks.