In the "Employees" table
below, the "Employee_ID" column is the
primary key, meaning that no two rows can have the same Employee_ID. The Employee_ID
distinguishes two persons even if they have the same name.
When you look at the example
tables below, notice that:
Employees:
|
Employee_ID |
Name |
|
01 |
Hansen, Ola |
|
02 |
Svendson, Tove |
|
03 |
Svendson, Stephen |
|
04 |
Pettersen, Kari |
Orders:
|
Prod_ID |
Product |
Employee_ID |
|
234 |
Printer |
01 |
|
657 |
Table |
03 |
|
865 |
Chair |
03 |
We can select data from two tables
by referring to two tables, like this:
Who has ordered a product, and
what did they order?
SELECT Employees.Name, Orders.ProductFROM Employees, OrdersWHERE Employees.Employee_ID=Orders.Employee_ID |
Result
|
Name |
Product |
|
Hansen, Ola |
Printer |
|
Svendson, Stephen |
Table |
|
Svendson, Stephen |
Chair |
Who ordered a printer?
SELECT Employees.NameFROM Employees, OrdersWHERE Employees.Employee_ID=Orders.Employee_IDAND Orders.Product='Printer' |
Result
|
Name |
|
Hansen, Ola |
OR we can select data from two
tables with the JOIN keyword, like this:
Syntax
SELECT field1, field2, field3FROM first_tableINNER JOIN second_tableON first_table.keyfield = second_table.foreign_keyfield |
Who has ordered a product, and
what did they order?
SELECT Employees.Name, Orders.ProductFROM EmployeesINNER JOIN OrdersON Employees.Employee_ID=Orders.Employee_ID |
The INNER JOIN returns all rows
from both tables where there is a match. If there are rows in Employees that do
not have matches in Orders, those rows will not be listed.
Result
|
Name |
Product |
|
Hansen, Ola |
Printer |
|
Svendson, Stephen |
Table |
|
Svendson, Stephen |
Chair |
Syntax
SELECT field1, field2, field3FROM first_tableLEFT JOIN second_tableON first_table.keyfield = second_table.foreign_keyfield |
List all employees, and their
orders - if any.
SELECT Employees.Name, Orders.ProductFROM EmployeesLEFT JOIN OrdersON Employees.Employee_ID=Orders.Employee_ID |
The LEFT JOIN returns all the rows
from the first table (Employees), even if there are no matches in the second
table (Orders). If there are rows in Employees that do not have matches in
Orders, those rows also will be listed.
Result
|
Name |
Product |
|
Hansen, Ola |
Printer |
|
Svendson, Tove |
|
|
Svendson, Stephen |
Table |
|
Svendson, Stephen |
Chair |
|
Pettersen, Kari |
|
Syntax
SELECT field1, field2, field3FROM first_tableRIGHT JOIN second_tableON first_table.keyfield = second_table.foreign_keyfield |
List all orders, and who has
ordered - if any.
SELECT Employees.Name, Orders.ProductFROM EmployeesRIGHT JOIN OrdersON Employees.Employee_ID=Orders.Employee_ID |
The RIGHT JOIN returns all the
rows from the second table (Orders), even if there are no matches in the first
table (Employees). If there had been any rows in Orders that did not have
matches in Employees, those rows also would have been listed.
Result
|
Name |
Product |
|
Hansen, Ola |
Printer |
|
Svendson, Stephen |
Table |
|
Svendson, Stephen |
Chair |
Who ordered a printer?
SELECT Employees.NameFROM EmployeesINNER JOIN OrdersON Employees.Employee_ID=Orders.Employee_IDWHERE Orders.Product = 'Printer' |
Result
|
Name |
|
Hansen, Ola |
A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values)
Cross joins return all rows from
the left table, each row from the left table is
combined with all rows from the right table. Cross joins are also called
Cartesian products.