SQL Join Reference
SQL Join Reference
SQL joins are used to combine rows from two or more tables.
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER
JOIN return all rows from multiple tables where the join condition is met.
Let's look at a selection from the "Orders" table:
OrderID
CustomerID
OrderDate
10308
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20
CustomerName
ContactName
Country
Alfreds Futterkiste
Maria Anders
Germany
Ana Trujillo
Mexico
Antonio Moreno
Mexico
Notice that the "CustomerID" column in the "Orders" table refers to the customer in the
"Customers" table. The relationship between the two tables above is the "CustomerID"
column.
Then, if we run the following SQL statement (that contains an INNER JOIN):
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
CustomerName
OrderDate
10308
9/18/1996
10365
11/27/1996
10383
12/16/1996
10355
11/15/1996
10278
Berglunds snabbkp
8/12/1996
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the
right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from
the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
PS! INNER JOIN is the same as JOIN.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
CustomerID CustomerName
ContactName Address
City
PostalCode Country
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
Ana Trujillo
Emparedados y helados
Ana Trujillo
Avda. de la
Mxico
Constitucin 2222 D.F.
05021
Mexico
Antonio Moreno
Taquera
Antonio
Moreno
Mataderos 2312
Mxico
D.F.
05023
Mexico
CustomerID
EmployeeID
OrderDate
ShipperID
10308
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The INNER JOIN keyword selects all rows from both tables as long as there is a
match between the columns. If there are rows in the "Customers" table that do not have
matches in "Orders", these customers will NOT be listed.
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases LEFT JOIN is called LEFT OUTER JOIN.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
CustomerID CustomerName
ContactName Address
City
PostalCode Country
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
Ana Trujillo
Emparedados y helados
Ana Trujillo
Avda. de la
Mxico
Constitucin 2222 D.F.
05021
Mexico
Antonio Moreno
Taquera
Antonio
Moreno
Mataderos 2312
Mxico
D.F.
05023
Mexico
CustomerID
EmployeeID
OrderDate
ShipperID
10308
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20
Example
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 the rows from the left table (Customers), even
if there are no matches in the right table (Orders).
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Orders" table:
OrderID
CustomerID
EmployeeID
OrderDate
ShipperID
10308
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20
Photo
Notes
Davolio
Nancy
12/8/1968
EmpID1.pic
Fuller
Andrew
2/19/1952
EmpID2.pic
Leverling
Janet
8/30/1963
EmpID3.pic
Example
SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees),
even if there are no matches in the left table (Orders).
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
CustomerID CustomerName
ContactName Address
City
PostalCode Country
Alfreds Futterkiste
Maria Anders
Obere Str. 57
Berlin
12209
Germany
Ana Trujillo
Emparedados y
helados
Ana Trujillo
Avda. de la
Mxico
Constitucin 2222 D.F.
05021
Mexico
Antonio Moreno
Taquera
Antonio
Moreno
Mataderos 2312
Mxico
D.F.
05023
Mexico
CustomerID
EmployeeID OrderDate
ShipperID
10308
1996-09-18
10309
37
1996-09-19
10310
77
1996-09-20
OrderID
Alfreds Futterkiste
Ana Trujillo Emparedados y helados
10308
10365
10382
10351
Note: The FULL OUTER JOIN keyword returns all the rows from the left table
(Customers), and all the rows from the right table (Orders). 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.