From the course: Practice It: Advanced SQL
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Check it: Ranking functions - SQL Tutorial
From the course: Practice It: Advanced SQL
Check it: Ranking functions
(sound logo hums) - [Instructor] So starting in line 30 in the FROM clause, we will be fetching our data from the Orders table. Then at line 26, beginning with our SELECT statement, we will select our CustomerID. And in order to calculate the total purchase made by each customer, we will use the operator SUM(Amount), and alias this selection as TotalPurchaseAmount. Then we'll utilize the RANK window function to assign the rank to each customer based on the total purchase amount. Including the ORDER BY SUM (Amount) DESC will rank each in descending order. We will then alias this selection as CustomerRank, and then group our results by customer IDs. We want to make sure our results are in the order of our results from the ranks that we made. So we will use ORDER BY CustomerRank in descending order. We will finish our query with a limit of three, so we only choose the top three customers. Even though we only have three customers in our data set anyways, it's just good practice to include…