Lateral / CROSS APPLY Top-N Per Row
Concepts covered: sqlSelfJoin
At this level the no-window question becomes a probe for whether you understand the optimizer's behavior across engines. The two fallback forms are familiar: correlated subquery and self-join. The senior conversation is about which one the engine actually plans well and what it costs when the plan goes wrong. The correlated subquery plan, by engine On a modern engine, the correlated subquery 'WHERE (SELECT COUNT(*) FROM e2 WHERE ... ) < 3' is rewritten by the optimizer into a hash-based set operation. The engine builds a hash of (department, salary) pairs, then evaluates each outer row against the hash. This plan is O(N) with a constant factor for the hash build. On older engines (Postgres < 12, MySQL < 8, Hive 1.x), the rewrite is not always performed, and the subquery executes literally:
About This Interactive Section
This section is part of the Top N Per Group: Advanced 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.