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
Expected result
| employee | department |
|---|---|
| Tom Becker | Engineering |
| Ana Souza | Sales |
| Priya Nair | Engineering |
| Lucas Moreau | Sales |
| Mei Chen | Engineering |
| Omar Haddad | Sales |
| Sofia Rossi | Marketing |
| Jonas Berg | Marketing |
| Aisha Bello | Support |
| Diego Ruiz | Support |
| Hana Kim | Engineering |
| Liam Walsh | Support |
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.