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

Keep Unassigned Employees in an As-Of Project Roster

Return every employee and any project assignment active on '2024-07-01'.

  • Joins
  • Date analysis
  • NULL handling
  • Sorting

Exercise brief

Understand the request

Portfolio operations manager A July 1 staffing snapshot must retain employees with no active project assignment.

Find pairs (employee, project_assignment) only when BOTH the employee_id matches AND the assignment is currently in effect (start_date <= '2024-06-30' AND (end_date IS NULL OR end_date >= '2024-06-30')). Return employee_id, first_name, last_name, project_id, allocation_pct, start_date, end_date — ordered by employee_id, project_id.

Return

  • Return employee_id, first_name, last_name, project_id, allocation_pct, start_date, and end_date.
  • Use NULL assignment fields for employees without an active assignment.
  • Order by employee_id and project_id.

Constraints

  • Use LEFT JOIN.
  • Keep all assignment date predicates inside ON so unmatched employees survive.

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
  • start_dateDATE
  • end_dateDATE

Hints, when you need them

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

Hint 1

A right-table filter in WHERE removes the NULL-padded rows produced by LEFT JOIN.

Hint 2

Put the employee key and both effective-date tests in the ON clause.

Hint 3

LEFT JOIN project_assignments pa ON e.employee_id = pa.employee_id AND pa.start_date <= '2024-07-01' AND (pa.end_date IS NULL OR pa.end_date >= '2024-07-01')

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, pa.project_id, pa.allocation_pct, pa.start_date, pa.end_date FROM employees e LEFT JOIN project_assignments pa ON e.employee_id = pa.employee_id AND pa.start_date <= '2024-07-01' AND (pa.end_date IS NULL OR pa.end_date >= '2024-07-01') ORDER BY e.employee_id, pa.project_id;

Why this works

Outer-join predicate placement is part of the result contract. Evaluating the date range in ON decides which assignments match while still emitting a NULL-padded row for every employee without a match.

Success check

All ten employees appear; employees 107, 108, and 109 have NULL assignment fields.

Expected result

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

employee_idfirst_namelast_nameproject_idallocation_pctstart_dateend_date
100JohnSmith1502024-01-012024-12-31
100JohnSmith2502024-01-012024-12-31
101AliceJohnson1802024-02-012024-11-30
102BobWilson1602024-02-152024-12-31
103CarolDavis41002024-04-012024-12-31
104DavidBrown2702024-03-012024-12-31
105EmmaTaylor1302024-01-152024-12-31
105EmmaTaylor2402024-01-152024-12-31
106FrankGreen21002024-03-012024-12-31
107GraceWhiteNULLNULLNULLNULL

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.