SQL Joins: Combining Tables
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
Let's look at a selection from the Orders table:
OrderID | CustomerID | OrderDate |
10308 | 2 | 1996-09-18 |
10309 | 37 | 1996-09-19 |
10310 | 77 | 1996-09-20 |
Then, look at a selection from the Customers table:
CustomerID | CustomerName | ContactName | Country |
1 | Alfreds Futterkiste | Maria Anders | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mexico |
Notice that the CustomerID column in the Orders table refers to the CustomerID in the Customers table. The relationship between the two tables above is the CustomerID column.
Then, we can create the following SQL statement (that contains an INNER JOIN
), that selects records that have matching values in both tables:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
and it will produce something like this:
OrderID CustomerName OrderDate 10308 Ana Trujillo Emparedados y helados 9/18/1996 10365 Antonio Moreno Taquería 11/27/1996 10383 Around the Horn 12/16/1996 10355 Around the Horn 11/15/1996 10278 Berglunds snabbköp 8/12/1996Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
(INNER) JOIN:
Returns records that have matching values in both tablesLEFT (OUTER) JOIN
Returns all records from the left table, and the matched records from the right tableRIGHT (OUTER) JOIN
Returns all records from the right table, and the matched records from the left tableFULL (OUTER) JOIN
Returns all records when there is a match in either left or right table
INNER JOIN
The INNER JOIN
keyword selects records that have matching values in both tables.
Example
Join Products and Categories with the INNER JOIN keyword:
SELECT ProductID, ProductName, CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Note: The INNER JOIN
keyword returns only rows with a match in both tables. Which means that if you have a product with no CategoryID, or with a CategoryID that is not present in the Categories table, that record would not be returned in the result.
Syntax
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
Naming the Columns
It is a good practice to include the table name when specifying columns in the SQL statement.
Example:
Specify the table names:
SELECT Products.ProductID, Products.ProductName, Categories.CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
The example above works without specifying table names, because none of the specified column names are present in both tables. If you try to include CategoryID in the SELECT statement, you will get an error if you do not specify the table name (because CategoryID is present in both tables).
JOIN
or INNER JOIN
JOIN
and INNER JOIN
will return the same result.
INNER
is the default join type for JOIN
, so when you write JOIN
the parser actually writes INNER JOIN
.
JOIN Three Tables
The following SQL statement selects all orders with customer and shipper information:
Example:
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);
SQL LEFT JOIN
Keyword
The LEFT JOIN
keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.
Syntax
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
SQL LEFT JOIN
Example
The following SQL statement will select all customers, and any orders they might have:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY Customers.CustomerName;
Note: The LEFT JOIN
keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).
SQL RIGHT JOIN
Keyword
The RIGHT JOIN
keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.
RIGHT JOIN
Syntax
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
SQL RIGHT JOIN
Example
The following SQL statement will return all employees, and any orders they might have placed:
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName FROM Orders RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID ORDER BY Orders.OrderID;
Note: The RIGHT JOIN
keyword returns all records from the right table (Employees), even if there are no matches in the left table (Orders).
SQL FULL OUTER JOIN
Keyword
The FULL OUTER JOIN
keyword returns all records when there is a match in left (table1) or right (table2) table records.
Note: FULL OUTER JOIN and FULL JOIN are the same.
Syntax
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name WHERE condition;
Example
SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName;
Note: The FULL OUTER JOIN
keyword returns all matching records from both tables whether the other table matches or not. So, if there are rows in Customers that do not have matches in Orders, or if there are rows in Orders that do not have matches in Customers, those rows will be listed as well.
A self join is a regular join, but the table is joined with itself.
Self Join Syntax
SELECT column_name(s) FROM table1 T1, table1 T2 WHERE condition;
SQL Self Join Example
The following SQL statement matches customers that are from the same city:
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City ORDER BY A.City;