MYSQL

11. MySQL Joins – Complete Guide with Practical Examples

πŸ”— MySQL Joins – Complete Guide with Practical Examples

In MySQL, JOIN clauses allow you to combine rows from two or more tables based on related columns.

We’ll use these three sample tables:


πŸ‘¨β€πŸ’Ό employees Table

employee_id name department_id manager_id
1 Alice 1 NULL
2 Bob 2 1
3 Charlie 3 1
4 David NULL 2
5 Eva 2 2

🏒 departments Table

department_id department_name
1 HR
2 IT
4 Marketing

πŸ’» laptops Table

laptop_id employee_id brand
1 1 Dell
2 2 HP
3 3 Lenovo
4 5 Apple

βœ… 1️⃣ INNER JOIN – Only Matching Rows

Goal: Get employee names with their department names.

SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;

βœ… Result: Only employees with matching departments.

name department_name
Alice HR
Bob IT
Eva IT

πŸŸ₯ Charlie (dept 3 not in departments) and David (no department) are excluded.

MySQL Left join
MySQL Left join

βœ… 2️⃣ LEFT JOIN – All from Left + Matching from Right

Goal: List all employees, even if no department exists.

SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;

βœ… Result:

name department_name
Alice HR
Bob IT
Charlie NULL
David NULL
Eva IT

All employees included. Charlie and David have no matching departments.

Left Join MySQL
Left Join MySQL

βœ… 3️⃣ RIGHT JOIN – All from Right + Matching from Left

Goal: List all departments, even if no employees exist.

SELECT e.name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;

βœ… Result:

name department_name
Alice HR
Bob IT
Eva IT
NULL Marketing

πŸŸ₯ Marketing has no employees, so name is NULL.

Right Join


βœ… 4️⃣ CROSS JOIN – All Combinations

Goal: Show all possible employee-department combinations.

SELECT e.name, d.department_name
FROM employees e
CROSS JOIN departments d;

βœ… Result: 5 employees Γ— 3 departments = 15 rows.

name department_name
Alice HR
Alice IT
Alice Marketing
Bob HR

Used rarely, but helpful for creating combinations or matrices.

Cross Join


βœ… 5️⃣ SELF JOIN – Relating a Table to Itself

Goal: Show which employee reports to whom (using manager_id).

SELECT e.name AS Employee, m.name AS Manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;

βœ… Result:

Employee Manager
Alice NULL
Bob Alice
Charlie Alice
David Bob
Eva Bob

βœ… 6️⃣ JOIN with a Third Table (Laptop Info)

Goal: Show each employee with their laptop brand and department.

SELECT e.name, d.department_name, l.brand
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
LEFT JOIN laptops l ON e.employee_id = l.employee_id;

βœ… Result:

name department_name brand
Alice HR Dell
Bob IT HP
Charlie NULL Lenovo
David NULL NULL
Eva IT Apple

πŸ”„ All employees shown β€” laptop and department shown if exists.


βœ… 7️⃣ FULL OUTER JOIN (Simulated in MySQL)

MySQL doesn’t support FULL OUTER JOIN natively. You can simulate it using UNION.

Goal: Get all employees and all departments even if no match.

-- LEFT JOIN part
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
UNION

— RIGHT JOIN part
SELECT e.name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;

βœ… Combines unmatched records from both sides.


βœ… 8️⃣ UNION – Merge Query Results Vertically

Goal: Combine all employee names and department names into one list.

SELECT name AS label FROM employees
UNION
SELECT department_name FROM departments;

βœ… Result: One-column result with all unique names (no duplicates).


πŸ“Š Summary Table

JOIN Type Description NULLs When No Match?
INNER JOIN Only matching rows from both tables ❌
LEFT JOIN All from left + matches from right βœ… Right side
RIGHT JOIN All from right + matches from left βœ… Left side
CROSS JOIN All combinations (Cartesian product) ❌
SELF JOIN Join a table to itself βœ…
UNION Merge results vertically (distinct only) βœ… Duplicates removed
UNION ALL Same as UNION, but includes duplicates βœ…
FULL OUTER JOIN Simulated using UNION of LEFT + RIGHT JOIN βœ… Both sides

Leave a Reply

Your email address will not be published. Required fields are marked *