A training coordinator at Helix Systems needs every possible pairing of an Engineering-department employee (department_id = 1) with every company department for a cross-functional assignment matrix.
Write a query to return the employee name and department name for every Engineering-employee × department combination.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - The
departmentstable contains every department; Engineering isdepartment_id = 1. - Each Engineering employee should appear once paired with every department, including Engineering itself.
Output:
- One row per Engineering-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
WHERE
e.department_id = 1 The shape
WHERE e.department_id = 1 narrows employees to the Engineering cohort, and CROSS JOIN departments d pairs each Engineering employee with every department in the company — including Engineering itself. The result is the cross-functional assignment matrix the training coordinator needs.
Clause by clause
FROM employees e CROSS JOIN departments dis the unconditional pairing. Every row inemployeescombines with every row indepartments.WHERE e.department_id = 1restricts the left side to Engineering employees. The integer1is the literal value ofdepartment_idfor Engineering; the filter reads the employee's own department from theemployeestable, not from the joineddepartmentsrow.e.name AS employee_namepulls the employee's name from the left side.d.name AS department_namepulls the department name from the right. Both source tables have anamecolumn, so the table aliases are what tell PostgreSQL which one each reference resolves to.
Why this and not INNER JOIN ... ON e.department_id = d.id
An INNER JOIN on department_id would return one row per Engineering employee paired with the Engineering department only — the existing assignment, nothing more. The matrix the coordinator is building needs every Engineering employee paired with every department, including the ones they could rotate into. CROSS JOIN produces that full grid by not matching on the department relationship at all. The Engineering filter is on the employee side; the department side is left wide open.
You practiced a CROSS JOIN where one side has been pre-filtered to a cohort. The shape generalises: any time one side is a cohort and the other is a complete catalogue, WHERE cohort_filter + CROSS JOIN produces the cohort-by-catalogue grid.