1 / 23

IM433-Industrial Data Systems Management

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 ). Simple SQL Query. Product. SELECT * FROM Product WHERE category= ‘ Gadgets ’.

neci
Download Presentation

IM433-Industrial Data Systems Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Relational Schema Another way of writing out a relational schema Underline the primary key Product(PName, Price, Category, Manfacturer)

  3. Simple SQL Query Product SELECT *FROM ProductWHERE category=‘Gadgets’ “selection”

  4. Simple SQL Query Product SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100 “selection” and “projection”

  5. The LIKE operator SELECT *FROM ProductsWHERE PName LIKE‘%gizmo%’ • s LIKE p: pattern matching on strings • p may contain two special symbols: • % = any sequence of characters • _ = any single character

  6. Eliminating Duplicates SELECTDISTINCT category FROM Product Compare to: SELECT category FROM Product

  7. Ordering the Results SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ordering is ascending, unless you specify the DESC keyword.

  8. SELECTDISTINCT category FROM Product ORDER BY category ? ? SELECT Category FROM Product ORDER BY PName ? SELECTDISTINCT category FROM Product ORDER BY PName

  9. Company and Product Company Key Product Foreignkey

  10. Joinbetween Productand Company Joins Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all products under $200 manufactured in Japan;return their names and prices. SELECTPName, PriceFROM Product, CompanyWHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200

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

  12. 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 cnameFROMWHERE

  13. 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

  14. A Subtlety about Joins Product Company SELECT CountryFROM Product, CompanyWHERE Manufacturer=CName AND Category=‘Gadgets’ What is the problem ? What’s thesolution ?

  15. Subqueries Returning Relations Company(cname, ccity)Product(pname, cname)Purchase(id, pname, buyer) Return cities where one can find companies that manufacture products bought by Joe Blow SELECTCompany.ccity FROM Company WHERECompany.cnameIN (SELECTProduct.cname FROM Purchase , Product WHEREProduct.pname=Purchase.pname AND Purchase .buyer = ‘Joe Blow’);

  16. Subqueries Returning Relations Is it equivalent to this ? SELECTCompany.ccity FROM Company, Product, Purchase WHERECompany.cname= Product.cname AND Product.pname = Purchase.pname AND Purchase.buyer = ‘Joe Blow’ Beware of duplicates !

  17. Removing Duplicates SELECTDISTINCT Company.city FROM Company WHERE Company.name IN (SELECT Product.maker FROM Purchase , Product WHERE Product.pname=Purchase.product AND Purchase .buyer = ‘Joe Blow‘); SELECTDISTINCT Company.city FROM Company, Product, Purchase WHERE Company.name= Product.maker AND Product.pname = Purchase.product AND Purchase.buyer = ‘Joe Blow’ Now they are equivalent

  18. Subqueries Returning Relations You can also use: s > ALL R s > ANY R EXISTS R Product ( pname, price, category, cname) Find products that are more expensive than all those produced By “Gizmo-Works” SELECTpname FROM Product WHERE price > ALL (SELECT price FROM Purchase WHEREcname=‘Gizmo-Works’)

  19. 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, maker FROM Product AS x WHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972);

  20. Aggregation SELECT avg(price) FROM Product WHERE maker=“Toyota” SELECT count(*) FROM Product WHERE year > 1995 SQL supports several aggregation operations: sum, count, min, max, avg Except count, all aggregations apply to a single attribute

  21. Aggregation: Count COUNT applies to duplicates, unless otherwise stated: same as Count(*) SELECT Count(category) FROM Product WHERE year > 1995 We probably want: SELECT Count(DISTINCT category) FROM Product WHERE year > 1995

  22. More Examples Purchase(product, date, price, quantity) SELECT Sum(price * quantity) FROM Purchase What do they mean ? SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’

  23. Simple Aggregations Purchase SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ 50 (= 20+30)

More Related