Avoiding the (a,b)/(b,a) Duplicate
Concepts covered: sqlSelfJoin
Symmetric pair questions ('find customers in the same region as another customer') are where the duplicate-pair bug ships most often. The naive query returns each pair twice plus every self-pair, inflating the row count by 2N + N². The fix is one inequality, but the choice of which column to use as the tiebreaker has implications for correctness, cardinality, and deduplication semantics. The bug and the standard fix Choosing the column and the direction Two decisions sit underneath the inequality. First: the column. The inequality column must be unique per row. customer_id is the standard choice because it is the table's primary key; order_id, transaction_id, email, anything-with-a-uniqueness-constraint works. Using a non-unique column for the inequality breaks the deduplication: if two cu
About This Interactive Section
This section is part of the Self-Join: 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.