From the course: SQL Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Aggregated subqueries

Aggregated subqueries - SQL Tutorial

From the course: SQL Essential Training

Aggregated subqueries

- [Instructor] Now if you recall in our last chapter on functions, we used the group by statement to show the average subtotals of invoices by city. Now, what if we had a new ask about how each individual city was performing against the global average sales? Now, one way to answer this question would be to write a query that was able to display the average sales of each city right next to the global average. The query we write to display the average sales by billing city is identical to the one we wrote previously, with one exception. We also need to include a subquery in the select clause to calculate the global average. This way, we can compare the two values. Let's go ahead and build out our query. As we usually do, let's start with the from clause. And we want the invoice table. I'm going to go above the from and include our select. And let's now include the fields we want to see. We want to see the billing city.…

Contents