| Join Type | What it Returns | Common Use Case |
|---|---|---|
| INNER JOIN | ONLY rows with a match in BOTH tables. | "Show me customers who have actually placed an order." |
| LEFT JOIN | ALL rows from the Left table, and matching rows from the Right. | "Show me ALL customers, and their orders if they have any." |
| RIGHT JOIN | ALL rows from the Right table, and matching rows from the Left. | "Show me ALL orders, and link the customer data to them." |
| FULL OUTER JOIN | ALL rows from BOTH tables, regardless of matches. | "Show me everything from both tables, matched up where possible." |
| CROSS JOIN | Every row from Left combined with Every row from Right. | "Generate all possible combinations of Size and Color." |
Because relational databases eliminate redundancy by splitting data into multiple tables (e.g., storing Customers in one table and Orders in another), we need a way to bring them back together. JOIN clauses allow us to link tables horizontally using their related columns (Primary Keys and Foreign Keys).
The INNER JOIN is the default and most common join. It acts like an intersection—it only returns records where the link exists in both tables.
-- Find details of orders and the customers who placed them
SELECT C.FirstName, C.LastName, O.OrderDate, O.TotalAmount
FROM Customers C
INNER JOIN Orders O
ON C.CustomerID = O.CustomerID;
Outer joins are used when you want to keep records even if there is no match in the other table. The missing data will simply be filled in with NULL values.
Keeps everything from the "Left" table (the one mentioned first/top), regardless of whether it found a match in the second table.
-- Shows ALL customers. If they haven't ordered, OrderDate will be NULL.
SELECT C.FirstName, O.OrderDate
FROM Customers C
LEFT JOIN Orders O
ON C.CustomerID = O.CustomerID;
The exact opposite of a Left Join. It keeps everything from the "Right" table (the one mentioned second/bottom).
SELECT C.FirstName, O.OrderDate
FROM Customers C
RIGHT JOIN Orders O
ON C.CustomerID = O.CustomerID;
Combines Left and Right joins. It keeps everything from both tables. If a match isn't found, NULLs are placed on the side that is missing data.
SELECT C.FirstName, O.OrderDate
FROM Customers C
FULL OUTER JOIN Orders O
ON C.CustomerID = O.CustomerID;
A CROSS JOIN produces a "Cartesian Product." It takes row 1 from Table A and pairs it with every row in Table B. Then it takes row 2 from Table A and does the same. Notice it does NOT use an ON clause.
-- If you have 3 Shirt Sizes and 4 Colors, this generates 12 rows of combinations. SELECT S.SizeName, C.ColorName FROM Sizes S CROSS JOIN Colors C;
A Self Join is a regular join, but the table is joined to itself. This is used when a table contains hierarchical data. For example, an Employee table where managers are also employees.
-- Match an employee to their manager (who is in the same table!)
SELECT
Emp.FirstName AS EmployeeName,
Mgr.FirstName AS ManagerName
FROM Employees Emp
LEFT JOIN Employees Mgr
ON Emp.ManagerID = Mgr.EmployeeID;