
Tech_updates
May 24, 2025 at 04:58 AM
*SELF JOIN & CROSS JOIN IN SQL*
*1. SELF JOIN*
A SELF JOIN is a regular join where a table is joined with itself.
*When to use:*
When rows in the same table relate to each other. Example: Employees and their managers in the same employee table.
*Example:*
SELECT A.name AS employee, B.name AS manager
FROM employees A
JOIN employees B ON A.manager_id = B.id;
Here, you're joining the employees table with itself to get each employee’s manager.
*2. CROSS JOIN*
A CROSS JOIN returns the cartesian product of two tables — every row of the first table combined with every row of the second.
*When to use:*
When you need all possible combinations. Useful in generating calendars, test data, etc.
*Example:*
SELECT *
FROM colors
CROSS JOIN sizes;
If colors has 3 rows and sizes has 4 rows, the result will have 3 × 4 = 12 rows.
*React with ❤️ if you're ready for the next quiz.* 📝
❤️
❤
👍
16