SQL - Inner Join
Overview
INNER JOIN returns rows where the join condition matches in both tables.
SELECT e.first_name, d.name AS department
FROM employees e
INNER JOIN departments d ON d.id = e.department_id;
SELECT e.first_name, d.name AS department
FROM employees e
INNER JOIN departments d ON d.id = e.department_id;
SELECT e.first_name, d.name AS department
FROM employees e
INNER JOIN departments d ON d.id = e.department_id;
Prerequisite
Load the Standard Test Data.
Expected Output (live)
| first_name | department |
|---|---|
| Ada | Engineering |
| Bob | Engineering |
| Chen | HR |
| Dee | Sales |
| Eli | Engineering |
Sample Use Cases (3)
1) Employees with department and city
| first_name | department | city |
|---|---|---|
| Ada | Engineering | San Francisco |
| Bob | Engineering | New York |
| Chen | HR | Seattle |
| Dee | Sales | Austin |
| Eli | Engineering | Boston |
2) Orders with customers
| order_id | customer | order_date |
|---|---|---|
| 1 | Alice | 2024-05-01 |
| 2 | Ben | 2024-05-03 |
| 3 | Cara | 2024-05-05 |
3) Order items with products
| order_id | product | qty | price |
|---|---|---|---|
| 1 | Laptop Pro | 1 | 1999 |
| 1 | Wireless Mouse | 1 | 49.99 |
| 2 | Office Chair | 1 | 299 |
| 2 | Coffee Beans | 1 | 14.99 |
| 3 | Standing Desk | 1 | 499 |
| 3 | Wireless Mouse | 1 | 14.99 |