Interview Question

What is ROW_NUMBER()?

ROW_NUMBER gives each ordered row a distinct sequence number.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

ROW_NUMBER assigns consecutive numbers to rows within each window partition. • The numbering follows the window ORDER BY. • Tied sort values still receive different row numbers. • Add deterministic tie-breaker columns when stable numbering matters.

💡 SQL Example

SELECT name, ROW_NUMBER() OVER (ORDER BY salary DESC, employee_id) AS position FROM employees;

Result

Each employee receives a unique position

⚡ Quick Revision

ROW_NUMBER gives each ordered row a distinct sequence number.