Exercise 1
intermediateQuestion
Join employees with their departments to show employee name and department name.
Table Schema
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department_id INT
);
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(100)
);Show Solution
Solution
1SELECT e.name AS employee_name, d.name AS department_name
2FROM employees e
3INNER JOIN departments d ON e.department_id = d.id;Expected Output
| employee_name | department_name | |---|---| | John | Sales | | Jane | Engineering |
Explanation
INNER JOIN returns only rows that have matching values in both tables. Use table aliases (e, d) for cleaner code.