Intro
A JOIN is used to combine the data from different tables based on a related column between them
There are many different types of JOINs:
- SET JOINs
- UNION - combines all data
- INTERSECT - shows the common data
- EXCEPT - shows the differences between the tables
- EQUI-JOINs
- SELF - joined to itself
- CROSS - (USED IN BUSINESS INTELIGENCE) finds all combinations of data
- INNER - (MOST IMPORTANT JOIN) finds matching data
- OUTER - finds missing data
- LEFT - returns all records from left side and matching records from right side
- RIGHT - returns all records from right side and matching records from left side
- FULL - returns all records when there is a match in either left or right table
- NESTED JOINs
- CORRELATED - (SLOWEST QUERY)
- NON-CORRELATED -
SET JOINs are used to join the same type of table
EQUI-JOINs are used for different types of tables
NESTED JOINs are used to solve complex query problems
Rules for joining tables:
- One of the sides MUST be a Primary Key
- Add data to parents first, then child tables - if there is no parent, you can't add data
Sample Data
These are the tables we will use as an example:
SELECT * FROM ar_invoices
| invoice# | customer# | salesperson# | invoice_date | invoice_amount | amount_paid | current_month_interest_charges | total_interest_charges |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 2019-06-17 10:43:53.727 | 100.00 | 1.00 | 0.00 | 0.00 |
| 2 | 2 | 2 | 2019-05-28 10:43:53.727 | 50.00 | 0.00 | 5.00 | 10.00 |
| 3 | 3 | 2 | 2019-06-17 10:43:53.727 | 70.00 | 0.00 | 10.00 | 20.00 |
| 4 | 2 | 1 | 2019-07-07 10:43:53.727 | 80.00 | 0.00 | 0.00 | 0.00 |
SELECT * FROM ar_salespeople
| salesperson# | salesperson_name |
|---|---|
| 1 | bob |
| 2 | mary |
| 3 | tim |
SELECT * FROM ar_customers
| customer# | customer_name | customer_address | customer_province | customer_rating |
|---|---|---|---|---|
| 1 | bill | 123 main st. | ab | 1 |
| 2 | ed | 456 main st. | bc | 2 |
| 3 | john | 789 main st. | ab | 3 |
INNER JOIN
Returns all the records that have matching data in both tables
Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name
SELECT salesperson_name,
invoice_amount,
customer#
FROM ar_invoices
INNER JOIN ar_salespeople
ON ar_invoices.salesperson# = ar_salespeople.salesperson#
| salesperson_name | invoice_amount | customer# |
|---|---|---|
| bob | 100.00 | 1 |
| mary | 50.00 | 2 |
| mary | 70.00 | 3 |
| bob | 80.00 | 2 |
Combine multiple JOINs to link many tables together:
SELECT salesperson_name,
invoice_amount,
customer_name
FROM ar_invoices
INNER JOIN ar_salespeople
ON ar_invoices.salesperson# = ar_salespeople.salesperson#
INNER JOIN ar_customers
ON ar_invoices.customer# = ar_customers.customer#
| salesperson_name | invoice_amount | customer_name |
|---|---|---|
| bob | 100.00 | bill |
| mary | 50.00 | ed |
| mary | 70.00 | john |
| bob | 80.00 | ed |
The old way to do an INNER JOIN (ditch the keywords for commas and use a WHERE):
SELECT salesperson_name,
invoice_amount,
customer_name
FROM ar_invoices, ar_salespeople, ar_customers
WHERE ar_invoices.salesperson# = ar_salespeople.salesperson#
AND ar_invoices.customer# = ar_customers.customer#
| salesperson_name | invoice_amount | customer_name |
|---|---|---|
| bob | 100.00 | bill |
| mary | 50.00 | ed |
| mary | 70.00 | john |
| bob | 80.00 | ed |
LEFT OUTER JOIN
Returns all the data from the left table, and the matched records from the right table. If there is no match in the right side, the result is NULL
Syntax
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name = table2.column_name
SELECT salesperson_name,
invoice#
FROM ar_salespeople LEFT OUTER JOIN ar_invoices
ON ar_invoices.salesperson# = ar_salespeople.salesperson#
| salesperson_name | invoice# |
|---|---|
| bob | 1 |
| bob | 4 |
| mary | 2 |
| mary | 3 |
| tim | NULL |
A RIGHT OUTER JOIN is the same except it shows all the records from the right table
FULL OUTER JOIN
A FULL OUTER JOIN returns all the data from both tables
Note: FULL OUTER JOIN can potentially return huge result sets!
Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition
SELECT salesperson_name,
invoice#
FROM ar_salespeople FULL OUTER JOIN ar_invoices
ON ar_invoices.salesperson# = ar_salespeople.salesperson#
| salesperson_name | invoice# |
|---|---|
| bob | 1 |
| bob | 4 |
| mary | 2 |
| mary | 3 |
| tim | NULL |
SELF JOIN
A SELF JOIN is a regular join, except the table is joined to itself
Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition
T1 and T2 are different table aliases for the same table
This example finds all the customers that live in the same province
SELECT A.customer_name AS CustomerA,
B.customer_name AS CustomerB,
A.customer_province
FROM ar_customers A,
ar_customers B
WHERE A.customer# <> B.customer#
AND A.customer_province = B.customer_province
| CustomerA | CustomerB | customer_province |
|---|---|---|
| bill | john | ab |
| john | bill | ab |
CROSS JOIN
A CROSS JOIN will link every column in the first table to every column in the second table
CROSS JOIN is used for business inteligence (research/data mining)
Syntax
SELECT column_name(s)
FROM table1
CROSS JOIN table2
Do not understand the use of this
SELECT salesperson_name,
invoice_amount
FROM ar_salespeople
CROSS JOIN ar_invoices
| salesperson_name | invoice_amount |
|---|---|
| bob | 100.00 |
| bob | 50.00 |
| bob | 70.00 |
| bob | 80.00 |
| mary | 100.00 |
| mary | 50.00 |
| mary | 70.00 |
| mary | 80.00 |
| tim | 100.00 |
| tim | 50.00 |
| tim | 70.00 |
| tim | 80.00 |
UNION JOIN
A UNION JOIN is used to combine the result-set of two or more SELECT statements
- Each
SELECTstatement withinUNIONmust have the same number of columns - Those columns must be of the same data types
- Those columns must also be in the same order
Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2
SELECT salesperson_name AS Name FROM ar_salespeople UNION SELECT customer_name FROM ar_customers
| Name |
|---|
| bill |
| bob |
| ed |
| john |
| mary |
| tim |