0% found this document useful (0 votes)
10 views

SQL Join

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL Join

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

SQL - JOIN

The LIKE operator


SELECT
SELECT **
FROM
FROM Products
Products
WHERE
WHERE PName
PName LIKE
LIKE ‘%gizmo%’
‘%gizmo%’
• s LIKE p: pattern matching on strings
• p may contain two special symbols:
– % = any sequence of characters
– _ = any single character
Eliminating Duplicates
Category
SELECT
SELECT DISTINCT
DISTINCTcategory
category Gadgets
FROM
FROM Product
Product Photography
Household

Compare to:
Category
Gadgets
SELECT
SELECT category
category Gadgets
FROM
FROM Product
Product
Photography
Household
Ordering the Results
SELECT
SELECT pname,
pname,price,
price,manufacturer
manufacturer
FROM
FROM Product
Product
WHERE
WHERE category=‘gizmo’
category=‘gizmo’AND
ANDprice
price>>50
50
ORDER
ORDERBY
BY price,
price,pname
pname

Ties are broken by the second attribute on the ORDER BY list, etc.

Ordering is ascending, unless you specify the DESC keyword.


Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)

Find all products under $200 manufactured in Japan;


return their names and prices.
Join
between Product
SELECT
SELECT PName,
PName,Price
Price
FROM Product, and Company
FROM Product,Company
Company
WHERE
WHERE Manufacturer=CName
Manufacturer=CNameAND
ANDCountry=‘Japan’
Country=‘Japan’
AND
ANDPrice
Price<=
<=200
200
Joins
Product Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan

SingleTouch $149.99 Photography Canon Hitachi 15 Japan

MultiTouch $203.99 Household Hitachi

SELECT
SELECT PName,
PName,Price
Price
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE Manufacturer=CName
Manufacturer=CNameAND
ANDCountry=‘Japan’
Country=‘Japan’ PName Price
AND
ANDPrice
Price<=
<=200
200 SingleTouch $149.99
Tuple Variables
Person(pname, address, worksfor)
Company(cname, address) Which
SELECT
SELECT DISTINCT
DISTINCTpname,
pname,address
address address ?
FROM
FROM Person,
Person,Company
Company
WHERE
WHERE worksfor
worksfor==cname
cname
SELECT
SELECT DISTINCT
DISTINCTPerson.pname,
Person.pname,Company.address
Company.address
FROM
FROM Person,
Person,Company
Company
WHERE
WHERE Person.worksfor
Person.worksfor==Company.cname
Company.cname
SELECT
SELECT DISTINCT
DISTINCTx.pname,
x.pname,y.address
y.address
FROM
FROM Person
PersonAS
ASx,x,Company
CompanyAS ASyy
WHERE
WHERE x.worksfor
x.worksfor==y.cname
y.cname
Subqueries Returning Relations
Company(name, city)
Product(pname, maker)
Purchase(id, product, buyer)
Return cities where one can find companies that manufacture
products bought by Joe Blow
SELECT
SELECT Company.city
Company.city
FROM
FROM Company
Company
WHERE
WHERE Company.name
Company.name ININ
(SELECT
(SELECTProduct.maker
Product.maker
FROM
FROM Purchase
Purchase, ,Product
Product
WHERE
WHEREProduct.pname=Purchase.product
Product.pname=Purchase.product
AND
ANDPurchase
Purchase.buyer
.buyer==‘Joe
‘JoeBlow‘);
Blow‘);
Subqueries Returning Relations

Is it equivalent to this ?


 SELECT
SELECT Company.city
Company.city

 FROM
FROM Company,
Company,Product,
Product,Purchase
Purchase

 WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker

 AND
AND Product.pname
Product.pname ==Purchase.product
Purchase.product

 AND
AND Purchase.buyer
Purchase.buyer==‘Joe
‘JoeBlow’
Blow’
Removing Duplicates

 SELECT
SELECTDISTINCT
DISTINCTCompany.city
Company.city

 FROM
FROM Company
Company

 WHERE
WHERE Company.name
Company.name IN
IN

 (SELECT
(SELECTProduct.maker
Product.maker

 FROM
FROM Purchase
Purchase,,Product
Product

 WHERE
WHEREProduct.pname=Purchase.product
Product.pname=Purchase.product

 AND
ANDPurchase
Purchase.buyer
.buyer==‘Joe
‘JoeBlow‘);
Blow‘);


 SELECT
