IM433-Industrial Data Systems Management Lecture 5: SQL.

Slides:



Advertisements
Similar presentations
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
Advertisements

SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
1 Lecture 2: SQL Wednesday, January 7, Agenda Leftovers from Monday The relational model (very quick) SQL Homework #1 given out later this week.
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
1 Information Systems Chapter 6 Database Queries.
Complex Queries (1) Product ( pname, price, category, maker)
CSE544: SQL Monday 3/27 and Wednesday 3/29, 2006.
+ From Relational Algebra to SQL W2013 CSCI 2141.
Lecture 3: Introduction to SQL September 29, 2014.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
1 SQL cont.. 2 Outline Unions, intersections, differences (6.2.5, 6.4.2) Subqueries (6.3) Aggregations (6.4.3 – 6.4.6) Hint for reading the textbook:
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
SQL SQL Review. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Introduction to Database Systems CSEP544 Lecture #1 January 5, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
1 Introduction to Database Systems CSEP 544 Lecture #1 March 29, 2004 Alon Halevy.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.
1 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
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),
SQL.
Lectures 2&3: Introduction to SQL
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Database Systems Subqueries, Aggregation
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Monday 3/27 and Wednesday 3/29, 2006
Class 2 Relational Data Languages
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Cse 344 January 12th –joins.
Introduction to Database Systems CSE 444 Lecture 03: SQL
Lecture 4: Advanced SQL – Part II
Introduction to SQL Wenhao Zhang October 5, 2018.
Introduction to Database Systems CSE 444 Lecture 03: SQL
CSE544 SQL Wednesday, March 31, 2004.
SQL Introduction Standard language for querying and manipulating data
Lecture 12: SQL Friday, October 20, 2000.
Lectures 3: Introduction to SQL Part II
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lectures 5: Introduction to SQL 4
Lecture 4: SQL Thursday, January 11, 2001.
Lectures 6: Introduction to SQL 5
Lecture 3 Monday, April 8, 2002.
Class 2 Relational Data Languages
CS639: Data Management for Data Science
Lecture 03: SQL Friday, October 3, 2003.
Lecture 3: Relational Algebra and SQL
Lecture 04: SQL Monday, October 6, 2003.
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

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

Simple SQL Query PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100 Product PNamePriceManufacturer SingleTouch$149.99Canon MultiTouch$203.99Hitachi “ selection ” and “ projection ”

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% ’

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

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 Ordering is ascending, unless you specify the DESC keyword.

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

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

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

Joins PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan PNamePrice SingleTouch$ SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country= ‘ Japan ’ AND Price <= 200

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

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 ’ Unexpected duplicates

A Subtlety about Joins NamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan Country ?? What is the problem ? What ’ s the solution ? SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category= ‘ Gadgets ’

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

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 ’ 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 !

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 ’

Subqueries Returning Relations SELECT pname FROM Product WHERE price > ALL (SELECT price FROM Purchase WHERE cname= ‘ Gizmo-Works ’ ) 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 produced By “ Gizmo-Works ” You can also use: s > ALL R s > ANY R EXISTS R

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

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

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

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 ?

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