You are on page 1of 10

JOIN

mySQL

join.pptx Trang 1 / 10
join.pptx Trang 2 / 10
join.pptx Trang 3 / 10
The relationship between the two tables is specified by the
customer_id key . It is the "primary key" in customer table and
the "foreign key" in order table.

CI firstname lastname email address city state ZC


1 George Washing gwashington 3200 Mt Vernon Mount VA 22121 OrI order_date amount CI
ton @usa.gov Hwy Vernon 1 07/04/1776 $234.56 1
2 John Adams jadams@usa 1250 Hancock St Quincy MA 02169
.gov
2 03/14/1760 $78.50 3
3 Thomas Jefferso tjefferson@u 931 Thomas Charlottesv VA 22902 3 05/23/1784 $124.00 2
n sa.gov Jefferson Pkwy ille 4 09/03/1790 $65.50 3
4 James Madison jmadison@us 11350 Constitution Orange VA 22960
a.gov Hwy
5 07/21/1795 $25.50 10
5 James Monroe jmonroe@us 2050 James Charlottesv VA 22902 6 11/27/1787 $14.40 9
a.gov Monroe Parkway ille

join.pptx Trang 4 / 10
The relationship between the two tables is specified by the
customer_id key . It is the "primary key" in customer table and
the "foreign key" in order table.

CI firstname lastname email address city state ZC


1 George Washingt gwashingto 3200 Mt Vernon Mount VA 22121 OrI order_date amount CI
on n@usa.gov Hwy Vernon 1 07/04/1776 $234.56 1
2 John Adams jadams@us 1250 Hancock St Quincy MA 02169
a.gov
2 03/14/1760 $78.50 3
3 Thomas Jefferson tjefferson@ 931 Thomas Charlottesv VA 22902 3 05/23/1784 $124.00 2
usa.gov Jefferson Pkwy ille 4 09/03/1790 $65.50 3
4 James Madison jmadison@ 11350 Constitution Orange VA 22960
usa.gov Hwy
5 07/21/1795 $25.50 10
5 James Monroe jmonroe@u 2050 James Charlottesv VA 22902 6 11/27/1787 $14.40 9
sa.gov Monroe Parkway ille

join.pptx Trang 5 / 10
Inner Join
SELECT firstname, lastname, amount, c.CI
FROM customer c
INNER JOIN order o
ON c.CI= o.CI

CI first_name last_name order_amount


1 George Washington $234.56
2 John Adams $124.00
3 Thomas Jefferson $78.50
3 Thomas Jefferson $65.50

join.pptx Trang 6 / 10
Left join
SELECT firstname, lastname, amount, c.CI, o.CI
FROM customer c
LEFT JOIN order o
ON c.CI= o.CI

c.CI first_name last_name order_amount o.CI


1 George Washingto $234.56 1
n
2 John Adams $124.00 2
3 Thomas Jefferson $78.50 3
3 Thomas Jefferson $65.50 3
4 James Madison NULL NULL
5 James Monroe NULL NULL
join.pptx Trang 7 / 10
Right join
SELECT firstname, lastname, amount, c.CI, o.CI
FROM customer c
RIGHT JOIN order o
ON c.CI= o.CI

c.CI first_name last_name order_amount o.CI


1 George Washington $234.56 1
2 Thomas Jefferson $78.50 2
3 John Adams $124.00 3
3 Thomas Jefferson $65.50 3
NULL NULL NULL $25.50 10
NULL NULL NULL $14.40 9
Right joining the orders table to the customers table

join.pptx Trang 8 / 10
Left vs Right join
• The order in which the tables are joined is important.
• Right join the customer table to the order table?
 The result would be the same as left joining the order table to the
customer table

join.pptx Trang 9 / 10
Full join
SELECT firstname, lastname, amount
FROM customer c
FULL JOIN order o
ON c.CI= o.CI

c.CI first_name last_name order_amount c.CI


1 George Washington $234.56 1
2 Thomas Jefferson $78.50 2
3 John Adams $124.00 3
3 Thomas Jefferson $65.50 3
NULL NULL NULL $25.50 10
NULL NULL NULL $14.40 9
4 James Madison NULL NULL
5 James Monroe NULL NULL
join.pptx Trang 10 / 10

You might also like