TOP

SQL Joins

SQL JOIN

The JOIN expression is used to join rows from two or more tables based on the related column between them.


Let's look at a sample from the table "Orders" ("Orders"):

ProductIDOrderIDCustomerIDEmployeeIDOrderDateShipperID
1102489051996-07-043
2102498161996-07-051
3102503441996-07-082

Then look at the sample from the table "Customers" ("Customers"):

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico

Note that column "CustomerID" in table "Orders" refers to "CustomerID" in table "Customers". The relationship between the two tables above is the "CustomerID" column.

We can then create the following SQL statement (containing INNER JOIN) that selects records that have matching values ​​in both tables:

Run SQLSELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID

and this will produce something like this:

OrderIDCustomerNameOrderDate
10308Ana Trujillo Emparedados y helados1996-09-18
10365Antonio Moreno Taquería1996-11-27
10355Around the Horn1996-11-15
10383Around the Horn1996-12-16
10289B's Beverages1996-08-26

Different types of SQL JOIN

Here are the different types of JOIN in SQL:

  • (INNER) JOIN: Returns records that have matching values ​​in both tables
  • LEFT (OUTER) JOIN: Returns all records from the left table and corresponding records from the right table
  • RIGHT (OUTER) JOIN: Returns all records from the right table and matching records from the left table
  • FULL (OUTER) JOIN: Returns all records if there is a match in the left or right table


  • SQL INNER JOIN  SQL LEFT JOIN  SQL RIGHT JOIN  SQL FULL OUTER JOIN