MSDN Documentation

Microsoft Developer Network

Joining Tables in SQL

Joining tables is a fundamental operation in relational databases. It allows you to combine rows from two or more tables based on a related column between them. This is essential for retrieving data that is spread across different tables.

Types of Joins

SQL provides several types of joins, each serving a specific purpose:

1. INNER JOIN

An INNER JOIN returns only the rows where the join condition is met in both tables. If there's no match in either table, the row is excluded from the result set.

Syntax Example:


SELECT
    column1, column2, ...
FROM
    table1
INNER JOIN table2
    ON table1.common_column = table2.common_column;
                    

Consider two tables: Customers (with CustomerID, CustomerName) and Orders (with OrderID, CustomerID, OrderDate). An INNER JOIN on CustomerID would return only customers who have placed orders.

2. LEFT (OUTER) JOIN

A LEFT JOIN returns all rows from the left table, and the matched rows from the right table. If there is no match in the right table, the result is NULL on the right side.

Syntax Example:


SELECT
    column1, column2, ...
FROM
    table1 LEFT JOIN table2
    ON table1.common_column = table2.common_column;
                    

Using the Customers and Orders example, a LEFT JOIN from Customers to Orders would list all customers, even those who haven't placed any orders (their OrderID and OrderDate would be NULL).

3. RIGHT (OUTER) JOIN

A RIGHT JOIN is the inverse of a LEFT JOIN. It returns all rows from the right table, and the matched rows from the left table. If there is no match in the left table, the result is NULL on the left side.

Syntax Example:


SELECT
    column1, column2, ...
FROM
    table1 RIGHT JOIN table2
    ON table1.common_column = table2.common_column;
                    

4. FULL (OUTER) JOIN

A FULL JOIN returns all rows when there is a match in either the left or the right table. It combines the results of both left and right outer joins. If there's no match for a row in one table, the columns from the other table will be NULL.

Syntax Example:


SELECT
    column1, column2, ...
FROM
    table1 FULL OUTER JOIN table2
    ON table1.common_column = table2.common_column;
                    

5. CROSS JOIN

A CROSS JOIN returns the Cartesian product of the two tables. This means it combines every row from the first table with every row from the second table. This is rarely used unless you explicitly need all possible combinations.

Syntax Example:


SELECT
    column1, column2, ...
FROM
    table1
CROSS JOIN table2;
                    

Self-Joins

A self-join is a regular join, but the table is joined with itself. This is useful for querying hierarchical data or comparing rows within the same table. You must use table aliases to distinguish between the two instances of the table.

Example: Finding employees and their managers:


SELECT
    e1.EmployeeName AS Employee,
    e2.EmployeeName AS Manager
FROM
    Employees e1
LEFT JOIN Employees e2
    ON e1.ManagerID = e2.EmployeeID;
                    

Choosing the Right Join

The type of join you choose depends entirely on the data you want to retrieve:

  • Use INNER JOIN when you only want rows that have matching values in both tables.
  • Use LEFT JOIN when you want all records from the left table, and matching records from the right, or NULLs if no match exists.
  • Use RIGHT JOIN when you want all records from the right table, and matching records from the left, or NULLs if no match exists.
  • Use FULL OUTER JOIN when you want all records from both tables, with NULLs where there are no matches.
  • Use CROSS JOIN sparingly, only when you need every possible combination of rows.