Medium SQL joins practice Employees database

Join two tables with INNER JOIN

Question

Show each employee's name next to the name of their department.

employeesid, name, salary, department_id, manager_id, hired_on
departmentsid, name, city

Your query

Ctrl+Enter to run

Result

Press Run to see your query's result, or Check answer to compare it with the expected result.

Expected result

employeedepartment
Tom BeckerEngineering
Ana SouzaSales
Priya NairEngineering
Lucas MoreauSales
Mei ChenEngineering
Omar HaddadSales
Sofia RossiMarketing
Jonas BergMarketing
Aisha BelloSupport
Diego RuizSupport
Hana KimEngineering
Liam WalshSupport

Rows can come in any order. Column names do not have to match.

Hint

Join employees to departments where employees.department_id equals departments.id.

Show the solution
solution.sql Run this code
SELECT e.name AS employee, d.name AS department
FROM employees e
JOIN departments d ON d.id = e.department_id;

Why it works

JOIN (short for INNER JOIN) pairs each employee row with the department row whose id matches. The aliases e and d keep the query short and make it clear which name is which.

Common mistake

Without the ON condition you get every employee combined with every department (12 x 4 = 48 rows). That is a cross join, almost never what you want.

Want to try the query on MySQL or PostgreSQL? Open it in the SQL Online Compiler or SQLite Online.