In these lessons, you'll learn about the advanced types of sub queries in school. And in this lesson, you'll gain experience writing sub queries that reference values from the outer query. These are known as correlated sub queries. Let's review the generic syntax for this type of query. First, we use a select statement to define the columns we want in our results. Set. These columns will come from the table or the outer part of our query. Then we'll use the WHERE clause to set up our conditional operator or the logic we want to define. And here is where correlated sub queries differ from traditional ask. Well, with a correlated sub query we can have more complex logic because we can use an entire query to define our condition. For our first example, let's find the employees with salaries that are higher than the average salary within their department. So for this query, we'll return the employee ID as well as their salary from the employees table, which we've alias as e. We'll use our conditional statement to say where the salary is greater than and now we can use our inner query or sub query to define our more complex logic. So in order to meet the requirements of our use case, we need to find the average. So we'll use the average aggregate function. Again, we'll use the employees table, which is totally fine. But please note we don't have an alias here to distinguish between the two. And then here is where our logic really ties together. We're using the department ID, but one from the inner query as well as one from the outer query. Let's go ahead and run this query by pressing the play button up here and we can see in our output here the employee IDs and their salary that are higher than the average within their department. Next, let's use our orders table to find the latest order for each customer. So here we're going to select star or select all columns from the orders table, which is Alias as O. Our conditional statement is where the order date is equal to. And again in our inner query, we can define our more complex logic. Since we want to find the latest order for each customer, we'll use the max aggregate function around the order date column from the same table, but with no alias. And again where these two values meet on the customer ID. But distinguishing between the inner and outer parts of our query, this will essentially create a subset or sub queries that meet certain conditions. And we want to find the roads that match our set condition. So let's go ahead and run this by pressing the play button and we can see here for each customer all the columns as well as the max order date for each customer. Now, since this is an advanced course, let's dive a bit deeper here by calculating running total of orders for our products Table. So in our Select statement or a result set, we want to return the product name and price from our products table as well as the order date from our orders table. And now, since we want a running total or essentially a timeline for our orders, we're going to go ahead and use as some price as well as over a keyword to create a window function. Within our window function, we want to partition or split by the product ID. And since we want this in chronological order, we'll go ahead and order by the order date and we'll go ahead and define all this logic with an alias of running underscore total. Now we just need to reference the associated tables. So we'll use our product table, which is Alias as P join into our order tables, which is alias as O. In the link between these two tables is the product ID column. Last, we're going to order by or organize our data based on the product name and the order date. Let's go ahead and run this by pressing the play button. And if we see here, this is broken down by each product and it shows in chronological order of the purchases correlated, some queries provide us with the massive amount of flexibility and complexity with our analysis. But please be cautious because these queries run a full table scan on each row in the outer query. My recommendation would be to start with one piece here logic and slowly build up complexity of testing the performance of your queries operation. Thanks for watching. Stay tuned for the next lesson. Rebuild Scale our sub queries to return a single value.