SQL JOINs: Combining Data from Multiple Tables

SQL joins are used to combine rows from two or more tables based on a related column between them. This allows you to retrieve data from multiple tables in a single query, creating a more comprehensive result.



INNER JOIN

INNER JOIN: Definition and Usage

An INNER JOIN returns only the rows where there is a match in both tables being joined. If a row in one table doesn't have a corresponding match in the other table (based on the join condition), that row is excluded from the results.

Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
      

Example: Joining Orders and Customers

Syntax

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
      
Output

OrderID | CustomerName
-----------------------
(Order IDs and corresponding Customer Names will be displayed here)
      

This query returns only orders with matching customer information. Orders without a matching customer are not included.

Joining Multiple Tables

You can join more than two tables using multiple `INNER JOIN` clauses. This example adds shipper information.

Syntax

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID;
      
Output

OrderID | CustomerName | ShipperName
-----------------------------------
(Order IDs, Customer Names, and Shipper Names will be displayed here)
      

LEFT JOIN

LEFT JOIN: Definition and Usage

A LEFT JOIN (also known as a LEFT OUTER JOIN) returns all rows from the left table, even if there is no match in the right table. For rows in the left table without a match in the right table, the columns from the right table will have NULL values.

Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
      

Example: Customers and Their Orders

Syntax

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
      
Output

CustomerName | OrderID
-----------------------
(All customers will be listed; if a customer has no orders, OrderID will be NULL)
      

RIGHT JOIN

RIGHT JOIN: Definition and Usage

A RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table, even if there is no match in the left table. For rows in the right table without a match in the left table, the columns from the left table will have NULL values.

Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
      

Example: Employees and Their Orders

Syntax

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
      
Output

OrderID | LastName | FirstName
-------------------------------
(All employees will be listed; if an employee has no orders, OrderID will be NULL)
      

FULL OUTER JOIN

FULL OUTER JOIN: Definition and Usage

A FULL OUTER JOIN returns all rows from both the left and right tables. If a row in one table doesn't have a match in the other, it's still included in the result set, with NULL values for the columns from the table where there's no match.

Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
      

Example: All Customers and Orders

Syntax

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
      
Output

CustomerName             | OrderID
---------------------------------
(All customers and all orders will be listed; NULLs where there is no match)
      

**Note:** The examples assume the existence of `Customers`, `Orders`, and `Employees` tables with the necessary columns and sample data. The `(...)` in the outputs indicate that the actual output will contain data from those tables based on the join conditions. The specific `OrderID` values and whether there are null values will depend on the data in your tables.