When I teach SQL Server Development/T-SQL courses, subqueries is usually one of those topics that students find confusing. Usually it’s a head scratcher, although there are some who just get it.
So let’s try to demystify subqueries.
Very simply put, a subquery is just a query within a query. It’s a SELECT inside another SELECT.
Now let’s break it down. Let’s start with understanding what a query gives you first.
Understanding results of regular queries
Before we can understand subqueries, we need to understand first the different results we get from regular queries. A regular query always results in a dataset. It gives you the following variations of results:
or Column List
or Single List of Values