im433-industrial data systems management lecture 5: sql

23
IM433-Industrial Data Systems Management Lecture 5: SQL

Upload: hortense-hawkins

Post on 16-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IM433-Industrial Data Systems Management Lecture 5: SQL

IM433-Industrial Data Systems Management

Lecture 5: SQL

Page 2: IM433-Industrial Data Systems Management Lecture 5: SQL

Relational Schema

Another way of writing out a relational schema

Underline the primary key

Product(PName, Price, Category, Manfacturer)

Page 3: IM433-Industrial Data Systems Management Lecture 5: SQL

Simple SQL Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

SELECT *FROM ProductWHERE category=‘Gadgets’

Product

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks“selection”

Page 4: IM433-Industrial Data Systems Management Lecture 5: SQL

Simple SQL Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

Product

PName Price Manufacturer

SingleTouch $149.99 Canon

MultiTouch $203.99 Hitachi

“selection” and“projection”

Page 5: IM433-Industrial Data Systems Management Lecture 5: SQL

The LIKE operator

s LIKE p: pattern matching on strings p may contain two special symbols:

% = any sequence of characters _ = any single character

SELECT *FROM ProductsWHERE PName LIKE ‘%gizmo%’

Page 6: IM433-Industrial Data Systems Management Lecture 5: SQL

Eliminating Duplicates

SELECT DISTINCT categoryFROM Product

Compare to:

SELECT categoryFROM Product

Category

Gadgets

Gadgets

Photography

Household

Category

Gadgets

Photography

Household

Page 7: IM433-Industrial Data Systems Management Lecture 5: SQL

Ordering the Results

SELECT pname, price, manufacturerFROM ProductWHERE category=‘gizmo’ AND price > 50ORDER BY price, pname

Ordering is ascending, unless you specify the DESC keyword.

Page 8: IM433-Industrial Data Systems Management Lecture 5: SQL

SELECT CategoryFROM ProductORDER BY PName

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

?SELECT DISTINCT categoryFROM ProductORDER BY category

SELECT DISTINCT categoryFROM ProductORDER BY PName

?

?

Page 9: IM433-Industrial Data Systems Management Lecture 5: SQL

Company and Product

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Company

CName StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

Key

Foreignkey

Page 10: IM433-Industrial Data Systems Management Lecture 5: SQL

Joins

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

Find all products under $200 manufactured in Japan;return their names and prices.

SELECT PName, PriceFROM Product, CompanyWHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200

Joinbetween Product

and Company

Page 11: IM433-Industrial Data Systems Management Lecture 5: SQL

Joins

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product Company

Cname StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

PName Price

SingleTouch $149.99

SELECT PName, PriceFROM Product, CompanyWHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200

Page 12: IM433-Industrial Data Systems Management Lecture 5: SQL

More Joins

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

Find all Chinese companies that manufacture products both in the ‘electronic’ and ‘toy’ categories

SELECT cname

FROM

WHERE

Page 13: IM433-Industrial Data Systems Management Lecture 5: SQL

A Subtlety about Joins

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

Find all countries that manufacture some product in the ‘Gadgets’ category.

SELECT CountryFROM Product, CompanyWHERE Manufacturer=CName AND Category=‘Gadgets’

Unexpected duplicates

Page 14: IM433-Industrial Data Systems Management Lecture 5: SQL

A Subtlety about Joins

Name Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product Company

Cname StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

Country

??

??

What isthe problem ?

What’s thesolution ?

SELECT CountryFROM Product, CompanyWHERE Manufacturer=CName AND Category=‘Gadgets’

Page 15: IM433-Industrial Data Systems Management Lecture 5: SQL

Subqueries Returning Relations

SELECT Company.ccity FROM Company WHERE Company.cname IN (SELECT Product.cname FROM Purchase , Product WHERE Product.pname=Purchase.pname AND Purchase .buyer = ‘Joe Blow’);

Return cities where one can find companies that manufacture products bought by Joe Blow

Company(cname, ccity)Product(pname, cname)Purchase(id, pname, buyer)

Page 16: IM433-Industrial Data Systems Management Lecture 5: SQL

Subqueries Returning Relations

SELECT Company.ccity FROM Company, Product, Purchase WHERE Company.cname= Product.cname AND Product.pname = Purchase.pname AND Purchase.buyer = ‘Joe Blow’

Is it equivalent to this ?

Beware of duplicates !

Page 17: IM433-Industrial Data Systems Management Lecture 5: SQL

Removing Duplicates

Nowthey are equivalent

SELECT DISTINCT Company.city FROM Company WHERE Company.name IN (SELECT Product.maker FROM Purchase , Product WHERE Product.pname=Purchase.product AND Purchase .buyer = ‘Joe Blow‘);

SELECT DISTINCT Company.city FROM Company, Product, Purchase WHERE Company.name= Product.maker AND Product.pname = Purchase.product AND Purchase.buyer = ‘Joe Blow’

Page 18: IM433-Industrial Data Systems Management Lecture 5: SQL

Subqueries Returning Relations

SELECT pname FROM Product WHERE price > ALL (SELECT price FROM Purchase WHERE cname=‘Gizmo-Works’)

Product ( pname, price, category, cname)Find products that are more expensive than all those producedBy “Gizmo-Works”

You can also use: s > ALL R s > ANY R EXISTS R

Page 19: IM433-Industrial Data Systems Management Lecture 5: SQL

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

Very powerful ! Also much harder to optimize.

SELECT DISTINCT pname, makerFROM Product AS xWHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972);

Page 20: IM433-Industrial Data Systems Management Lecture 5: SQL

Aggregation

SELECT count(*)FROM ProductWHERE year > 1995

Except count, all aggregations apply to a single attribute

SELECT avg(price)FROM ProductWHERE maker=“Toyota”

SQL supports several aggregation operations:

sum, count, min, max, avg

Page 21: IM433-Industrial Data Systems Management Lecture 5: SQL

COUNT applies to duplicates, unless otherwise stated:

SELECT Count(category) FROM ProductWHERE year > 1995

same as Count(*)

We probably want:

SELECT Count(DISTINCT category)FROM ProductWHERE year > 1995

Aggregation: Count

Page 22: IM433-Industrial Data Systems Management Lecture 5: SQL

Purchase(product, date, price, quantity)

More Examples

SELECT Sum(price * quantity)FROM Purchase

SELECT Sum(price * quantity)FROM PurchaseWHERE product = ‘bagel’

What dothey mean ?

Page 23: IM433-Industrial Data Systems Management Lecture 5: SQL

Simple AggregationsPurchase

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 Sum(price * quantity)FROM PurchaseWHERE product = ‘bagel’

50 (= 20+30)