SQL Joins SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Many-to-Many — Employees ↔ Projects via Junction Table

Follow employees through project_assignments to projects and return one row per assignment.

  • Joins
  • Sorting

Exercise brief

Understand the request

Project portfolio analyst A delivery roster needs the attributes stored on both sides of an employee-project many-to-many relationship.

List every employee-project assignment with the project's name and status. Many-to-many: an employee can be on multiple projects, a project can have multiple employees. Each assignment appears once. Return employee_id, first_name, last_name, project_id, project_name, status, allocation_pct — ordered by employee_id, project_id.

Return

  • Return employee_id, first_name, last_name, project_id, project_name, status, allocation_pct in this exact left-to-right order.

Constraints

  • Use the junction table as the relationship path.
  • Do not join employees directly to projects.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • first_nameVARCHAR(50)
  • last_nameVARCHAR(50)

project_assignments

  • employee_idINTEGER
  • project_idINTEGER
  • allocation_pctINTEGER

projects

  • project_idINTEGER
  • project_nameVARCHAR
  • statusVARCHAR

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Many-to-many shape: entity A → junction → entity B. THREE tables, TWO joins.

Hint 2

The junction table's PK is usually the COMPOSITE (employee_id, project_id) — preventing duplicate assignments.

Hint 3

Number of result rows = number of rows in the JUNCTION (12 here), NOT (employees × projects).

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT e.employee_id, e.first_name, e.last_name, p.project_id, p.project_name, p.status, pa.allocation_pct FROM employees e INNER JOIN project_assignments pa ON e.employee_id = pa.employee_id INNER JOIN projects p ON pa.project_id = p.project_id ORDER BY e.employee_id, p.project_id;

Why this works

Many-to-many is the third great relational primitive after one-to-one and one-to-many. The junction table holds the relationship; relationship attributes (start date, role, allocation) live there too. Always join entity-A → junction → entity-B in that order.

Success check

All 12 assignments appear exactly once with their employee and project attributes.

Expected result

Use this output to verify values, aliases, ordering, and row count.

employee_idfirst_namelast_nameproject_idproject_namestatusallocation_pct
100JohnSmith1ApolloActive50
100JohnSmith2BoltActive50
101AliceJohnson1ApolloActive80
102BobWilson1ApolloActive60
102BobWilson3CometCompleted40
103CarolDavis4DawnPlanned100
104DavidBrown2BoltActive70
105EmmaTaylor1ApolloActive30
105EmmaTaylor2BoltActive40
105EmmaTaylor3CometCompleted30

Previewing 10 of 12 expected rows. Run the query in the editor to inspect the full result.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.