An Outer Join is a type of database join (commonly used in SQL) that returns records from one or both tables even if there’s no matching record in the other table.
LEFT OUTER JOIN (or simply: LEFT JOIN):
→ Returns all records from the left table, and the matching ones from the right table.
→ If there’s no match, the result is filled with NULL
values from the right table.
RIGHT OUTER JOIN (or: RIGHT JOIN):
→ Returns all records from the right table, and the matching ones from the left table.
→ If there’s no match, NULL
is used for the left side.
FULL OUTER JOIN:
→ Returns all records from both tables, with NULL
where no match exists on either side.
Suppose you have two tables:
Customers
CustomerID | Name |
1 | Anna |
2 | Bernd |
3 | Clara |
Orders
OrderID | CustomerID | Product |
101 | 2 | Book |
102 | 4 | Lamp |
CustomerID | Name | OrderID | Product |
---|---|---|---|
1 | Anna | NULL | NULL |
2 | Bernd | 101 | Book |
3 | Clara | NULL | NULL |
An INNER JOIN is a term used in SQL (Structured Query Language) to combine rows from two (or more) tables based on a related column between them.
You have two tables:
Table: Customers
CustomerID | Name |
---|---|
1 | Anna |
2 | Bernd |
3 | Clara |
Table: Orders
OrderID | CustomerID | Product |
---|---|---|
101 | 1 | Book |
102 | 2 | Laptop |
103 | 4 | Phone |
Now you want to know which customers have placed orders. You only want the customers who exist in both tables.
SELECT Customers.Name, Orders.Product
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Name | Product |
---|---|
Anna | Book |
Bernd | Laptop |
Clara didn’t place any orders → not included.
The order with CustomerID 4 doesn’t match any customer → also excluded.
An INNER JOIN returns only the rows with matching values in both tables.