Writing the Self-Join Condition
Concepts covered: sqlSelfJoin
The interviewer will give you a question that sounds slightly different: 'find pairs of employees in the same department.' Now the comparison is symmetric. Alice paired with Bob is the same pair as Bob paired with Alice. If you write the obvious join, you get every pair twice, plus every employee paired with themselves. The fix is a single inequality in the join condition. Most candidates miss it the first time. The interviewer is watching for it. The bug, stated plainly The fix: the < condition Add the condition e1.employee_id < e2.employee_id to the ON clause. This guarantees three things at once. First, the inequality is strict, so e1 cannot equal e2 (no self-pairs). Second, only one of (Alice, Bob) and (Bob, Alice) survives, because if Alice's id is smaller, the pair appears as (Alice,
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.