SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
Look at the selection from the "Customers" table:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
Note that the "CustomerID" column is the primary key in the "Customers" table. This means that no two rows can have the same "CustomerID". The "CustomerID" distinguishes two companies even if they have the same name.
Next, we have the selection from the "Orders" table:
| OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
|---|---|---|---|---|
| 10308 | 2 | 7 | 1996-09-18 | 3 |
| 10309 | 37 | 3 | 1996-09-19 | 1 |
| 10310 | 77 | 8 | 1996-09-20 | 2 |
Note that the "OrderID" column is the primary key in the "Orders" table and that the "CustomerID" column refers to the company in the "Customers" table without using their name.
Notice that the relationship between the two tables above is the "CustomerID" column.
Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
Your message has been sent to W3Schools.