SELECTDISTINCT
DISTINCTCompany.city
Company.city 
Now

 FROM
FROM Company,
Company,Product,
Product,Purchase
Purchase 
they are

 WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker 
equivalent

 AND
AND Product.pname
Product.pname ==Purchase.product
Purchase.product

 AND
AND Purchase.buyer
Purchase.buyer==‘Joe
‘JoeBlow’
Blow’
Correlated Queries

Movie (title, year, director, length)

Find movies whose title appears more than once.

correlation


 SELECT
SELECTDISTINCT
DISTINCTtitle
title

 FROM
FROM Movie
MovieAS
ASxx

 WHERE
WHERE year
year<>
<>ANY
ANY

 (SELECT
(SELECT year
year

 FROM
FROM Movie
Movie

 WHERE
WHERE title
title== x.title);
x.title);
Complex Correlated Query
Product ( pname, price, category, maker, year)
• Find products (and their manufacturers) that are more expensive than all
products made by the same manufacturer before 1972
SELECT
SELECTDISTINCT
DISTINCT pname,
pname,maker
maker
FROM
FROM Product
ProductAS
ASxx
WHERE
WHERE price
price>>ALL
ALL (SELECT
(SELECT price
price

 FROM
FROM Product
ProductAS
ASyy

 WHERE
WHERE x.maker
x.maker==y.maker
y.makerAND
ANDy.year
y.year<<1972);
1972);
Quantifiers
Product ( pname, price, company)
Company( cname, city)


Find all companies that make some products with price < 100

SELECT
SELECTDISTINCT
DISTINCT Company.cname
Company.cname
FROM
FROM Company,
Company,Product
Product
WHERE
WHERE Company.cname
Company.cname==Product.company
Product.companyand
andProduct.price
Product.price<<100
100
Quantifiers
Product ( pname, price, company)
Company( cname, city)


Find all companies that make only products with price < 100

same as:

Find all companies s.t. all of their products have price < 100
Quantifiers

