SQL
Self Join and Cross Join
Learn about joining a table to itself and Cartesian products.
By TechCoder TeamLast updated: 2026-06-02
In a Nutshell
Learn about joining a table to itself and Cartesian products. This hands-on tutorial focuses on practical implementation of self join and cross join concepts.
Self Join
A self join is a regular join, but the table is joined with itself. This is useful for hierarchical data (e.g., Employees table with a ManagerID).
Syntax
SELECT A.CustomerName AS Customer1, B.CustomerName AS Customer2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City;
Example: Customers from Same City
Find pairs of customers who are from the same country (using Country as we have more data for it).
Cross Join
A CROSS JOIN returns the Cartesian product of the two tables – i.e., it combines every row from the first table with every row from the second table.
Example
Combine every customer with every product.