Lectures 5: Introduction to SQL 4

Slides:



Advertisements
Similar presentations
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Advertisements

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:
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.
1 Lecture 03: SQL Friday, January 7, Administrivia Have you logged in IISQLSRV yet ? HAVE YOU CHANGED YOUR PASSWORD ? Homework 1 is now posted.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
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.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
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’
Union, Intersection, Difference (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The.
Complex Queries (1) Product ( pname, price, category, maker)
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
CSE544: SQL Monday 3/27 and Wednesday 3/29, 2006.
Integrity Constraints An important functionality of a DBMS is to enable the specification of integrity constraints and to enforce them. Knowledge of integrity.
+ 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 Lecture 4: More SQL Monday, January 13th, 2003.
SQL April 22 th, Agenda Union, intersections Sub-queries Modifying the database Views Modifying views Reusing views.
Lecture 3: Relational Algebra and SQL Tuesday, March 25, 2008.
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:
IM433-Industrial Data Systems Management Lecture 5: SQL.
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.
1 Lecture 25 Friday, November 30, Outline Query execution –Two pass algorithms based on indexes (6.7) Query optimization –From SQL to logical.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 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 Lecture 03: SQL Monday, January 9, Project t/Default.aspxhttp://iisqlsrv.cs.washington.edu/444/Projec.
Lectures 2&3: Introduction to SQL
Cours 7: Advanced SQL.
Lecture 04: SQL Monday, January 10, 2005.
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Database Systems Subqueries, Aggregation
06a: SQL-1 The Basics– Select-From-Where
Cse 344 April 4th – Subqueries.
The Best Of Collection (Master Tracks), Vol. 1
Monday 3/27 and Wednesday 3/29, 2006
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Introduction to Database Systems CSE 444 Lecture 03: SQL
January 19th – Subqueries 2 and relational algebra
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 7: Introduction to SQL 6
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Where are we? Until now: Modeling databases (ODL, E/R): all about the schema Now: Manipulating the data: queries, updates, SQL Then: looking inside -
Lecture 4: SQL Thursday, January 11, 2001.
Lectures 6: Introduction to SQL 5
Integrity Constraints
Lecture 3 Monday, April 8, 2002.
Lecture 24: Query Execution
Lecture 25: Query Optimization
CS639: Data Management for Data Science
Lecture 03: SQL Friday, October 3, 2003.
Lecture 3: Relational Algebra and SQL
Relational Schema Design (end) Relational Algebra SQL (maybe)
Lectures 2: Introduction to SQL 1
Lecture 23: Monday, November 25, 2002.
Lecture 04: SQL Monday, October 6, 2003.
Presentation transcript:

Lectures 5: Introduction to SQL 4 Lecture and activity contents are based on what Prof Chris Ré used in his CS 145 in the fall 2016 term with permission.

Nested queries: Sub-queries Returning Relations Lecture 3 > Section 1 > Nested Queries Nested queries: Sub-queries Returning Relations Another example: Company(name, city) Product(name, maker) Purchase(id, product, buyer) SELECT c.city FROM Company c WHERE c.name IN ( SELECT pr.maker FROM Purchase p, Product pr WHERE p.product = pr.name AND p.buyer = ‘Joe Blow‘) “Cities where one can find companies that manufacture products bought by Joe Blow”

Nested Queries Beware of duplicates! Is this query equivalent? Lecture 3 > Section 1 > Nested Queries Nested Queries Is this query equivalent? SELECT c.city FROM Company c, Product pr, Purchase p WHERE c.name = pr.maker AND pr.name = p.product AND p.buyer = ‘Joe Blow’ Beware of duplicates!

Now they are equivalent Lecture 3 > Section 1 > Nested Queries Nested Queries SELECT DISTINCT c.city FROM Company c, Product pr, Purchase p WHERE c.name = pr.maker AND pr.name = p.product AND p.buyer = ‘Joe Blow’ SELECT DISTINCT c.city FROM Company c WHERE c.name IN ( SELECT pr.maker FROM Purchase p, Product pr WHERE p.product = pr.name AND p.buyer = ‘Joe Blow‘) Now they are equivalent

Subqueries Returning Relations Lecture 3 > Section 1 > Nested Queries Subqueries Returning Relations ANY and ALL not supported by SQLite. You can also use operations of the form: s > ALL R s < ANY R EXISTS R Ex: Product(name, price, category, maker) SELECT name FROM Product WHERE price > ALL( SELECT price WHERE maker = ‘Gizmo-Works’) Find products that are more expensive than all those produced by “Gizmo-Works”

Subqueries Returning Relations Lecture 3 > Section 1 > Nested Queries Subqueries Returning Relations You can also use operations of the form: s > ALL R s < ANY R EXISTS R Ex: Product(name, price, category, maker) Find ‘copycat’ products, i.e. products made by competitors with the same names as products made by “Gizmo-Works” SELECT p1.name FROM Product p1 WHERE p1.maker = ‘Gizmo-Works’ AND EXISTS( SELECT p2.name FROM Product p2 WHERE p2.maker <> ‘Gizmo-Works’ AND p1.name = p2.name) <> means !=

Nested queries as alternatives to INTERSECT and EXCEPT Lecture 3 > Section 1 > Nested Queries Nested queries as alternatives to INTERSECT and EXCEPT INTERSECT and EXCEPT not in some DBMSs! (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) If R, S have no duplicates, then can write without sub-queries (HOW?) (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)

Correlated Queries Movie(title, year, director, length) Lecture 3 > Section 1 > Nested Queries Correlated Queries Movie(title, year, director, length) Find movies whose title appears more than once. SELECT DISTINCT title FROM Movie AS m WHERE year <> ANY( SELECT year FROM Movie WHERE title = m.title) Note the scoping of the variables! Note also: this can still be expressed as single SFW query…

Complex Correlated Query Lecture 3 > Section 1 > Nested Queries Complex Correlated Query Product(name, price, category, maker, year) SELECT DISTINCT x.name, x.maker FROM Product AS x WHERE x.price > ALL( SELECT y.price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972) Find products (and their manufacturers) that are more expensive than all products made by the same manufacturer before 1972 Can be very powerful (also much harder to optimize)

Lecture 3 > Section 1 > Summary Basic SQL Summary SQL provides a high-level declarative language for manipulating data (DML) The workhorse is the SFW block Set operators are powerful but have some subtleties Powerful, nested queries also allowed.