1. Find the other companies: i.e. s.t. some product  100
SELECT
SELECTDISTINCT
DISTINCT Company.cname
Company.cname
FROM
FROM Company
Company
WHERE
WHERE Company.cname
Company.cnameIN
IN(SELECT
(SELECTProduct.company
Product.company
FROM
FROMProduct
Product
WHERE
WHEREProduc.price
Produc.price>=
>=100
100

2. Find all companies s.t. all their products have price < 100
SELECT
SELECTDISTINCT
DISTINCT Company.cname
Company.cname
FROM
FROM Company
Company
WHERE
WHERE Company.cname
Company.cnameNOT
NOTININ(SELECT
(SELECTProduct.company
Product.company
FROM
FROMProduct
Product
WHERE
WHEREProduc.price
Produc.price>=
>=100
100
Different
Different Types
Types of of
SQLSQL JOINs
JOINs
Here are the different types of the JOINs in SQL:

(INNER) JOIN: Returns records that have matching values in both tables

LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table

RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table

FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
Sample Tables
TableA TableB
PK Value PK Value
1 FOX 1 TROT
2 COP 2 CAR
3 TAXI 3 CAB
6 WASHINGTON 6 MONUMENT
7 DELL 7 PC
5 ARIZONA 8 MICROSOFT
4 LINCOLN 9 APPLE
10 LUCENT 11 SCOTCH
Inner Join
• Inner join produces only the
set of records that match in
both Table A and Table B
• Most commonly used, best
understood join

INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Inner Join
TableA Value TableB PK
PK Value

FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
WASHINGTON 6 6 MONUMENT
DELL
SELECT * 7 7 PC
FROM TableA INNER JOIN TableB
ON TableA.PK = TableB.PK;

This is the same as doing


SELECT *
FROM TableA, TableB
WHERE TableA.PK = TableB.PK;
Inner Join (continued)
TableA PK TableB PK
SELECT * Value Value

FROM TableA 2 COP 1 TROT


INNER JOIN TableB 3 TAXI 1 TROT
3 TAXI 2 CAR
ON TableA.PK > TableB.PK;
4 LINCOLN 1 TROT
4 LINCOLN 2 CAR
4 LINCOLN 3 CAB
5 ARIZONA 1 TROT

5 ARIZONA 2 CAR

5 ARIZONA 3 CAB

… More… Rows…
Left Outer Join
• Left outer join produces a
complete set of records
from Table A, with the
matching records (where
available) in Table B. If there
is no match, the right side
will contain null.
Left Outer Join
TableA Value TableB PK
PK Value

FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
LUCENT 10 NULL NULL

• SELECT *
• FROM TableA LEFT OUTER JOIN TableB
• ON TableA.PK = TableB.PK
SQL Joins…Example
SELECT S.name, E.classid
FROM Students S LEFT OUTER JOIN Enrolled E
ON S.sid=E.sid
S.name S.sid E.sid E.classid
S E 11111 History105
Jones 11111 11111 DataScience194
Smith 22222
Brown 33333 22222 French150
S.name E.classid 44444 English10

Jones History105
Jones DataScience194

Smith French150
Brown NULL
Right Outer Join
• Right outer join produces a
complete set of records
from Table B, with the
matching records (where
available) in Table A. If there
is no match, the left side
will contain null.
Right Outer Join
Note: RIGHT are not currently supported

TableA Value TableB PK


PK Value

FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH

• SELECT *
• FROM TableA RIGHT OUTER JOIN TableB
• ON TableA.PK = TableB.PK
Full Outer Join
• Full outer join produces the
set of all records in Table A
and Table B, with matching
records from both sides
where available. If there is
no match, the missing side
will contain null.
Full Outer Join
TableA TableB
Value PK PK Value
FOX 1 1 TROT
COP 2 2 CAR
TAXI 3 3 CAB
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
WASHINGTON 6 6 MONUMENT
DELL 7 7 PC
LUCENT 10 NULL NULL
NULL NULL 8 MICROSOFT
NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
• SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK
= TableB.PK
Cross Join
• A cross join is a Cartesian Product join – it is every record in
Table A combined with every record in Table B.
• It gives the same results as not using a WHERE clause when
querying two tables in MySQL
• SELECT * from TableA CROSS JOIN TableB
• SELECT * from TableA, TableB
Cross Join
SQL Joins
SELECT *
FROM Students S CROSS JOIN Enrolled E

S S.name S.sid E E.sid E.classid


11111 History105
Jones 11111 11111 DataScience194
Smith 22222
22222 French150
S.name S.sid E.sid E.classid

Jones 11111 11111 History105


Jones 11111 11111 DataScience194

Jones 11111 22222 French150


Smith 22222 11111 History105
Smith 22222 11111 DataScience194
Theta Joins
SELECT *
FROM Students S, Enrolled E
WHERE S.sid <= E.sid

E.sid E.classid
S S.name S.sid E
11111 History105
Jones 11111 11111 DataScience194
Smith 22222
22222 French150
S.name S.sid E.sid E.classid

Jones 11111 11111 History105


Jones 11111 11111 DataScience194

Jones 11111 22222 French150


Smith 22222 22222 French150
Left Join Excluding Inner Join
• This query will return all of
the records in the left table
(table A) that do not match
any records in the right
table (table B).
Left Join Excluding Inner Join
TableA Value TableB PK
PK Value

LINCOLN 4 NULL NULL


ARIZONA 5 NULL NULL
LUCENT 10 NULL NULL

• SELECT * FROM TableA LEFT JOIN TableB


• ON TableA.PK = TableB.PK
WHERE TableB.PK IS NULL;
• Perform left outer join, then exclude the records we don't want
from the right side via a where clause.
Right Join Excluding Inner Join
• This query will return all of
the records in the right table
(table B) that do not match
any records in the left table
(table A).
Right Join Excluding Inner Join
TableA Value TableB PK
PK Value

NULL NULL 8 MICROSOFT


NULL NULL 9 APPLE
NULL NULL 11 SCOTCH

• SELECT * FROM TableA RIGHT JOIN TableB


• ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL
• Perform right outer join, then exclude the records we don't
want from the left side via a where clause.
Full Outer Excluding Inner Join
• This query will return all of
the records in Table A and
Table B that do not have a
matching record in the
other table.

• In general, Not used


Full Outer Excluding Inner Join
TableA Value TableB PK
PK Value

NULL NULL 8 MICROSOFT


NULL NULL 9 APPLE
NULL NULL 11 SCOTCH
LINCOLN 4 NULL NULL
ARIZONA 5 NULL NULL
LUCENT 10 NULL NULL

• SELECT * FROM TableA FULL OUTER JOIN TableB


• ON TableA.PK = TableB.PK
WHERE TableA.PK IS NULL OR TableB.PK IS NULL

You might also like