From the course: Querying Microsoft SQL Server 2019

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use a subquery in a WHERE clause

Use a subquery in a WHERE clause - SQL Server Tutorial

From the course: Querying Microsoft SQL Server 2019

Start my 1-month free trial

Use a subquery in a WHERE clause

- [Narrator] SQL sub-queries can also be used in the WHERE or the HAVING clause of a SELECT statement. This will allow you to filter your rows based off of a dynamic calculation that's kept up to date as the data in your database changes. I want to start by reviewing the raw data that's stored in the SalesOrderDetail table. You'll recall that this table is structured in a way that each sales order number has a number of line items. For instance, sales order 43659 is made up of all the lines starting here on line number one and coming down to line number 12. To get the total value of the sales order we can add an aggregate function to get the sum total of the lines and group them based off of the SalesOrderID number. The value of each line is going to come out of a column called LineTotal. So let's create a SELECT query that'll find the total value of each sales order. We'll start by selecting the SalesOrderID column and…

Contents