"Earns More Than Their Manager"

Concepts covered: sqlSelfJoin

The interviewer's follow-up: 'now compare each row to the previous one.' Two solutions work. The self-join, with the inequality moved into the join condition. The window function, with LAG over the ordered partition. Knowing both, and knowing when each wins, is the move. The two solutions, side by side Question: for each transaction, return the previous transaction's amount for the same account. Self-join solution: join transactions to itself on account_id, with the second copy filtered to the immediately preceding txn. Window function solution: LAG(amount) OVER (PARTITION BY account_id ORDER BY txn_date). The window function version is shorter, faster, and easier to read. The self-join version is longer because finding 'the immediately previous row' requires either a NOT EXISTS subquery o

About This Interactive Section

This section is part of the Self-Join: 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.