PARTITION BY Plus ROW_NUMBER

Concepts covered: sqlRankDenseRank

The interviewer will ask 'what if two employees have the same salary?' This is the tie question, and it is the most common follow-up on top N per group. The right answer depends on what 'top 3' means in the business context. ROW_NUMBER gives one answer. RANK gives another. DENSE_RANK gives a third. Picking the wrong one produces wrong row counts and a wrong report. ROW_NUMBER: always exactly N RANK: ties get the same rank, then a gap RANK assigns 1, 2, 2, 4, ... If two employees tie for second, both get rank=2 and the next employee gets rank=4. Rank=3 is skipped. Filtering rank <= 3 gives you 3 rows in this case (positions 1, 2, 2). But if the tie were larger, you would get more rows than expected. Use RANK when the business says 'top 3 including ties.' DENSE_RANK: ties get the same rank,

About This Interactive Section

This section is part of the Top N Per Group: Beginner lesson on DataDriven, a free data engineering interview prep platform. Each section includes explanations, worked examples, and hands-on code challenges that execute in real time. SQL queries run against a live database. Python runs in a sandboxed Docker container. Data modeling problems validate against interactive schema canvases. All content is framed around what data engineering interviewers actually test at companies like Meta, Google, Amazon, Netflix, Stripe, and Databricks.

How DataDriven Lessons Work

DataDriven combines four interview rounds (SQL, Python, Data Modeling, Pipeline Architecture) with adaptive difficulty and spaced repetition. Easy problems get harder as you improve. Weak concepts resurface until you master them. Your readiness score tracks progress across every topic interviewers test. Every lesson section ends with problems you solve by writing and running real code, not by picking multiple-choice answers.