Filtering rn <= N
Concepts covered: sqlSubqueryCorrelated
Most interviewers let you use window functions. Some do not. If you hear 'now solve it without window functions,' the interviewer is testing whether you understand the underlying mechanics or whether you just memorized ROW_NUMBER. Two fallback approaches work. Know both. The correlated subquery is more common in interviews. The self-join is more common in older codebases. The correlated subquery approach Idea: for each row, count how many other rows in the same group have a higher value. If that count is less than N, this row is in the top N. The counting happens in a correlated subquery that runs once per outer row. Read this carefully. For each row e1, the subquery counts how many e2 rows share the same department and have a strictly greater salary. If exactly zero rows beat e1, e1 is ra
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.