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
WHEREclause. - In
JOINconditions to combine data. - To generate computed columns or summary values in a
SELECTstatement.
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:
- Start by selecting from the
sales.customertable to retrieve customer data. - Instead of hard-coding a list of customer IDs, use a subquery to find which customers have made purchases. This subquery selects the
customer_idfrom thesales.sales_order_headertable, 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:
- Start by selecting products from the
production.productstable. - Next, use a subquery to filter products that have been sold, selecting from the
sales.sales_order_detailtable. - To refine the data further, a second subquery filters sales by the year using the
YEAR()function on theorder_datefield from thesales.sales_order_headertable.
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.
Sign-up now and get instant access
ABOUT THE AUTHOR
Austin is a Jacksonville native who graduated from The Baptist College of Florida in 2012. He previously worked as a manager in the retail service industry. He enjoys spending time with his wife and two kids. His primary focus at Pragmatic Works is on Azure Synapse Analytics and teaching the best practices for data integration, enterprise data warehousing, and big data analytics. He also enjoys helping customers learn the ins and outs of Power BI and showing people new ways to grow their business with the Power Platform.
Free Community Plan
On-demand learning
Most Recent
private training

Leave a comment