21
1 Information Systems Chapter 6 Database Queries

1 Information Systems Chapter 6 Database Queries

Embed Size (px)

Citation preview

Page 1: 1 Information Systems Chapter 6 Database Queries

1

Information Systems

Chapter 6 Database Queries

Page 2: 1 Information Systems Chapter 6 Database Queries

2

SQL Queries

Page 3: 1 Information Systems Chapter 6 Database Queries

3

SQL Query

Basic form:

SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)

SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)

Page 4: 1 Information Systems Chapter 6 Database Queries

4

Simple SQL Query

PName Price CategoryManufactu

rer

Gizmo $19.99 GadgetsGizmoWor

ks

Powergizmo

$29.99 GadgetsGizmoWor

ks

SingleTouch

$149.99Photograp

hyCanon

MultiTouch

$203.99 Household Hitachi

SELECT *FROM ProductWHERE category=‘Gadgets’

SELECT *FROM ProductWHERE category=‘Gadgets’

Product

PName Price CategoryManufactu

rer

Gizmo $19.99 GadgetsGizmoWor

ks

Powergizmo

$29.99 GadgetsGizmoWor

ks

“selection”

Page 5: 1 Information Systems Chapter 6 Database Queries

5

Simple SQL Query

PName Price CategoryManufactu

rer

Gizmo $19.99 GadgetsGizmoWor

ks

Powergizmo

$29.99 GadgetsGizmoWor

ks

SingleTouch

$149.99Photograp

hyCanon

MultiTouch

$203.99 Household Hitachi

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

Product

PName PriceManufactu

rer

SingleTouch

$149.99 Canon

MultiTouch

$203.99 Hitachi

“selection” and“projection”

Page 6: 1 Information Systems Chapter 6 Database Queries

6

A Notation for SQL Queries

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100

Product(PName, Price, Category, Manfacturer)

Answer(PName, Price, Manfacturer)

Input Schema

Output Schema

Page 7: 1 Information Systems Chapter 6 Database Queries

7

Selections

What goes in the WHERE clause:• x = y, x < y, x <= y, etc

– For number, they have the usual meanings– For CHAR and VARCHAR: lexicographic

ordering• Expected conversion between CHAR and

VARCHAR

– For dates and times, what you expect...

• Pattern matching on strings: s LIKE p (next)

Page 8: 1 Information Systems Chapter 6 Database Queries

8

The LIKE operator

• s LIKE p: pattern matching on strings

• p may contain two special symbols:– % = any sequence of characters– _ = any single character

Product(Name, Price, Category, Manufacturer)Find all products whose name mentions ‘gizmo’:

SELECT *FROM ProductsWHERE PName LIKE ‘%gizmo%’

SELECT *FROM ProductsWHERE PName LIKE ‘%gizmo%’

Page 9: 1 Information Systems Chapter 6 Database Queries

9

Eliminating Duplicates

Compare to:

SELECT DISTINCT categoryFROM Product

SELECT DISTINCT categoryFROM Product

Household

Photography

Gadgets

Category

SELECT categoryFROM Product

SELECT categoryFROM Product

Household

Photography

Gadgets

Gadgets

Category

Page 10: 1 Information Systems Chapter 6 Database Queries

10

Ordering the Results

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

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

Ordering is ascending, unless you specify the DESC keyword.

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

Page 11: 1 Information Systems Chapter 6 Database Queries

11

Ordering the Results

SELECT CategoryFROM ProductORDER BY PName

SELECT CategoryFROM ProductORDER BY PName

PName Price CategoryManufactu

rer

Gizmo $19.99 GadgetsGizmoWor

ks

Powergizmo

$29.99 GadgetsGizmoWor

ks

SingleTouch

$149.99Photograp

hyCanon

MultiTouch

$203.99 Household Hitachi

?

Page 12: 1 Information Systems Chapter 6 Database Queries

12

Ordering the Results

SELECT DISTINCT categoryFROM ProductORDER BY category

SELECT DISTINCT categoryFROM ProductORDER BY category

Compare to:

Category

