
Answer-first summary for fast verification
Answer: SELECT TOP 5 CustomerID, SUM(TotalAmount) AS TotalSpent FROM Orders WHERE OrderDate >= DATEADD(year, -1, GETDATE()) GROUP BY CustomerID ORDER BY TotalSpent DESC;
Option A correctly selects the top 5 customers, sums their total amounts, and filters the orders to include only those from the last year. Options B, C, and D either do not limit the results to the top 5 customers or do not filter the date correctly.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You are working with a data warehouse in Fabric that contains a table named 'Orders' with columns 'OrderID', 'CustomerID', 'OrderDate', and 'TotalAmount'. You need to write a SQL query to find the top 5 customers who have spent the most in the last year. How would you structure this query?
A
SELECT TOP 5 CustomerID, SUM(TotalAmount) AS TotalSpent FROM Orders WHERE OrderDate >= DATEADD(year, -1, GETDATE()) GROUP BY CustomerID ORDER BY TotalSpent DESC;
B
SELECT CustomerID, SUM(TotalAmount) AS TotalSpent FROM Orders WHERE OrderDate >= DATEADD(year, -1, GETDATE()) GROUP BY CustomerID ORDER BY TotalSpent DESC;
C
SELECT TOP 5 CustomerID, SUM(TotalAmount) AS TotalSpent FROM Orders GROUP BY CustomerID ORDER BY TotalSpent DESC;
D
SELECT CustomerID, SUM(TotalAmount) AS TotalSpent FROM Orders GROUP BY CustomerID ORDER BY TotalSpent DESC;
No comments yet.