All problems

Employee Names With Department Names

mediumSQLJoins

Kestrel Labs is publishing an internal staff directory: every employee on one line, next to the team they belong to. The intranet template that renders it takes exactly two columns and insists on the headings employee_name and department_name.

That heading requirement is the catch, and it is not cosmetic. Both tables call their label column name, so pulling both straight through hands the template two columns with the same heading — and the template will render one of them twice without complaining about it.

The link between the two tables is a bare number: an employee row records the id of its team, never the team's name.

departments — one row per team. budget is the annual budget in dollars.

id name budget
1 Engineering 900000
2 Sales 400000
3 Marketing 250000
4 Data 500000

employees — one row per employee. department_id records which team they belong to. manager_id points at another employee, or is NULL.

id name department_id salary hire_date manager_id
1 Ava Chen 1 145000 2021-03-14 NULL
2 Ben Ortiz 1 118000 2022-06-01 1
3 Cara Novak 1 121000 2023-01-10 1
4 Deshawn Lee 2 95000 2020-09-23 NULL
5 Elin Kask 2 88000 2022-11-05 4
6 Farid Amiri 3 76000 2023-04-18 NULL
7 Grace Kim 4 132000 2021-07-30 NULL
8 Hugo Silva 4 110000 2023-02-14 7
9 Ines Duarte 4 104000 2023-08-01 7
10 Jonas Weber 2 91000 2021-12-19 4

Both tables already exist in the database — there is nothing to create or load.

Task: Write a query returning two columns, employee_name and department_name, with one row per employee. Rows come back in alphabetical order of employee_name.

Example output — shape only. These are the opening rows of an invented company, not this one's answer.

employee_name department_name
Anton Reis Research
Lena Fournier Facilities
Mira Kovac Research
Rosa Kwan Legal

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…