You are on page 1of 29

Chapter 4

How to retrieve data


from two or more tables
Objectives
• How to work with inner joins
• How to work with outer joins
• Other skills for working with joins
• How to work with unions
How to work with inner joins
JOINS
Customer
Orders
Customer Code
Name
City Order Number
Address Order Date
Pin code Customer code
JOINS (2)

JOINS

INNER OUTER SELF


How to code an inner join
How to use table aliases
• The syntax for an inner join that uses table aliases

• Example:
How to work with tables from
different databases
How to use compound
join conditions
• A join condition can include 2 or more conditions by
using AND, OR
• Condition in ON run before data join and condition
in WHERE run after data joined

SELECT InvoiceNumber, InvoiceDate,


InvoiceTotal, InvoiceLineItemAmount
FROM Invoices i JOIN InvoiceLineItem li
ON i.InvoiceID = li.InvoiceID
WHERE i.InvoiceTotal >li.InvoiceLineItemAmount
ORDER BY InvoiceNumber
Inner joins that join
more than two tables
• Multi-table join: a series of two table joins
• Example:
SELECT VendorName, InvoiceNumber, InvoiceDate,
InvoiceLineItemAmount AS LineItemAmount, AccountDescription
FROM Vendors
JOIN Invoices ON Venders.VenderID=Invoices.VenderID
JOIN InvoiceLineItems ON Invoices.InvoiceID =
InvoiceLineItems.InvoiceID
JOIN GLAccounts ON InvoiceLineItems.AccountNo=
GLAccounts. AccountNo
How to use the implicit
inner join syntax
• Instead code Join from FROM clause, you can code search
condition in WHERE clause, it is called implicit inner join.
How to use a self-join
• Self-join: a table joined with itself
• Use correlation names for the tables,
columns
• Frequently include DISTINCT keyword to
eliminate duplicate rows
Self-join example
EmployeeCode EmployeeName ManagerCode
1 John Smith
2 Alan Shearer 1
3 Golden Brown 2
4 David Kan 2

• List name of employee and name of his/her


manager
Self-join example (cont.)
EMPLOYEE as Emp EMPLOYEE as Mgr
EmployeeCode EmployeeCode
EmployeeName EmployeeName
ManagerCode ManagerCode

SELECT emp.name as EmployeeName,


mgr.name as ManagerName
FROM employee emp JOIN employee mgr
ON emp.managerCode = mgr.employeeCode

EmployeeName ManagerName
Alan Shearer John Smith
Golden Brown Alan Shearer
David Kan Alan Shearer
How to work with outer joins

15/31
Introduction to outer join
• Inner Joins are the most common type of
join. But MySQL support outer join.
• Outer Joins retrieves all rows of that
satisfy the join condition, plus unmatched
rows in the left or right table.
• There are 3 types of outer joins:
– LEFT JOIN
– RIGHT JOIN
– FULL JOIN
How to code an outer join
Outer join examples
Outer joins that join more than
two tables
Other skills for working with joins
This session use USING and NATURAL
keywords and cross join.
How to join tables with the
USING keyword
• If the column join are same name in both join table.
You can use USING keyword to simplify the syntax
for join statement.
• Example:
SELECT invoice_number, vendor_name
FROM vendors
JOIN invoices USING(vendor_id)
ORDER BY invoice_number
How to join tables with the
NATURAL keyword
• The NATURAL keyword is used to create a natural
join that joins two tables based on all columns in
the two tables that have the same name.
• Example:
SELECT
department_name,last_name,project_name
FROM departments
NATURAL JOIN employees
LEFT JOIN projects USING(vendor_id)
ORDER BY department_name
How to use cross joins
• A cross join joins each row from the first table with
each row from the second table.
• Example:
SELECT d.department_number,department_name,
employee_id,last_name
FROM departments d
CROSS JOIN employees
ORDER BY d.department_number
How to work with unions
The syntax of a union
• Same number of columns
• Same columns name, data types
SELECT column_list FROM table1
UNION
SELECT column_list FROM table2
• Default: eliminates duplicate rows
• ALL keyword: include duplicate rows
Unions that combines result set
from the different tables
Unions that combines result set
from the same table
Summary

• INNER JOIN
• OUTER JOIN
• UNION

You might also like