Professional Documents
Culture Documents
Relational Algebra To SQ L
Relational Algebra To SQ L
Query optimizer
DBMS
Basic Relational Algebra Operations
(Unary):
Selection, Projection
Set Difference: R – S
Produces a relation with tuples that are in R but NOT in S.
+
Relational Algebra Operations (Set):
Cartesian Product, Intersect
Cartesian Product: R x S
Produces a relation that is concatenation of every tuple of R with
every tuple of S
The Above operations are the 5 fundamental operations of relational
algebra.
Intersection: R S
All tuples that are in both R and S
+
Relational Algebra Operations (Join):
Theta Join, Natural Join
Theta Join: R F S = F (R x S)
Selectall tuples from the Cartesian product of
the two relations, matching condition F
When F contains only equality “=“, it is called
Equijoin
Natural Join: R S
Equijoin with common attributes eliminated
+
Relational Algebra Operations:
Division
Division: R ÷ S
Produce a relation consist of the set of tuples from R that matches the
combination of every tuple in S
R S R÷S
+
Relational algebra, SQL, and the
DBMS?
Optimized
Relational
Relational Query Executable
algebra
SQL algebra execution code
expression
expression plan
Code
parser
generator
Query optimizer
DBMS
+
SQL Introduction
Atomic types:
Characters: CHAR(20), VARCHAR(50), LONG VARCHAR
Numbers: INT, BIGINT, SMALLINT, FLOAT
Others: MONEY, DATETIME, …
MySQL
http://dev.mysql.com/doc/refman/5.0/en/data-types.html
Characters: CHIAR(20), VARCHAR(50), MEDIUMTEXT
Numbers: INT, BIGINT, SMALLINT, FLOAT
Others: (couldn’t find money), DATETIME
SELECT
SELECT <attributes>
<attributes>
FROM
FROM <one
<oneor ormore
morerelations>
relations>
WHERE
WHERE <conditions>
<conditions>
+
Relational algebra queries to SQL
queries
FROM clause produces Cartesian product (x) of listed tables
WHERE clause assigns rows to C in sequence and produces table
Tuple variables (Course C, Teaching T) (aka aliases in MYSQL) clarify meaning (Course AS C)
SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE category=‘Gadgets’
category=‘Gadgets’
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
Input Schema
SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
Output Schema
+
Details
Case insensitive:
Same: SELECT Select select
Same: Product product
Different: ‘Seattle’ ‘seattle’
Constants:
‘abc’ - yes
“abc” - no
+
The LIKE operator
SELECT
SELECT **
FROM
FROM Products
Products
WHERE
WHERE PName
PName LIKE
LIKE ‘%gizmo%’
‘%gizmo%’
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
You can use more than one column in the ORDER BY clause.
Make sure whatever column you are using to sort is in the column-
list of the SELECT
Ties are broken by the second attribute on the ORDER BY list, etc.
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
SELECT
SELECT PName,
PName,Price
Price
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE Manufacturer=CName
Manufacturer=CNameAND
ANDCountry=‘Japan’
Country=‘Japan’
AND
ANDPrice
Price<=
<=200
200
+
Joins
Product
Company
PName Price
SELECT
SELECT PName,
PName,Price
Price
FROM Product, SingleTouch $149.99
FROM Product,Company
Company
WHERE
WHERE Manufacturer=CName
Manufacturer=CNameAND
ANDCountry=‘Japan’
Country=‘Japan’
AND
ANDPrice
Price<=
<=200
200
+
A Subtlety about Joins
SELECT
SELECT Country
Country
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE Manufacturer=CName
Manufacturer=CNameAND
ANDCategory=‘Gadgets’
Category=‘Gadgets’
Unexpected duplicates
+
A Subtlety about Joins
Product
Company
SELECT
SELECT Country
Country
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE Manufacturer=CNameAND
Manufacturer=CName ANDCategory=‘Gadgets’
Category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
+
Tuple Variables
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
CompanyASASyy
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 Product.pname=Purchase.product
WHERE 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’
Beware of duplicates !
+
Removing Duplicates
SELECT
SELECTDISTINCT
DISTINCTCompany.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‘);
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’
+
Subqueries Returning Relations
SELECT
SELECT name
name
FROM
FROM Product
Product
WHERE
WHERE price
price>> ALL
ALL(SELECT
(SELECTprice
price
FROM
FROM Purchase
Purchase
WHERE
WHERE maker=‘Gizmo-Works’)
maker=‘Gizmo-Works’)
+
Question: How are EXISTS and IN
different?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-
operators.html#function_in
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
http://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-
subqueries.html
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== x.title);
title x.title);
+
Complex Correlated Query
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);
+
Aggregation
SELECT
SELECT avg(price)
avg(price) SELECT
SELECT count(*)
count(*)
FROM
FROM Product
Product FROM
FROM Product
Product
WHERE
WHERE maker=“Toyota”
maker=“Toyota” WHERE
WHERE year
year>>1995
1995
We probably want:
SELECT
SELECT Count(DISTINCT
Count(DISTINCTcategory)
category)
FROM
FROM Product
Product
WHERE
WHERE year
year>>1995
1995
+
More Examples
SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
What do
they mean ?
SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
WHERE
WHERE product
product==‘bagel’
‘bagel’
+
Simple Aggregations
Purchase
Product Date Price Quantity
Bagel 10/21 1 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Bagel 10/25 1.50 20
SELECT
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase 50 (= 20+30)
WHERE
WHERE product
product==‘bagel’
‘bagel’
+
Grouping and Aggregation
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBY product
product
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBY product
product
GROUP BY v.s. Nested Quereis
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBY product
product
SELECT
SELECTDISTINCT
DISTINCT x.product,
x.product,(SELECT
(SELECTSum(y.price*y.quantity)
Sum(y.price*y.quantity)
FROM
FROM Purchase
Purchaseyy
WHERE
WHEREx.product
x.product==y.product
y.product
AND
ANDy.date
y.date>>‘10/1/2005’)
‘10/1/2005’)
AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchasexx
WHERE
WHERE x.date
x.date>>‘10/1/2005’
‘10/1/2005’
+
Another Example
What does
it mean ?
SELECT
SELECT product,
product,
sum(price
sum(price**quantity)
quantity)AS
ASSumSales
SumSales
max(quantity)
max(quantity)AS
ASMaxQuantity
MaxQuantity
FROM
FROM Purchase
Purchase
GROUP
GROUPBY
BYproduct
product
+
HAVING Clause
SELECT
SELECT product,
product,Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBYproduct
product
HAVING
HAVING Sum(quantity)
Sum(quantity)>>30
30
HAVING C2
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES
Evaluation steps:
2. Quantifiers
(SELECT
(SELECTR.A,
R.A,R.B
R.B SELECT
SELECTR.A,
R.A,R.B
R.B
FROM
FROM R)R) FROM
FROM RR
EXCEPT
EXCEPT WHERE
WHERE
(SELECT
(SELECTS.A,
S.A,S.B
S.B NOT
NOT EXISTS(SELECT
EXISTS(SELECT**
FROM
FROM S)S) FROM
FROMSS
WHERE
WHERER.A=S.A
R.A=S.Aand
andR.B=S.B)
R.B=S.B)
+
2. Quantifiers
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
Existential: easy !
+
2. Quantifiers
Find all companies that make only products with price < 100
same as:
Find all companies s.t. all of their products have price < 100
Universal: hard !
+
2. Quantifiers
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 Product
FROM Product
WHERE
WHEREProduc.price
Produc.price>=
>=100
100
+
3. Group-by v.s. Nested Query
Author(login,name)
Wrote(login,url)
Find authors who wrote 10 documents:
This is
SQL by
Attempt 1: with nested queries
a novice
SELECT
SELECTDISTINCT
DISTINCTAuthor.name
Author.name
FROM
FROM Author
Author
WHERE
WHERE count(SELECT
count(SELECTWrote.url
Wrote.url
FROM
FROMWrote
Wrote
WHERE
WHEREAuthor.login=Wrote.login)
Author.login=Wrote.login)
>>10
10
+
3. Group-by v.s. Nested Query
SELECT
SELECT Author.name
Author.name This is
FROM
FROM Author,
Author,Wrote
Wrote SQL by
WHERE
WHERE Author.login=Wrote.login
Author.login=Wrote.login an expert
GROUP
GROUPBYBYAuthor.name
Author.name
HAVING
HAVING count(wrote.url)
count(wrote.url)>>10
10
Author(login,name)
Wrote(login,url)
Mentions(url,word)
Find authors with vocabulary 10000 words:
SELECT
SELECT Author.name
Author.name
FROM
FROM Author,
Author,Wrote,
Wrote,Mentions
Mentions
WHERE
WHERE Author.login=Wrote.login
Author.login=Wrote.loginAND
ANDWrote.url=Mentions.url
Wrote.url=Mentions.url
GROUP
GROUPBYBY Author.name
Author.name
HAVING
HAVING count(distinct
count(distinctMentions.word)
Mentions.word)>>10000
10000
+
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Find all stores that sell only products with price > 100
same as:
Find all stores s.t. all their products have price > 100)
SELECT
SELECTStore.name
Store.name
FROM
FROM Store,
Store,Product
Product
WHERE
WHERE Store.sid
Store.sid==Product.sid
Product.sid
GROUP Why both ?
GROUPBY
BY Store.sid,
Store.sid,Store.name
Store.name
HAVING
HAVING100
100<<min(Product.price)
min(Product.price)
SELECT
SELECTStore.name
Store.name
FROM
FROM Store
Store
WHERE
WHERE
Almost equivalent… 100
100<<ALL
ALL(SELECT
(SELECTProduct.price
Product.price
FROM
FROMproduct
product
WHERE
WHEREStore.sid
Store.sid==Product.sid)
Product.sid)
SELECT
SELECTStore.name
Store.name
FROM
FROM Store
Store
WHERE
WHERE Store.sid
Store.sidNOT
NOTININ
(SELECT
(SELECTProduct.sid
Product.sid
FROM
FROMProduct
Product
WHERE
WHERE Product.price
Product.price<=
<=100)
100)
+
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Better:
SELECT
SELECTStore.sname,
Store.sname,x.pname
x.pname
FROM
FROM Store,
Store,Product
Productxx
WHERE
WHERE Store.sid
Store.sid==x.sid
x.sidand
and
But may x.price
x.price>=
>=
return ALL
ALL(SELECT
(SELECTy.price
y.price
multiple FROM
FROMProduct
Productyy
product names WHERE
WHEREStore.sid
Store.sid==y.sid)
y.sid)
per store
+
Two Examples
Finally, choose some pid arbitrarily, if there are many
with highest price:
SELECT
SELECTStore.sname,
Store.sname,max(x.pname)
max(x.pname)
FROM
FROM Store,
Store,Product
Productxx
WHERE
WHERE Store.sid
Store.sid==x.sid
x.sidand
and
x.price
x.price>=
>=
ALL
ALL(SELECT
(SELECTy.price
y.price
FROM
FROMProduct
Productyy
WHERE
WHEREStore.sid
Store.sid==y.sid)
y.sid)
GROUP
GROUPBY
BYStore.sname
Store.sname
+
Announcement
I wanted to confirm for you that the notetaker would be paid in full
and will mostly likely be asked to provide retroactive notes for the
term.
+
Subqueries Returning Relations
SELECT
SELECT name
name
FROM
FROM Product
Product
WHERE
WHERE price
price>> ALL
ALL(SELECT
(SELECTprice
price
FROM
FROM Purchase
Purchase
WHERE
WHERE maker=‘Gizmo-Works’)
maker=‘Gizmo-Works’)
+
Question: How are EXISTS and IN
different?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-
operators.html#function_in
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
http://dev.mysql.com/doc/refman/5.1/en/exists-and-not-exists-
subqueries.html
SELECT
SELECT ** E.g.
FROM
FROM Person
Person age=20
WHERE
WHERE (age
(age << 25)
25) AND
AND height=NULL
weight=200
(height
(height >> 66 OR
OR weight
weight >> 190)
190)
Rule in SQL: include only tuples that yield TRUE
+
Null Values
Unexpected behavior:
SELECT
SELECT **
FROM
FROM Person
Person
WHERE
WHERE age
age << 25
25 OR
OR age
age >=
>= 25
25
SELECT
SELECT **
FROM
FROM Person
Person
WHERE
WHERE age
age << 25
25 OR
OR age
age >=
>= 25
25 OR
OR
age
age IS
IS NULL
NULL
Now it includes all Persons
+
Outerjoins
SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product
ProductJOIN
JOINPurchase
PurchaseON
ON
Product.name
Product.name==Purchase.prodName
Purchase.prodName
SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product
ProductLEFT
LEFTOUTER
OUTERJOIN
JOINPurchase
PurchaseON
ON
Product.name
Product.name==Purchase.prodName
Purchase.prodName
SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
+ FROM
FROM Product
ProductLEFT
LEFTOUTER
OUTERJOIN
Product.name
JOINPurchase
PurchaseON
ON
Product.name==Purchase.prodName
Purchase.prodName
Product Purchase
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
+
Application
SELECT
SELECTProduct.name,
Product.name,count(*)
count(*)
FROM
FROM Product,
Product,Purchase
Purchase
WHERE
WHERE Product.name
Product.name==Purchase.prodName
Purchase.prodName
and
and Purchase.month
Purchase.month==‘September’
‘September’
GROUP BY Product.name
GROUP BY Product.name
What’s wrong ?
+
Application
SELECT
SELECTProduct.name,
Product.name,count(*)
count(*)
FROM
FROM Product
ProductLEFT
LEFTOUTER
OUTERJOINJOINPurchase
PurchaseON
ON
Product.name
Product.name==Purchase.prodName
Purchase.prodName
and
and Purchase.month
Purchase.month==‘September’
‘September’
GROUP
GROUPBY
BYProduct.name
Product.name
Insertions
Deletions
Updates
General form:
INSERT
INSERT INTO
INTO R(A1,….,
R(A1,….,An)
An) VALUES
VALUES (v1,….,
(v1,….,vn)
vn)
INSERT
INSERT INTO
INTO Purchase(buyer,
Purchase(buyer,seller,
seller,product,
product,store)
store)
VALUES
VALUES (‘Joe’,
(‘Joe’,‘Fred’,
‘Fred’,‘wakeup-clock-espresso-machine’,
‘wakeup-clock-espresso-machine’,
‘The
‘TheSharper
SharperImage’)
Image’)
INSERT
INSERT INTO
INTO PRODUCT(name)
PRODUCT(name)
SELECT
SELECT DISTINCT
DISTINCT Purchase.product
Purchase.product
FROM
FROM Purchase
Purchase
WHERE
WHERE Purchase.date
Purchase.date>>“10/26/01”
“10/26/01”
Product(name,
Product(name,listPrice,
listPrice,category)
category)
Purchase(prodName,
Purchase(prodName,buyerName,
buyerName,price)
price)
Purchase
Product
prodName buyerName price
name listPrice category
camera John 200
INSERT
INSERT INTO
INTO Product(name)
Product(name)
SELECT
SELECT DISTINCT
DISTINCT prodName
prodName
FROM
FROM Purchase
Purchase
WHERE
WHERE prodName
prodName NOT
NOTININ(SELECT
(SELECT name
nameFROM
FROM Product)
Product)
camera - -
+
Insertion: an Example
INSERT
INSERT INTO
INTO Product(name,
Product(name,listPrice)
listPrice)
SELECT
SELECT DISTINCT
DISTINCT prodName,
prodName,price
price
FROM
FROM Purchase
Purchase
WHERE
WHERE prodName
prodName NOT
NOTININ(SELECT
(SELECT name
nameFROM
FROM Product)
Product)
camera 200 -
Example:
DELETE
DELETE FROM
FROM PURCHASE
PURCHASE
WHERE
WHERE seller
seller==‘Joe’
‘Joe’ AND
AND
product
product==‘Brooklyn
‘BrooklynBridge’
Bridge’
Example:
UPDATE
UPDATE PRODUCT
PRODUCT
SET
SET price
price==price/2
price/2
WHERE
WHERE Product.name
Product.name IN IN
(SELECT
(SELECTproduct
product
FROM Purchase
FROM Purchase
WHERE
WHERE Date
Date=‘Oct,
=‘Oct,25,
25,1999’);
1999’);