39. Coworker Pairs in the Same Department
HardJoinsself-joinpair generationdeduplicate A-B/B-A
Finding pairs that share a property is a self-join classic. The gotcha is avoiding two kinds of junk: pairing a person with themselves, and listing each pair twice (A-B and B-A).
Task: find every pair of distinct employees who work in the same department, listing each unordered pair once. Use the name < name trick to keep only one ordering of each pair and to drop self-pairs in a single condition.
Output columns, in this exact order: employee_a, employee_b, dept. Order by dept ascending, then employee_a ascending, then employee_b ascending.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.