Join Example in SQL
Simple JOIN Example in SQL
Let's say you have two tables:
Employees
| employee_id | name | department_id |
|---|---|---|
| 1 | Alice | 10 |
| 2 | Bob | 20 |
| 3 | Charlie | 30 |
Departments
| department_id | department_name |
|---|---|
| 10 | Sales |
| 20 | Marketing |
| 30 | IT |
Example: INNER JOIN to get employee names with their department names
SELECT e.name, d.department_nameFROM Employees eINNER JOIN Departments d ON e.department_id = d.department_id;Result:
| name | department_name |
|---|---|
| Alice | Sales |
| Bob | Marketing |
| Charlie | IT |
Explanation:
The query joins
EmployeesandDepartmentsusing the matchingdepartment_id.Only rows with matching
department_idin both tables are included.
If you want, I can show examples of other types of joins too!