đ Lab 2.4: SQL CROSS JOIN and SELF JOIN
This lab focuses on two special types of joins in SQL — CROSS JOIN and SELF JOIN. We’ll use Bangladeshi employee data to understand how each works.
đ Sample Employees Table
emp_id | emp_name | manager_id |
---|---|---|
1 | Rakib | 3 |
2 | Tamanna | 3 |
3 | Imran (Manager) | NULL |
đ 1. CROSS JOIN
SELECT a.emp_name AS emp1, b.emp_name AS emp2
FROM employees a
CROSS JOIN employees b;
This joins every row from the first table with every row from the second table. Useful for combinations or testing all pairs.
- If 3 employees exist, you get 3 × 3 = 9 rows
- This is also called a **Cartesian Product**
đ¯ Example Use Case:
Generate all possible pairs of employees for assigning as team leads.
đ 2. SELF JOIN
SELECT e.emp_name AS Employee, m.emp_name AS Manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.emp_id;
A SELF JOIN joins a table with itself. Here, we’re joining the employee table to itself to match each employee with their manager.
đ¯ Output:
- Rakib → Manager: Imran
- Tamanna → Manager: Imran
- Imran → Manager: NULL
đ¯ Example Use Case:
Show who reports to whom in an organizational hierarchy.
đ Summary
- CROSS JOIN — All combinations between two tables
- SELF JOIN — Join table with itself to show relationships like manager-employee
✅ These joins are less common but very powerful in data modeling and reporting. Practice them using real datasets like employee-manager relationships.
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