N020-E2 Tier 2 · Core SQL · easy hr · Helix Systems

Return the employee name and department name for every combination

Part of CROSS JOIN in SQL

The problem

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 employees table contains every active and former employee at Helix Systems.
  • The departments table 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_name and department_name.
Schema · hr 4 tables
departments
id integer
name text
location text
budget numeric
salaries
id integer
employee_id integer
amount numeric
effective_date date
end_date? date
employees
id integer
name text
email text
department_id integer
manager_id? integer
hire_date date
title text
is_active boolean
job_history
id integer
employee_id integer
title text
department_id integer
start_date date
end_date? date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
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 d is the operation. No ON clause, no condition. Every row in employees combines with every row in departments. If there are 30 employees and 5 departments, the result has 150 rows — one for every possible pairing.
  • e.name AS employee_name reads the employee's name from the left side of each paired row. The alias e disambiguates which name column is meant, since departments also has a column called name.
  • d.name AS department_name reads 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.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.