RANK and DENSE_RANK difference in SQL - Time & Space Complexity
We want to understand how the time it takes to run ranking functions changes as the data grows.
Specifically, we ask: How does using RANK or DENSE_RANK affect the work done by the database?
Analyze the time complexity of these SQL queries using ranking functions.
SELECT employee_id, salary,
RANK() OVER (ORDER BY salary DESC) AS rank_position
FROM employees;
SELECT employee_id, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank_position
FROM employees;
These queries assign ranks to employees based on salary, showing two ways to handle ties.
Look for repeated work inside the ranking process.
- Primary operation: Sorting all rows by salary.
- How many times: Once per query, over all rows.
As the number of employees grows, the sorting work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 operations |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: Sorting work grows faster than the number of rows, roughly like n log n.
Time Complexity: O(n log n)
This means the time to assign ranks grows a bit faster than the number of rows because sorting is needed first.
[X] Wrong: "Ranking functions just scan rows once, so they run in linear time."
[OK] Correct: Ranking needs sorting to order rows, and sorting takes more than just scanning once.
Knowing how ranking functions scale helps you explain query performance clearly and confidently.
What if we added a filter to rank only employees in one department? How would that change the time complexity?
Practice
RANK() and DENSE_RANK() functions in SQL?Solution
Step 1: Understand how
RANK()works with tiesRANK()assigns the same rank to tied rows but skips the next rank numbers accordingly. For example, if two rows tie for rank 1, the next rank will be 3.Step 2: Understand how
DENSE_RANK()works with tiesDENSE_RANK()also assigns the same rank to tied rows but does not skip rank numbers. So after two rows tied at rank 1, the next rank will be 2.Final Answer:
RANK()skips rank numbers after ties,DENSE_RANK()does not skip numbers. -> Option BQuick Check:
Skipping ranks =RANK()[OK]
- Thinking both functions behave the same with ties
- Confusing which function skips rank numbers
- Assuming ranks are assigned randomly
DENSE_RANK()?Solution
Step 1: Identify correct
The correct syntax usesDENSE_RANK()syntaxDENSE_RANK() OVER (ORDER BY column)to rank rows based on a column.Step 2: Check each option
SELECT employee_id, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank FROM employees; uses the correct window function syntax. Options A, B, and D have syntax errors or misuse the function.Final Answer:
SELECT employee_id, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank FROM employees; -> Option CQuick Check:
Window function syntax = SELECT employee_id, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank FROM employees; [OK]
- Omitting OVER clause
- Passing column inside function parentheses
- Using wrong keywords like BY instead of ORDER BY
Scores with values:Player | Score
Alice | 100
Bob | 90
Carol | 90
Dave | 80What will be the output of this query?
SELECT Player, Score, RANK() OVER (ORDER BY Score DESC) AS Rank, DENSE_RANK() OVER (ORDER BY Score DESC) AS DenseRank FROM Scores;
Solution
Step 1: Assign
Alice has highest score 100, rank 1. Bob and Carol tie at 90, both get rank 2. Because of tie at rank 2, next rank is 4 for Dave.RANK()valuesStep 2: Assign
Alice is dense rank 1. Bob and Carol tie at 90, dense rank 2. Dave has next dense rank 3 (no skipping).DENSE_RANK()valuesFinal Answer:
[{"Player":"Alice","Score":100,"Rank":1,"DenseRank":1},{"Player":"Bob","Score":90,"Rank":2,"DenseRank":2},{"Player":"Carol","Score":90,"Rank":2,"DenseRank":2},{"Player":"Dave","Score":80,"Rank":4,"DenseRank":3}] -> Option AQuick Check:
RANK skips numbers, DENSE_RANK does not [OK]
- Assigning dense ranks with gaps
- Not skipping ranks in RANK after ties
- Mixing up rank numbers for tied rows
SELECT name, RANK() ORDER BY score DESC FROM players;
But it gives a syntax error. What is the problem?
Solution
Step 1: Check
RANK()function usageRANK()is a window function and must be followed byOVER()clause specifying order.Step 2: Identify missing
The query usesOVER()clauseRANK()withoutOVER(), causing syntax error.Final Answer:
Missing OVER() clause after RANK() function. -> Option AQuick Check:
Window functions need OVER() [OK]
- Forgetting OVER() clause
- Trying to put ORDER BY inside SELECT without OVER()
- Assuming RANK() takes parameters
Salesperson | SalesAmount
John | 500
Jane | 700
Joe | 700
Jill | 600You want to assign ranks so that tied salespeople share the same rank and the next rank number is not skipped. Which query correctly achieves this?
Solution
Step 1: Understand ranking requirements
Tied salespeople should share the same rank, and the next rank number should not skip any numbers.Step 2: Choose correct function
DENSE_RANK()assigns same rank to ties and does not skip ranks.RANK()skips ranks after ties.ROW_NUMBER()never assigns same rank to ties. SELECT Salesperson, SalesAmount, RANK() OVER (PARTITION BY SalesAmount ORDER BY SalesAmount DESC) AS Rank FROM sales; misuses PARTITION BY.Final Answer:
SELECT Salesperson, SalesAmount, DENSE_RANK() OVER (ORDER BY SalesAmount DESC) AS Rank FROM sales; -> Option DQuick Check:
DENSE_RANK() for no skipped ranks after ties [OK]
- Using RANK() when no skipping is wanted
- Using ROW_NUMBER() which never ties
- Incorrect use of PARTITION BY in ranking
Ready to prove it?
Pass the test and this pattern moves unknown
