r/learnSQL • • Oct 11 '26

Day 34 — 8 more Hard JOIN questions (93/100)

Finished 8 more Hard questions from my 100-question lab today. Some of the ones I worked through:

  • Pairs of employees who are in different departments but work on the same project together. I self-joined assignments on project_id and used a1.emp_id < a2.emp_id to stop mirrored duplicates (A-B and B-A), then joined to employees twice and filtered e1.dept_id <> e2.dept_id
  • Departments ranked by average hours worked per employee, which chains departments → employees → assignments and aggregates by department
  • Employees whose manager works in a different department than they do, a self-join on employees comparing dept_id on both sides

The < trick for removing mirrored pairs was new to me today. I’d been using DISTINCT for that kind of thing, and this is cleaner.

7 left. Does anyone have a favorite pattern for ranking queries in MySQL? I haven’t done window functions properly yet and wondering if they’d make the “ranked by” questions easier.

20 Upvotes

0 comments sorted by