Helix Systems' HR team is building a cross-functional training matrix and needs every possible pairing of an employee with a department — independent of whether the employee actually belongs to that department.
Write a query to return the employee name and department name for every combination.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - The
departmentstable contains every department. - Every employee should appear once paired with every department, including the department they're already assigned to and every other one.
Output:
- One row per employee-department combination, with columns
employee_nameanddepartment_name.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
e.name AS employee_name,
d.name AS department_name
FROM
employees e
CROSS JOIN departments d The shape
CROSS JOIN pairs every employee with every department, including the department they already belong to. The HR team's training matrix doesn't care about current assignments; it needs the full set of possible pairings so any cross-functional combination can be reviewed.
Clause by clause
FROM employees e CROSS JOIN departments dis the operation. NoONclause, no condition. Every row inemployeescombines with every row indepartments. If there are 30 employees and 5 departments, the result has 150 rows — one for every possible pairing.e.name AS employee_namereads the employee's name from the left side of each paired row. The aliasedisambiguates whichnamecolumn is meant, sincedepartmentsalso has a column calledname.d.name AS department_namereads the department name from the right side and labels it for the output.
Why this and not an INNER JOIN on department_id
The instinct from the previous node is to write JOIN departments d ON e.department_id = d.id. That would return every employee paired with their own department — one row per employee, the actual assignment. The training matrix needs the opposite: every employee paired with every department they could be assigned to. CROSS JOIN produces that grid by ignoring the relationship column entirely. The departmental assignment is irrelevant to the pairing.
You practiced producing a many-to-many enumeration where no relationship exists in the data. CROSS JOIN doesn't read any link between the tables — it simply produces every possible pair, which is exactly what a "who could potentially be paired with whom" question wants.