A reader recently asked me to help them solve a problem combining
SQL Subqueries provide a powerful means to combine data from one or more table into one result. They are sometimes called nested queries. As the name implies, they one or more queries, one inside one another other.
IF you’re unfamiliar them, then your first order of business should be to read our article Ultimate Guide to Subqueries. This is super place to start.
Subqueries are slippery and our guide help you get a handle on all the places to use a sub query in your SQL. If you are just looking to learn sub queries, then this is a must read.
If you looking for advanced topics then check out Correlated Subqueries in SQL to see how to write SQL that is more data driven, and doesn’t need to rely on hard coded values.
Or check out our case study on using a Join, Union, and Subquery all in one SQL Statement! This is based on question I received from a reader. It a good read, and give you good insight into my approach to solving problems as well as writing SQL.
All the examples for this lesson are based on Microsoft SQL Server Management Studio and the sample databases AdventureWorks and WideWorldImporters. You can get started using these free tools using my guide Getting Started Using SQL Server
Many times in a relational database the information you want to show in your query is in more than one table. This
SQL ANY and ALL keywords are used in subquery comparisons to compare a set of value against all values in the result
The SQL In operator compare a column to a list. In this article we’ll see how to use In and NOT IN
A derived table is a subquery in the from clause. It returns a table as its result. Unlike other subqueries you name
Correlated subqueries are used to tie an inner query’s criteria to values within the outer query. They are powerful technique to avoid
In this puzzle we’ll explore a SQL bill of materials example. Companies use a BOM (Bill of Materials) to itemize the components
Use both Joins and subqueries to query data from different tables. Though they may even share the same query plan, are many
SQL subqueries make it possible for you to write queries that are more dynamic, and data driven. Think of them as query within another
In this video we’ll walk you though how to use the IN operator with a Subquery; we’ll show you why you would
In this puzzle, we’re going to learn about joins vs subqueries. In many queries you can substitute joins and subqueries. Yet, since
In this puzzle, we’re going to learn how to rewrite a subquery using inner joins. Knowing about a subquery versus inner join
The SQL EXISTS operator is mostly used to test whether a subquery returns rows. It returns TRUE if rows exists in the