Answer: Aggregate Window Function
Explore how to apply aggregate window functions to analyze sales data efficiently. Understand how to use SELECT queries, aliases, ORDER BY clauses, and window partitions. Learn alternative approaches like GROUP BY and CTEs to solve intermediate SQL interview problems.
We'll cover the following...
Solution
The solution is given below:
Code explanation
The explanation of the solution code is given below:
Lines 2–3: The
SELECTstatement selects the columnsMonthandTotal sales. TheTotal salescolumn retrieves the count of the product in each category using the aggregate window function. We useASto set an alias for the columns.Line 4: The data is retrieved from the
Salestable.Line 5: The
ORDER BYclause sorts the results by theMonthcolumn in custom order of months using theFIELD()function.
Recalling relevant concepts
We have covered the following concepts in this question:
Selective columns ...