An Engineering compensation reviewer at Helix Systems needs a list of every Engineering employee alongside their salary records.
Write a query to return the employee name, department name, and salary amount for every salary record belonging to an Engineering employee.
Assumptions:
- The chain reaches:
employees→departments,employees→salaries. - The Engineering department is identified by
departments.name = 'Engineering'; the condition can equivalently usedepartment_id = 1. - An Engineering employee with three salary records contributes three rows to the result.
Output:
- One row per qualifying salary record, with columns
employee_name,department_name, andamount.
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,
s.amount
FROM
employees e
JOIN departments d ON e.department_id = d.id
JOIN salaries s ON e.id = s.employee_id
WHERE
d.name = 'Engineering' The shape
The chain reaches from employees sideways to departments and forward to salaries. A WHERE filter on departments.name = 'Engineering' narrows the result to salary records belonging to Engineering employees only. The row count comes from the salaries side — one row per qualifying salary record, not per Engineering employee.
Clause by clause
SELECT e.name AS employee_name, d.name AS department_name, s.amountpulls one column from each of the three tables. Bothemployeesanddepartmentshave anamecolumn, so the aliases are load-bearing.FROM employees eanchors the chain.JOIN departments d ON e.department_id = d.idattaches the department to each employee. One-to-one on the employee side, no row multiplication.JOIN salaries s ON e.id = s.employee_idattaches every salary record for that employee. An Engineering employee with two salary records contributes two rows — Sarah Chen's appearance twice in the result is exactly this. The chain produces one row per salary record before any filter runs.WHERE d.name = 'Engineering'filters the joined result down to rows whose employee belongs to the Engineering department.
Why this and not WHERE e.department_id = 1
Both predicates return the same rows, because the prompt notes the equivalence — Engineering's department ID is 1. The textual filter is preferred for the same reason the prompt mentions it: d.name = 'Engineering' is a self-documenting expression, readable to anyone who didn't write the schema. e.department_id = 1 requires the reader to know that 1 means Engineering. For a one-off ad-hoc query against a familiar schema, the integer form is fine. For a query that lands in a report someone else has to read, the textual form pays for itself.
You practiced filtering a multi-table chain by an attribute on a joined dimension (departments.name). The same pattern from M3 — WHERE on a joined table — works regardless of whether the filtering column is on a central table or a leaf.