Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3: Introduction to SQL September 29, 2014.

Similar presentations


Presentation on theme: "Lecture 3: Introduction to SQL September 29, 2014."— Presentation transcript:

1 Lecture 3: Introduction to SQL September 29, 2014

2 Announcements There are good videos. – Watch the SQL video. There is an activity next class. SQL Challenge is Due 10/6

3 SQL Motivation Dark times 5 years ago. – Are databases dead? Everyone sells SQL – Pig, Hive, Impala “Not-Yet-SQL?”

4 4 Basic SQL

5 5 SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), …. Vendors support various subsets A very high-level programming language. This works because it is optimized well! NB: Probably the world’s most successful parallel programming language (multicore?)

6 6 SQL Data Definition Language (DDL) – Create/alter/delete tables and their attributes Data Manipulation Language (DML) – Insert/delete/modify tuples in tables (last time) – Query one or more tables – discussed next !

7 7 Tables in SQL PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Attribute names Table name Tuples or rows

8 8 Tables Explained The schema of a table is the table name and its attributes: Product(PName, Price, Category, Manfacturer) A key is an attribute whose values are unique; we underline a key Product(PName, Price, Category, Manfacturer)

9 9 Data Types in SQL Atomic types: – Characters: CHAR(20), VARCHAR(50) – Numbers: INT, BIGINT, SMALLINT, FLOAT – Others: MONEY, DATETIME, … Every attribute must have an atomic type – Hence tables are flat – Why ?

10 10 Tables Explained A tuple = a record – Restriction: all attributes are of atomic type A table = a (multi)-set of tuples – Like a list… – …but it is unordered: no first(), no next(), no last().

11 11 Outline Data in SQL (Review) Simple Queries in SQL Queries with more than one relation

12 12 SQL Query Basic form: (there are many many more bells and whistles) SELECT FROM WHERE SELECT FROM WHERE Call this: An SFW query.

13 13 Simple SQL Query PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT * FROM Product WHERE category=‘Gadgets ’ Product PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks “selection”

14 14 Simple SQL Query PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT Pname, Price,Manufacturer FROM Product WHERE category=‘Gadgets ’ Product PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks “selection” “projection”

15 15 Notation Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer) Input Schema Output Schema SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100

16 16 Details Case insensitive: – Same: SELECT Select select – Same: Product product – Different: ‘Seattle’ ‘seattle’ Constants: – ‘abc’ - yes – “abc” - no

17 17 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 Products WHERE PName LIKE ‘%gizmo%’

18 18 Eliminating Duplicates SELECT DISTINCT category FROM Product SELECT DISTINCT category FROM Product Compare to: SELECT category FROM Product SELECT category FROM Product Category Gadgets Photography Household Category Gadgets Photography Household

19 19 Ordering the Results SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ties are broken by the second attribute on the ORDER BY list, etc. Ordering is ascending, unless you specify the DESC keyword.

20 20 SELECT Category FROM Product ORDER BY PName SELECT Category FROM Product ORDER BY PName PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi ? SELECT DISTINCT category FROM Product ORDER BY category SELECT DISTINCT category FROM Product ORDER BY category SELECT DISTINCT category FROM Product ORDER BY PName SELECT DISTINCT category FROM Product ORDER BY PName ? ?

21 21 Keys and Foreign Keys PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CNameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan Key Foreign key

22 22 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, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 Join between Product and Company

23 23 Joins PNamePriceCategoryManuf Gizmo$19GadgetsGWorks Powergizmo$29GadgetsGWorks SingleTouch$149PhotographyCanon MultiTouch$203HouseholdHitachi Product Company CnameStockCountry GWorks25USA Canon65Japan Hitachi15Japan PNamePrice SingleTouch$149.99 SELECT PName, Price FROM Product, Company WHERE Manuf=CName AND Country=‘Japan’ AND Price <= 200 SELECT PName, Price FROM Product, Company WHERE Manuf=CName AND Country=‘Japan’ AND Price <= 200

24 24 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

25 25 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 Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’

26 26 A subtlety about Joins PNamePriceCategoryManuf Gizmo$19GadgetsGWorks Powergizmo$29GadgetsGWorks SingleTouch$149PhotographyCanon MultiTouch$203HouseholdHitachi Product Company CnameStockCountry GWorks25USA Canon65Japan Hitachi15Japan Country ? ? SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’ SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’ What is the problem ? What’s the solution ?

