Knowing the gap difference isn't enough for FAANG interviews. See the answer that covers edge cases, performance, and real-world usage patterns.
Demonstrate the difference between DENSE_RANK() and RANK()
RANK() assigns the same rank to ties but skips subsequent ranks. For example: 1, 2, 2, 4. DENSE_RANK() assigns the same rank to ties but doesn't skip ranks. For example: 1, 2, 2, 3. ROW_NUMBER() assigns unique sequential numbers regardless of ties: 1, 2, 3, 4.
All three assign ranks, but the business context determines which to use:
sqlSELECT name, department, salary, RANK() OVER (ORDER BY salary DESC) as rank_val, DENSE_RANK() OVER (ORDER BY salary DESC) as dense_val, ROW_NUMBER() OVER (ORDER BY salary DESC) as row_val FROM employees;
| name | salary | RANK | DENSE_RANK | ROW_NUMBER |
|---|---|---|---|---|
| Alice | 120K | 1 | 1 | 1 |
| Bob | 120K | 1 | 1 | 2 |
| Carol | 100K | 3 | 2 | 3 |
sql-- Deduplication: keep latest record per user WITH ranked AS ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY user_id ORDER BY updated_at DESC ) AS rn FROM events ) SELECT * FROM ranked WHERE rn = 1; -- Top 3 products per category (no gaps) WITH ranked AS ( SELECT *, DENSE_RANK() OVER ( PARTITION BY category ORDER BY revenue DESC ) AS dr FROM products ) SELECT * FROM ranked WHERE dr <= 3;
Performance note: All three are computed in the same sort pass. The window ORDER BY determines the sort cost — adding PARTITION BY converts it to a partial sort (much cheaper on large tables).
The definition is table stakes. The interview-winning answer shows WHICH function solves WHICH business problem, with real query patterns (deduplication, top-N per group) that you've used in production.
Paste your answer and get instant AI-powered feedback with a FAANG-level improved version.
Analyze My Answer — Free3 free analyses per day. No sign-up required.