From the course: SQL Essential Training

Subqueries and aggregate functions - SQL Tutorial

From the course: SQL Essential Training

Subqueries and aggregate functions

Have you ever heard of tree hugging? Well, a subquery is kind of like that. It's one query hugging another. A subquery is simply one query written inside of another. When a query is wrapped inside of another, it's called a nested query. The wrapping is just an open and closed parentheses or brackets that surround the query. Let's say we were asked by WSDA management how each individual city was performing against the global average sales. Let's think about that. To respond to this request, we would need to display the global average sales amount and at the same time display the average sales per city. Management can then see what cities are on par, over, and underperforming. Subqueries are useful in this type of scenario. Instead of writing one query, for instance, to get the global average, then another query to get the average sales per city, we can just use a subquery that performs both operations at once. Let's begin our first look at subqueries by looking at how they are used with aggregate functions.

Contents