Tech
ROW_NUMBER vs RANK vs DENSE_RANK: one of them silently duplicates your Top-N
I was reviewing a "top 3 products per category" report last week and found a bug that had been live for months. One category showed four products. Another showed the same product twice.
The query used RANK() . It should have used ROW_NUMBER() .
They look interchangeable in a tutorial and behave completely differently the moment your data has ties.
The setup
Here is a small sales table with an obvious tie — two products in the same category sold 120 units:
CREATE ...
Read the full discussion on Dev.to
This article was aggregated from Dev.to. Click to join the conversation.
View on Dev.to