Deterministic Tiebreaks for Exactly N

Concepts covered: sqlSubqueryCorrelated

At this level the interviewer's no-window fallback is less about whether you remember the syntax and more about whether you understand what the optimizer does with each form. Two fallbacks work. The correlated subquery is the cleaner write. The self-join is the form that lets you reason about the join algebra directly. Knowing when each one's plan diverges is the depth signal. The correlated subquery fallback The semantics: for each row e1, count how many rows in the same department strictly exceed its salary. If fewer than 3 rows beat it, e1 is in the top 3. Ties behave like RANK: if two employees share second place, neither has a strictly-greater predecessor, so both pass through the filter as ranks 2 and 2 (not 2 and 3). This is identical to RANK <= 3, not ROW_NUMBER <= 3. If the consum

About This Interactive Section

This section is part of the Top N Per Group: Intermediate 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.