Professional Documents
Culture Documents
1
Agenda
• Finish subqueries, correlated queries.
• Grouping and aggregation
• Creating a schema
• Modifying the database
• Defining views
2
Subqueries Returning Relations
You can also use: s > ALL R
s > ANY R
EXISTS R
Product ( pname, price, category, maker)
Find products that are more expensive than all those produced
By “Gizmo-Works”
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’) 3
Conditions on Tuples
SELECT
SELECT DISTINCT
DISTINCTCompany.name
Company.name
FROM
FROM Company,
Company,Product
Product
WHERE
WHERE Company.name=
Company.name=Product.maker
Product.maker
AND
AND (Product.name,price)
(Product.name,price) IN
IN
(SELECT
(SELECTPurchase.product,
Purchase.product,Purchase.price)
Purchase.price)
FROM
FROM Purchase
Purchase
WHERE
WHEREPurchase.buyer
Purchase.buyer==“Joe
“JoeBlow”);
Blow”);
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);
Note (1) scope of variables (2) this can still be expressed as single SFW
5
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);
6
Aggregation
SELECT
SELECT Avg(price)
Avg(price)
FROM
FROM Product
Product
WHERE
WHERE maker=“Toyota”
maker=“Toyota”
7
Aggregation: Count
SELECT
SELECT Count(*)
Count(*)
FROM
FROM Product
Product
WHERE
WHERE year
year>>1995
1995
8
Aggregation: Count
Better:
11
Grouping and Aggregation
Usually, we want aggregations on certain parts of the relation.
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>“10/1”
“10/1”
GROUPBY
GROUPBY product
product
13
First compute the FROM-WHERE clauses
(date > “10/1”) then GROUP BY product:
14
Then, aggregate
Product TotalSales
Bagel $29.75
Banana $12.48
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>“10/1”
“10/1”
GROUPBY
GROUPBY product
product
15
GROUP BY v.s. Nested Queries
SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE datedate>>“10/1”
“10/1”
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’)
‘10/1’)
AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchasexx
WHERE
WHERE x.date
x.date>>“10/1”
“10/1” 16
Another Example
Product SumSales MaxQuantity
Banana $12.48 17
Bagel $29.75 20
For every product, what is the total sales and max quantity sold?
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 17
HAVING Clause
Same query, except that we consider only products that had
at least 100 buyers.
SELECT
SELECT product,
product,Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase
WHERE
WHERE datedate>>“9/1”
“9/1”
GROUP
GROUPBYBYproduct
product
HAVING
HAVING Sum(quantity)
Sum(quantity)>>30
30
Evaluation steps:
1. Compute the FROM-WHERE part, obtain a table with all attributes
in R1,…,Rn
2. Group by the attributes a1,…,ak
3. Compute the aggregates in C2 and keep only groups satisfying C2
4. Compute aggregates in S and return the result
20
Aggregation
Author(login,name)
Document(url, title)
Wrote(login,url)
Mentions(url,word)
21
• Find all authors who wrote at least 10
documents:
This is
• Attempt 1: with nested queries SQL by
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
22
• Find all authors who wrote at least 10
documents:
• Attempt 2: SQL style (with GROUP BY)
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
(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)
25
Null Values and Outerjoins
• If x=Null then 4*(3-x)/7 is still NULL
26
Null Values and Outerjoins
• C1 AND C2 = min(C1, C2)
• C1 OR C2 = max(C1, C2)
• NOT C1 = 1 – C1
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
27
Null Values and Outerjoins
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
SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product
ProductJOIN
JOINPurchase
PurchaseON
ON
Product.name
Product.name==Purchase.prodName
Purchase.prodName
Same as:
SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product,
Product,Purchase
Purchase
WHERE
But Products that never Product.name
WHERE will be lost=!=Purchase.prodName
Product.name
sold Purchase.prodName
30
Null Values and Outerjoins
Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)
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
31
Product Purchase
Name Category ProdName Store
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL 32
Outer Joins
INSERT
INSERT INTO
INTO R(A1,….,
R(A1,….,An)
An) VALUES
VALUES (v1,….,
(v1,….,vn)
vn)
SELECT
SELECT DISTINCT
DISTINCT Purchase.product
Purchase.product
FROM
FROM Purchase
Purchase
WHERE
WHERE Purchase.date
Purchase.date>>“10/26/01”
“10/26/01”
SELECT
SELECT DISTINCT
DISTINCT prodName
prodName
FROM
FROM Purchase
Purchase
WHERE
WHERE prodName
prodName NOT
NOTININ(SELECT
(SELECT name
nameFROM
FROM Product)
Product)
camera - -
38
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 -
DELETE
DELETE FROM
FROM PURCHASE
PURCHASE
WHERE
WHERE seller
seller==‘Joe’
‘Joe’ AND
AND
product
product==‘Brooklyn
‘BrooklynBridge’
Bridge’
UPDATE
UPDATE PRODUCT
PRODUCT
SET
SET price
price==price/2
price/2
WHERE
WHERE Product.name
Product.name ININ
(SELECT
(SELECTproduct
product
FROM
FROM Purchase
Purchase
WHERE
WHERE DateDate=‘Oct,
=‘Oct,25,
25,1999’);
1999’);
41
Data Definition in SQL
So far we have see the Data Manipulation Language, DML
Next: Data Definition Language (DDL)
Data types:
Defines the types.
• Create tables
• Delete tables
• Modify table schema
42
Indexes: to improve performance
Data Types in SQL
• Characters:
– CHAR(20) -- fixed length
– VARCHAR(40) -- variable length
• Numbers:
– INT, REAL plus variations
• Times and dates:
– DATE, DATETIME (SQL Server only)
• To reuse domains:
CREATE DOMAIN address AS VARCHAR(55)
43
Creating Tables
Example:
CREATE
CREATE TABLE
TABLEPerson(
Person(
name
name VARCHAR(30),
VARCHAR(30),
social-security-number
social-security-number INT,
INT,
age
age SHORTINT,
SHORTINT,
city
city VARCHAR(30),
VARCHAR(30),
gender
gender BIT(1),
BIT(1),
Birthdate
Birthdate DATE
DATE
););
44
Deleting or Modifying a Table
Deleting:
Example: DROP
DROPPerson;
Person; Exercise with care !!
CREATE
CREATE TABLE
TABLEPerson(
Person(
name
name VARCHAR(30),
VARCHAR(30),
social-security-number
social-security-number INT,
INT,
age
age SHORTINT
SHORTINT DEFAULT
DEFAULT100,
100,
city
city VARCHAR(30)
VARCHAR(30)DEFAULT
DEFAULT ‘Seattle’,
‘Seattle’,
gender
gender CHAR(1)
CHAR(1) DEFAULT
DEFAULT ‘?’,
‘?’,
Birthdate
Birthdate DATE
DATE
SELECT
SELECT**
FROM
FROM Person
Person
WHERE
WHERE name
name==“Smith”
“Smith”
Syntax:
CREATE
CREATEINDEX
INDEX nameIndex
nameIndexON
ONPerson(name)
Person(name)
49
Creating Indexes
Indexes can be created on more than one attribute:
CREATE
CREATEINDEX
INDEXdoubleindex
doubleindexONON
Example: Person
Person(age,
(age,city)
city)
SELECT
SELECT**
Helps in: FROM
FROM Person
Person
WHERE
WHEREage
age==55
55AND
ANDcity
city==“Seattle”
“Seattle”
SELECT
SELECT**
But not in: FROM
FROM Person
Person
WHERE
WHEREcity
city==“Seattle”
“Seattle” 50
Creating Indexes
CREATE
CREATEINDEX
INDEXageIndex
ageIndexON
ON Person
Person(age)
(age)
CREATE
CREATEVIEW
VIEW Developers
DevelopersASAS
SELECT
SELECTname,
name,project
project
FROM
FROM Employee
Employee
WHERE
WHEREdepartment
department==“Development”
“Development”
CREATE
CREATEVIEW
VIEW Seattle-view
Seattle-view AS
AS
SELECT
SELECT buyer,
buyer,seller,
seller,product,
product,store
store
FROM
FROM Person,
Person,Purchase
Purchase
WHERE
WHERE Person.city
Person.city==“Seattle”
“Seattle” AND
AND
Person.name
Person.name==Purchase.buyer
Purchase.buyer
SELECT
SELECT name,
name,store
store
FROM
FROM Seattle-view,
Seattle-view,Product
Product
WHERE
WHERE Seattle-view.product
Seattle-view.product==Product.name
Product.name AND
AND
Product.category
Product.category==“shoes”
“shoes”
54
What Happens When We Query
a View ?
SELECT
SELECT name,
name,Seattle-view.store
Seattle-view.store
FROM
FROM Seattle-view,
Seattle-view,Product
Product
WHERE
WHERE Seattle-view.product
Seattle-view.product==Product.name
Product.name AND
AND
Product.category
Product.category==“shoes”
“shoes”
SELECT
SELECT name,
name,Purchase.store
Purchase.store
FROM
FROM Person,
Person,Purchase,
Purchase,Product
Product
WHERE
WHERE Person.city
Person.city==“Seattle”
“Seattle” AND
AND
Person.name
Person.name==Purchase.buyer
Purchase.buyer AND
AND
Purchase.poduct
Purchase.poduct==Product.name
Product.name AND
AND
Product.category
Product.category==“shoes”
“shoes” 55
Types of Views
• Virtual views:
– Used in databases
– Computed only on-demand – slow at runtime
– Always up to date
• Materialized views
– Used in data warehouses
– Precomputed offline – fast at runtime
– May have stale data
56
Updating Views
How can I insert a tuple into a table that doesn’t exist?
CREATE
CREATEVIEW
VIEW Developers
DevelopersASAS
SELECT
SELECTname,
name,project
project
FROM
FROM Employee
Employee
WHERE
WHEREdepartment
department==“Development”
“Development”
It becomes: INSERT
INSERTINTO
INTO Employee
Employee
VALUES(NULL,
VALUES(NULL,“Joe”,
“Joe”,NULL,
NULL,“Optimizer”,
“Optimizer”,NULL)
NULL)
57
Non-Updatable Views
CREATE
CREATEVIEW
VIEW Seattle-view
Seattle-view AS
AS
SELECT
SELECT seller,
seller,product,
product,store
store
FROM
FROM Person,
Person,Purchase
Purchase
WHERE
WHERE Person.city
Person.city==“Seattle”
“Seattle” AND
AND
Person.name
Person.name==Purchase.buyer
Purchase.buyer
We need to add “Joe” to Person first, but we don’t have all its attributes
58