27 27 Tuple Variables SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor = cname Which address ? Person(pname, address, worksfor) Company(cname, address) SELECT DISTINCT Person.pname, Company.address FROM Person, Company WHERE Person.worksfor = Company.cname SELECT DISTINCT x.pname, y.address FROM Person AS x, Company AS y WHERE x.worksfor = y.cname

28 28 Meaning (Semantics) of SQL Queries SELECT a 1, a 2, …, a k FROM R 1 AS x 1, R 2 AS x 2, …, R n AS x n WHERE Conditions SELECT a 1, a 2, …, a k FROM R 1 AS x 1, R 2 AS x 2, …, R n AS x n WHERE Conditions Answer = {} for x 1 in R 1 do for x 2 in R 2 do ….. for x n in R n do if Conditions then Answer = Answer  {(a 1,…,a k )} return Answer Answer = {} for x 1 in R 1 do for x 2 in R 2 do ….. for x n in R n do if Conditions then Answer = Answer  {(a 1,…,a k )} return Answer Almost never the fastest way to do it! WARNING: This is a multiset union

29 An example of the semantics 29 SELECT R.A FROM R, S WHERE R.A = S.B SELECT R.A FROM R, S WHERE R.A = S.B A 1 3 BC 23 34 35 ABC 123 134 135 323 334 335 Cross Product Apply selections ABC 334 335 A 3 3 Where would DISTINCT be applied?

30 30 SELECT DISTINCT R.A FROM R, S, T WHERE R.A=S.A OR R.A=T.A SELECT DISTINCT R.A FROM R, S, T WHERE R.A=S.A OR R.A=T.A An Unintuitive Query Computes R  (S  T)But what if S =  ? What does it compute ?

31 More SQL

32 Outline Reprise: ORDER BY & Semantics Set Operators and Nested Queries Fancy SQL – Aggregation – NULL and Outer Joins – Host Languages & Cursors 32

33 Ordering 33 SELET Name FROM Product ORDER BY Price SELET Name FROM Product ORDER BY Price SQL-89 says “This makes no sense!” Intuitively, clear what it means: “Give me the products in descending order…” … still a little weird, products listed multiple times… Formally, the ordering should only be applied on the values returned (Remember the big for loop?) Some RDBMSs allow you to do this.

34 Ordering again… 34 SELET DISTINCT Name FROM Product ORDER BY Price SELET DISTINCT Name FROM Product ORDER BY Price SQL-89 said “This definitely makes no sense!” Intuitively, clear what it means: ??? Some products allow you to do this. How?

35 Set Operators & Nested Queries 35

36 SELECT R.A FROM R, S WHERE R.A=S.A UNION ALL SELECT R.A FROM R, T WHERE R.A=T.A SELECT R.A FROM R, S WHERE R.A=S.A UNION ALL SELECT R.A FROM R, T WHERE R.A=T.A Abstract Union 36 SELECT DISTINCT R.A FROM R, S, T WHERE R.A=S.A OR R.A=T.A SELECT DISTINCT R.A FROM R, S, T WHERE R.A=S.A OR R.A=T.A SELECT R.A FROM R, S WHERE R.A=S.A UNION SELECT R.A FROM R, T WHERE R.A=T.A SELECT R.A FROM R, S WHERE R.A=S.A UNION SELECT R.A FROM R, T WHERE R.A=T.A Why aren’t there duplicates? What if we want duplicates?

37 Union 37 Product(pname, maker, factory_loc) Company(name, hq_city) Write “Company names who make gizmos in US or China”

38 Intersect 38 “The headquarter location of companies who make gizmos in US AND China” The SQL key word is now intersect…

39 Intersect: subtle problem… 39 Product(pname, maker, factory_loc)Company(name, hq_city) SELECT hq_city FROM Company, Product WHERE maker = name and factor_loc = ‘US’ INTERSECT SELECT hq_city FROM Company, Product WHERE maker = name and factor_loc = ‘China’ SELECT hq_city FROM Company, Product WHERE maker = name and factor_loc = ‘US’ INTERSECT SELECT hq_city FROM Company, Product WHERE maker = name and factor_loc = ‘China’ What if two companies HQ in US: BUT one has loc in China (but not US) and vice versa? What’s wrong? “Company names who make gizmos in US AND China”

