r/SQL • • Jul 15 '26

SQL Server how to solve this ??

Consider a table named "Sales" with columns: SalespersonID, CustomerID, SaleDate. Write a SQL query to calculate the salesperson who made the highest number of sales each quarter.

0 Upvotes

20 comments sorted by

View all comments

Show parent comment

2

u/wittgenstein1312 Jul 15 '26

This is overkill

1

u/Yavuz_Selim Jul 15 '26 edited Jul 15 '26

Why?

Every step seems logical.

OP mentions quarter, so you calculate the quarter based on SaleDate.
I would've used ROW_NUMBER instead of DENSE_RANK - as that would arbitrarily pick 1 salesperson (I would say the request was to select one (only one) salesperson).
If there are multiple salespersons that have the highest sales, DENSE_RANK ranks them all as best.
The query ends with selecting the highest ranked salesperson (or possible salespersons).
(I also wouldn't have used an ORDER BY.)

 

So why overkill?