Subqueries Archives

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 Introduction to Subqueries.  This is super place to start.

They can be used in many places throughout the SELECT statement.

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

Joins versus Subqueries SQL Puzzle

Joins versus Subqueries

In this puzzle, we’re going to learn about joins versus subqueries.  In many queries you can substitute joins and subqueries.  Yet, since each has their strengths, it isn’t wise to do so.  Once you have worked through this puzzle you see some of the benefits and disadvantages of joins versus subqueries. Solving puzzles is a […]

Continue reading
2

Is it possible to use Joins, Subqueries, and Unions in one Query?

A reader recently asked me to help them solve a problem combining data from multiple tables into a single result.  What was interesting was that the solution used the three ways to combine data that we have recently covered, namely:  JOINS, set operators, and subqueries. Given the comprehensive nature of this problem I thought it […]

Continue reading

Get Ready to Learn SQL Server 23: Using Subqueries in the HAVING Clause

HAVING Clause Subquery

This is the fifth in a series of articles about subqueries.  In this article we discuss subqueries in the HAVING clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools using […]

Continue reading
2

Using A Subquery in the FROM clause

Derived Tables in From Statement

This is the fourth in a series of articles about subqueries.  In this article we discuss using a subquery in the FROM clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools […]

Continue reading

Using Subqueries in the WHERE Clause

Sub query in Where Clause

This is the third in a series of articles about subqueries.  In this article we discuss subqueries in the WHERE clause.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools […]

Continue reading
8

Using Subqueries in the Select Statement

Subqueries in Select Statement

This is the second in a series of articles about subqueries.  In this article we discuss subqueries in the SELECT statement’s column list.  Other articles discuss their uses in other clauses. All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these […]

Continue reading
4

Learn SQL – Introduction to Subqueries

Introduction to Sub Queries

The purpose of this article is to introduce you to subqueries and some of their high-level concepts.  There are a lot of details to cover in order to learn sub queries, but you’ll see we cover those in depth in later articles. All the examples for this lesson are based on Microsoft SQL Server Management […]

Continue reading