40 Other Set operators 40 EXCEPT (set difference) Also ALL variants INTERSECT “min # of duplicates” EXCEPT ALL “multiset subtract”

41 41 Subqueries Returning Relations SELECT 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 Company.city FROM Company WHERE Company.name IN (SELECT Product.maker FROM Purchase, Product WHERE Product.pname=Purchase.product AND Purchase.buyer = ‘Joe Blow‘); Return cities where one can find companies that manufacture products bought by Joe Blow Company(name, city) Product(pname, maker) Purchase(id, product, buyer)

42 42 Subqueries Returning Relations SELECT Company.city FROM Company, Product, Purchase WHERE Company.name= Product.maker AND Product.pname = Purchase.product AND Purchase.buyer = ‘Joe Blow’ SELECT Company.city FROM Company, Product, Purchase WHERE Company.name= Product.maker AND Product.pname = Purchase.product AND Purchase.buyer = ‘Joe Blow’ Is it equivalent to this ? Beware of duplicates!

43 43 Removing Duplicates Now they 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 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’ SELECT DISTINCT Company.city FROM Company, Product, Purchase WHERE Company.name= Product.maker AND Product.pname = Purchase.product AND Purchase.buyer = ‘Joe Blow’

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

45 45 Question for Database Fans and their Friends Can we express this query as a single SELECT- FROM-WHERE query, without subqueries ? Hint: show that all SFW queries are monotone (figure out what this means). A query with ALL is not monotone

46 46 Correlated Queries SELECT DISTINCT title FROM Movie AS x WHERE year <> ANY (SELECT year FROM Movie WHERE title = x.title); SELECT DISTINCT title FROM Movie AS x WHERE year <> ANY (SELECT year FROM Movie WHERE title = x.title); Movie (title, year, director, length) Find movies whose title appears more than once. Note (1) scope of variables (2) this can still be expressed as single SFW correlation

47 47 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); 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);

48 Exercises If you have difficultly with this material here is the way to fix it: – postgreSQL (free on the web/apt-get). Others on the website. – Play with examples on EdX. – Run and play with these examples 48

49 Basic SQL Summary SQL provides a high-level declarative language for manipulating data (DML) The workhorse is the SFW block Powerful, nested queries also allowed. 49

50 Fancy SQL: Aggregation 50

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

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

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

54 54 Simple Aggregations Purchase ProductDatePriceQuantity Bagel10/21120 Banana10/30.510 Banana10/10110 Bagel10/251.5020 SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ SELECT Sum(price * quantity) FROM Purchase WHERE product = ‘bagel’ 50 (= 20+30)

55 55 Grouping and Aggregation Purchase(product, date, price, quantity) SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product Let’s see what this means… Find total sales after 10/1/2005 per product.

56 56 Grouping and Aggregation 1. Compute the FROM and WHERE clauses. 2. Group by the attributes in the GROUPBY 3. Compute the SELECT clause: grouped attributes and aggregates.

57 57 1&2. FROM-WHERE-GROUPBY ProductDatePriceQuantity Bagel10/21120 Bagel10/251.5020 Banana10/30.510 Banana10/10110

58 58 3. SELECT SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product ProductDatePriceQuantity Bagel10/21120 Bagel10/251.5020 Banana10/30.510 Banana10/10110 ProductTotalSales Bagel50 Banana15

59 59 GROUP BY v.s. Nested Quereis SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > ‘10/1/2005’) AS TotalSales FROM Purchase x WHERE x.date > ‘10/1/2005’ SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity) FROM Purchase y WHERE x.product = y.product AND y.date > ‘10/1/2005’) AS TotalSales FROM Purchase x WHERE x.date > ‘10/1/2005’

60 60 Another Example SELECT product, sum(price * quantity) AS SumSales max(quantity) AS MaxQuantity FROM Purchase GROUP BY product SELECT product, sum(price * quantity) AS SumSales max(quantity) AS MaxQuantity FROM Purchase GROUP BY product What does it mean ?

61 61 HAVING Clause SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 SELECT product, Sum(price * quantity) FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product HAVING Sum(quantity) > 30 Same query, except that we consider only products that had at least 100 buyers. HAVING clause contains conditions on aggregates.

62 62 General form of Grouping and Aggregation SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2 S = may contain attributes a 1,…,a k and/or any aggregates but NO OTHER ATTRIBUTES C1 = is any condition on the attributes in R 1,…,R n C2 = is any condition on aggregate expressions Why ?