Gadgets

Household

Photography

SELECT DISTINCT categoryFROM ProductORDER BY PName

SELECT DISTINCT categoryFROM ProductORDER BY PName ?

Page 13: 1 Information Systems Chapter 6 Database Queries

13

Joins in SQL

• Connect two or more tables:PName Price Category

Manufacturer

Gizmo $19.99 GadgetsGizmoWork

s

Powergizmo $29.99 GadgetsGizmoWork

s

SingleTouch $149.99Photograph

yCanon

MultiTouch $203.99 Household Hitachi

Product

Company CName StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

What isthe Connection

betweenthem ?

Page 14: 1 Information Systems Chapter 6 Database Queries

14

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

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

Joinbetween Product

and Company

Page 15: 1 Information Systems Chapter 6 Database Queries

15

Joins in SQL

PName Price CategoryManufactur

er

Gizmo $19.99 GadgetsGizmoWork

s

Powergizmo

$29.99 GadgetsGizmoWork

s

SingleTouch

$149.99Photograp

hyCanon

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

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

Page 16: 1 Information Systems Chapter 6 Database Queries

16

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’

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

Page 17: 1 Information Systems Chapter 6 Database Queries

17

Joins

Product (pname, price, category, manufacturer)Purchase (buyer, seller, store, product)Person(persname, phoneNumber, city)

Find names of people living in Seattle that bought some product in the ‘Gadgets’ category, and the names of the stores they bought such product from

SELECT DISTINCT persname, storeFROM Person, Purchase, ProductWHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’

SELECT DISTINCT persname, storeFROM Person, Purchase, ProductWHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’

Page 18: 1 Information Systems Chapter 6 Database Queries

18

Disambiguating Attributes

• Sometimes two relations have the same attr:Person(pname, address, worksfor)Company(cname, address)

SELECT DISTINCT pname, addressFROM Person, CompanyWHERE worksfor = cname

SELECT DISTINCT pname, addressFROM Person, CompanyWHERE worksfor = cname

SELECT DISTINCT Person.pname, Company.addressFROM Person, CompanyWHERE Person.worksfor = Company.cname

SELECT DISTINCT Person.pname, Company.addressFROM Person, CompanyWHERE Person.worksfor = Company.cname

Whichaddress ?

Page 19: 1 Information Systems Chapter 6 Database Queries

19

Tuple Variables

SELECT DISTINCT x.storeFROM Purchase AS x, Purchase AS yWHERE x.product = y.product AND y.store = ‘BestBuy’

SELECT DISTINCT x.storeFROM Purchase AS x, Purchase AS yWHERE x.product = y.product AND y.store = ‘BestBuy’

Find all stores that sold at least one product that the store‘BestBuy’ also sold:

Answer (store)

Product (pname, price, category, manufacturer)Purchase (buyer, seller, store, product)Person(persname, phoneNumber, city)

Page 20: 1 Information Systems Chapter 6 Database Queries

20

Tuple VariablesGeneral rule: tuple variables introduced automatically by the system: Product ( name, price, category, manufacturer)

Becomes:

Doesn’t work when Product occurs more than once:In that case the user needs to define variables explicitely.

SELECT name FROM Product WHERE price > 100

SELECT name FROM Product WHERE price > 100

SELECT Product.name FROM Product AS Product WHERE Product.price > 100

SELECT Product.name FROM Product AS Product WHERE Product.price > 100

Page 21: 1 Information Systems Chapter 6 Database Queries

21

Renaming Columns

PName Price CategoryManufactu

rer

Gizmo $19.99 GadgetsGizmoWor

ks

Powergizmo

$29.99 GadgetsGizmoWor

ks

SingleTouch

$149.99Photograp

hyCanon

MultiTouch

$203.99 Household Hitachi

SELECT Pname AS prodName, Price AS askPriceFROM ProductWHERE Price > 100

SELECT Pname AS prodName, Price AS askPriceFROM ProductWHERE Price > 100

Product

prodName askPrice

SingleTouch

$149.99

MultiTouch

$203.99

Query withrenaming