Walk me through a query that uses dense_rank, rank, and row_number on an employee table with 526 employee IDs. Provide examples and explain the results.
1Times asked
Aug 2026Last seen
Aug 2026First seen
💡 Model Answer
The three window functions serve different purposes:
- ROW_NUMBER() assigns a unique sequential number to each row in the partition, regardless of duplicates. Example: SELECT employee_id, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn FROM employees; If two employees have the same salary, they still get different rn values.
- RANK() assigns the same rank to ties but leaves gaps. Example: SELECT employee_id, RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees; If two employees tie for first, both get rank 1, and the next distinct salary gets rank 3.
- DENSE_RANK() assigns the same rank to ties but does not leave gaps. Example: SELECT employee_id, DENSE_RANK() OVER (ORDER BY salary DESC) AS drnk FROM employees; With two ties for first, both get rank 1, and the next distinct salary gets rank 2.
With 526 employee IDs, you can apply these functions to see how each ranking behaves. For instance, if salaries are 100k, 100k, 90k, 80k, then ROW_NUMBER() yields 1,2,3,4; RANK() yields 1,1,3,4; DENSE_RANK() yields 1,1,2,3. These differences are crucial when you need to group or paginate results.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500