63 63 General form of Grouping and Aggregation Evaluation steps: 1.Evaluate FROM-WHERE, apply condition C1 2.Group by the attributes a 1,…,a k 3.Apply condition C2 to each group (may have aggregates) 4.Compute aggregates in S and return the result SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2 SELECT S FROM R 1,…,R n WHERE C1 GROUP BY a 1,…,a k HAVING C2

64 64 Advanced SQLizing 1.Getting around INTERSECT and EXCEPT 2.Quantifiers 3.Aggregation v.s. subqueries

65 65 1. INTERSECT and EXCEPT: (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) EXCEPT (SELECT S.A, S.B FROM S) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) SELECT R.A, R.B FROM R WHERE NOT EXISTS(SELECT * FROM S WHERE R.A=S.A and R.B=S.B) If R, S have no duplicates, then can write without subqueries (HOW ?) INTERSECT and EXCEPT: not in SQL Server

66 66 2. Quantifiers Product ( pname, price, company) Company( cname, city) Find all companies that make some products with price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.company and Product.price < 100 SELECT DISTINCT Company.cname FROM Company, Product WHERE Company.cname = Product.company and Product.price < 100 Existential: easy !

67 67 2. Quantifiers Product ( pname, price, company) Company( cname, city) Find all companies s.t. all of their products have price < 100 Universal: hard !  Find all companies that make only products with price < 100 same as:

68 68 2. Quantifiers 2. Find all companies s.t. all their products have price < 100 1. Find the other companies: i.e. s.t. some product  100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN (SELECT Product.company FROM Product WHERE Produc.price >= 100 SELECT DISTINCT Company.cname FROM Company WHERE Company.cname NOT IN (SELECT Product.company FROM Product WHERE Produc.price >= 100

69 69 3. Group-by v.s. Nested Query Find authors who wrote  10 documents: Attempt 1: with nested queries SELECT DISTINCT Author.name FROM Author WHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10 SELECT DISTINCT Author.name FROM Author WHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10 This is SQL by a novice Author(login,name) Wrote(login,url)

70 70 3. Group-by v.s. Nested Query Find all authors who wrote at least 10 documents: Attempt 2: SQL style (with GROUP BY) SELECT Author.name FROM Author, Wrote WHERE Author.login=Wrote.login GROUP BY Author.name HAVING count(wrote.url) > 10 SELECT Author.name FROM Author, Wrote WHERE Author.login=Wrote.login GROUP BY Author.name HAVING count(wrote.url) > 10 This is SQL by an expert No need for DISTINCT: automatically from GROUP BY

71 71 3. Group-by v.s. Nested Query Find authors with vocabulary  10000 words: SELECT Author.name FROM Author, Wrote, Mentions WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url GROUP BY Author.name HAVING count(distinct Mentions.word) > 10000 SELECT Author.name FROM Author, Wrote, Mentions WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url GROUP BY Author.name HAVING count(distinct Mentions.word) > 10000 Author(login,name) Wrote(login,url) Mentions(url,word)

72 Fancy SQL: NULL and Outer joins 72

73 73 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)

74 74 SELECT Store.name FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.name HAVING 100 < min(Product.price) SELECT Store.name FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.name HAVING 100 < min(Product.price) SELECT Store.name FROM Store WHERE Store.sid NOT IN (SELECT Product.sid FROM Product WHERE Product.price <= 100) SELECT Store.name FROM Store WHERE Store.sid NOT IN (SELECT Product.sid FROM Product WHERE Product.price <= 100) SELECT Store.name FROM Store WHERE 100 < ALL (SELECT Product.price FROM product WHERE Store.sid = Product.sid) SELECT Store.name FROM Store WHERE 100 < ALL (SELECT Product.price FROM product WHERE Store.sid = Product.sid) Almost equivalent… Why both ?

75 75 Two Examples Store(sid, sname) Product(pid, pname, price, sid) For each store, find its most expensive product

76 76 Two Examples SELECT Store.sname, max(Product.price) FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.sname SELECT Store.sname, max(Product.price) FROM Store, Product WHERE Store.sid = Product.sid GROUP BY Store.sid, Store.sname SELECT Store.sname, x.pname FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) SELECT Store.sname, x.pname FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) This is easy but doesn’t do what we want: Better: But may return multiple product names per store

77 77 Two Examples SELECT Store.sname, max(x.pname) FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) GROUP BY Store.sname SELECT Store.sname, max(x.pname) FROM Store, Product x WHERE Store.sid = x.sid and x.price >= ALL (SELECT y.price FROM Product y WHERE Store.sid = y.sid) GROUP BY Store.sname Finally, choose some pid arbitrarily, if there are many with highest price:

78 78 NULLS in SQL Whenever we don’t have a value, we can put a NULL Can mean many things: – Value does not exists – Value exists but is unknown – Value not applicable – Etc. The schema specifies for each attribute if can be null (nullable attribute) or not How does SQL cope with tables that have NULLs ?

79 79 Null Values If x= NULL then 4*(3-x)/7 is still NULL If x= NULL then x=“Joe” is UNKNOWN In SQL there are three boolean values: FALSE = 0 UNKNOWN = 0.5 TRUE = 1

80 80 Null Values C1 AND C2 = min(C1, C2) C1 OR C2 = max(C1, C2) NOT C1 = 1 – C1 Rule in SQL: include only tuples that yield TRUE SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) E.g. age=20 heigth=NULL weight=200

81 81 Null Values Unexpected behavior: Some Persons are not included ! SELECT * FROM Person WHERE age = 25 SELECT * FROM Person WHERE age = 25

82 82 Null Values Can test for NULL explicitly: – x IS NULL – x IS NOT NULL Now it includes all Persons SELECT * FROM Person WHERE age = 25 OR age IS NULL SELECT * FROM Person WHERE age = 25 OR age IS NULL

83 83 Outerjoins Explicit joins in SQL = “inner joins”: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product, Purchase WHERE Product.name = Purchase.prodName Same as: But Products that never sold will be lost !

84 84 Outerjoins Left outer joins in SQL: Product(name, category) Purchase(prodName, store) SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName SELECT Product.name, Purchase.store FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName

85 85 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz NameStore GizmoWiz CameraRitz CameraWiz OneClickNULL ProductPurchase

86 86 Application Compute, for each product, the total number of sales in ‘September’ Product(name, category) Purchase(prodName, month, store) SELECT Product.name, count(*) FROM Product, Purchase WHERE Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name SELECT Product.name, count(*) FROM Product, Purchase WHERE Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name What’s wrong ?

87 87 Application Compute, for each product, the total number of sales in ‘September’ Product(name, category) Purchase(prodName, month, store) SELECT Product.name, count(*) FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name SELECT Product.name, count(*) FROM Product LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName and Purchase.month = ‘September’ GROUP BY Product.name Now we also get the products who sold in 0 quantity

88 88 Outer Joins Left outer join: – Include the left tuple even if there’s no match Right outer join: – Include the right tuple even if there’s no match Full outer join: – Include the both left and right tuples even if there’s no match

89 Fancy SQL: Host Languages 89

90 SQL does data SQL doesn’t have a GUI library SQL can’t drive your soundcard SQL can’t serve web pages SQL can’t even poll for keyboard input 90 We need to embed SQL inside another language (called the host language)

91 Host Language Embedding C/Java have types and SQL has types… Java uses loops and SQL is set-oriented… 91 Ok, but SQL has CAST This is a problem! Called impedance mismatch.

92 Cursors Cursors ~ iterators – We can fetch, move, open, close them. Read-only or updatable Optimized to retrieve the next row – But, can make SCROLLABLE 92 Please see the book (6.1)

93 Dynamic SQL How do we accept user generated values? – E.g., “I want cameras less than 10 dollars” 93 char szQuery = “SELECT * FROM PROD \ WHERE PRICE=“ + szPrice + …. EXEC SQL PREPARE thequery FROM :szQuery; EXEC SQL EXECUTE thequery; char szQuery = “SELECT * FROM PROD \ WHERE PRICE=“ + szPrice + …. EXEC SQL PREPARE thequery FROM :szQuery; EXEC SQL EXECUTE thequery; Build query string in response to user input

94 Host Language Embedding 94 This is called SQL Injection http://xkcd.com/327/

95 Summary SQL is a rich programming language – There is much more! SQL handles the way data is processed declaratively SQL can’t do it all alone, so is embedded 95


Download ppt "Lecture 3: Introduction to SQL September 29, 2014."

Similar presentations


Ads by Google