Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

Similar documents
Introduction to Database Systems CSE 444

Introduction to Database Systems CSE 414

Lecture 2: Introduction to SQL

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Systems CSE 414

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Lectures 2&3: Introduction to SQL

Announcements. Subqueries. Lecture Goals. 1. Subqueries in SELECT. Database Systems CSE 414. HW1 is due today 11pm. WQ1 is due tomorrow 11pm

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

Lecture 3: SQL Part II

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

Subqueries. 1. Subqueries in SELECT. 1. Subqueries in SELECT. Including Empty Groups. Simple Aggregations. Introduction to Database Systems CSE 414

CSE 544 Principles of Database Management Systems

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Database design and implementation CMPSCI 645. Lecture 04: SQL and Datalog

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL. Structured Query Language

CSC 261/461 Database Systems Lecture 5. Fall 2017

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

CSE 544 Principles of Database Management Systems

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

Introduction to Database Systems CSE 414. Lecture 8: SQL Wrap-up

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

CS 564 Midterm Review

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

Introduction to Data Management CSE 414

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

CMPT 354: Database System I. Lecture 3. SQL Basics

SQL. Assit.Prof Dr. Anantakul Intarapadung

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

Before we talk about Big Data. Lets talk about not-so-big data

Announcements. Multi-column Keys. Multi-column Keys (3) Multi-column Keys. Multi-column Keys (2) Introduction to Data Management CSE 414

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Announcements. Multi-column Keys. Multi-column Keys. Multi-column Keys (3) Multi-column Keys (2) Introduction to Data Management CSE 414

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Chapter 6 The database Language SQL as a tutorial

COMP 430 Intro. to Database Systems

Introduction to Databases CSE 414. Lecture 2: Data Models

Querying Data with Transact SQL

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

8. Relational Calculus (Part II)

CSE 344 JANUARY 8 TH SQLITE AND JOINS

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Lecture 03: SQL. Friday, April 2 nd, Dan Suciu Spring

Database Systems CSE 414

Introduction to Data Management CSE 344

Chapter 6 The database Language SQL as a tutorial

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Introduction to Data Management CSE 344

CompSci 516: Database Systems

Introduction to Database Systems CSE 414

CSE344 Midterm Exam Fall 2016

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Plan for today. Query Processing/Optimization. Parsing. A query s trip through the DBMS. Validation. Logical plan

CSC 261/461 Database Systems Lecture 13. Fall 2017

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Lecture 16. The Relational Model

MTAT Introduction to Databases

Introduction to Relational Databases

Lecture 5 Design Theory and Normalization

CSEN 501 CSEN501 - Databases I

CSE 344 MAY 7 TH EXAM REVIEW

EECS 647: Introduction to Database Systems

L12: ER modeling 5. CS3200 Database design (sp18 s2) 2/22/2018

The Relational Data Model

Data Informatics. Seon Ho Kim, Ph.D.

SQL: Data Manipulation Language

Introduction to Data Management CSE 344. Lecture 2: Data Models

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

CS 186, Fall 1999 Midterm 2 Professor Hawthorn

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Introduction to Data Management CSE 344. Lecture 12: Relational Calculus

Homework 1: RA, SQL and B+-Trees (due Feb 7, 2017, 9:30am, in class hard-copy please)

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

COMP 430 Intro. to Database Systems

Querying Data with Transact-SQL

CS 186, Fall 1999 Midterm 2 Professor Hawthorn

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Announcements. Outline UNIQUE. (Inner) joins. (Inner) Joins. Database Systems CSE 414. WQ1 is posted to gradebook double check scores

Database Systems CSE 414

Subqueries. Must use a tuple-variable to name tuples of the result

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Homework 1: RA, SQL and B+-Trees (due September 24 th, 2014, 2:30pm, in class hard-copy please)

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Query Processing & Optimization. CS 377: Database Systems

Keys, SQL, and Views CMPSCI 645

EECS 647: Introduction to Database Systems

A tuple is dangling if it doesn't join with any

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

20461: Querying Microsoft SQL Server 2014 Databases

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Transcription:

L04: SQL 124

Announcements! Polls on Piazza. Open for 2 days Outline today: - practicing more joins and specifying key and FK constraints - nested queries Next time: "witnesses" (traditionally students find this topic the most difficult) 125

Queries via SQL have multiple words: If you master this structure you know 50% about SQL Queries List of attributes to be included in final result (also called projection! ("*" selects all attributes) Indicates the table(s) from which data is to be retrieved SELECT FROM WHERE GROUP BY HAVING ORDER BY Note1 : SQL is generally case insensitive, e.g. SELECT = Select = select Note2 : The words always appear in this order you CANNOT reorder them Lists a comparison predicate, which restricts the rows returned by the query, e.g. price < 20 or different join conditions Groups rows that have one more common values together into a smaller set of rows A comparison predicate used to restrict the rows resulting from the GROUP BY clause Identifies which columns are used to sort the resulting data, plus the direction each column is sorted by (ascending or descending) 126

How to specify Foreign Key constraints Suppose we have the following schema: Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) And we want to impose the following constraint: - Only bona fide students may enroll in courses i.e. a student must appear in the Students table to enroll in a class Students sid name gpa 101 Bob 3.2 123 Mary 3.8 Enrolled student_id cid grade 123 564 A 123 537 A+ student_id alone is not a key- what is? We say that student_id is a foreign key that refers to Students 127

Declaring Primary Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) CREATE TABLE Students( sid CHAR(20) PRIMARY KEY, name CHAR(20), gpa REAL ) 128

Declaring Primary Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) CREATE TABLE Students( sid CHAR(20), name CHAR(20), gpa REAL, PRIMARY KEY (sid) ) 129

Declaring Foreign Keys Students(sid: string, name: string, gpa: float) Enrolled(student_id: string, cid: string, grade: string) CREATE TABLE Enrolled( student_id CHAR(20), cid CHAR(20), grade CHAR(10), PRIMARY KEY (student_id, cid), FOREIGN KEY (student_id) REFERENCES Students(sid) ) 130

An example of SQL semantics A 1 3 B C 2 3 3 4 3 5 Cross Product SELECT R.A FROM R, S WHERE R.A = S.B A B C 1 2 3 1 3 4 1 3 5 3 2 3 3 3 4 3 3 5 Output Apply Selections / Conditions A A 3 3 B C 3 3 4 3 3 5 Apply Projection 131

Note the semantics of a join 1. Take cross product: X = R S 2. Apply selections / conditions: Y = r, s X r. A = r. B} 3. Apply projections to get final output: Z = (y. A, ) for y Y SELECT R.A FROM R, S WHERE R.A = S.B Recall: Cross product (A X B) is the set of all unique tuples in A,B Ex: {a,b,c} X {1,2} = {(a,1), (a,2), (b,1), (b,2), (c,1), (c,2)} = Filtering! = Returning only some attributes Remembering this order is critical to understanding the output of certain queries (see later on ) 132

Note: we say semantics not execution order The preceding slides show what a join means Not actually how the DBMS executes it under the covers 133

Practicing more Joins 134

Quiz 4 302 Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all US companies that manufacture at least two different products. SELECT DISTINCT cname FROM WHERE 135

Quiz 4 302 Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all US companies that manufacture at least two different products. SELECT DISTINCT cname C FROM Product P1, Product P2, Company WHERE country = 'USA' and P1.manufacturer = cname P1 and P2.manufacturer = cname and P1.pName <> P2.pName <> P2 136

Quiz 4 P1 PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks............ P2 <> PName Price Category Manufacturer............ Powergizmo $29.99 Gadgets GizmoWorks Company CName StockPrice Country GizmoWorks 25 USA......... 302 SELECT DISTINCT cname FROM Product P1, Product P2, Company WHERE country = 'USA' and P1.manufacturer = cname and P2.manufacturer = cname and P1.pName <> P2.pName Cname GizmoWorks 137

Quiz 5 302 Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all US companies that manufacture a product below $20 and a product above $15. SELECT DISTINCT cname FROM Product as P1, Product as P2, Company WHERE country = 'USA' and P1.price < 20 and P2.price > 15 and P1.manufacturer = cname and P2.manufacturer = cname Product PName Price Category Manufacturer Gizmo 19.99 Gadgets GizmoWorks Powergizmo 29.99 Gadgets GizmoWorks SingleTouch Company CName GizmoWorks 25 StockPrice Country 149.99 Photography Canon MultiTouch 203.99 Household Hitachi USA Canon 65 Japan Hitachi 15 Japan 138

Quiz 5 302 Product (pname, price, category, manufacturer) Company (cname, stockprice, country) Q: Find all US companies that manufacture a product below $20 and a product above $15. C Note that we did not specify any condition that P1 and P2 need to be distinct. An alternative interpretation is "...and another product above..." P1 P.price > 15 P2 P.price < 20 139

Quiz 5 P1 PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks............ P2 PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks............ Company CName StockPrice Country GizmoWorks 25 USA......... 302 SELECT DISTINCT cname FROM Product as P1, Product as P2, Company WHERE country = 'USA' and P1.price < 20 and P2.price > 15 and P1.manufacturer = cname and P2.manufacturer = cname Cname GizmoWorks 140

Quiz 6 302 Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan Q: Find all countries that have companies that manufacture some product in the Gadgets category! SELECT country FROM Product, Company WHERE manufacturer = cname and category = 'Gadgets'? 141

Quiz 6 302 Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 Japan Q: Find all countries that have companies that manufacture some product in the Gadgets category! SELECT country FROM Product, Company WHERE manufacturer = cname and category = 'Gadgets' Country USA USA Joins can introduce duplicates -> remember to use DISTINCT! 142

Nested queries (Subqueries) 143

High-level note on nested queries We can do nested queries because SQL is compositional: - Everything (inputs / outputs) is represented as multisets- the output of one query can thus be used as the input to another (nesting)! This is extremely powerful! High-level idea: subqueries return relations (yet sometimes just values) 144

Subqueries = Nested queries Outer block Inner block SELECT... FROM... WHERE... (SELECT... FROM... WHERE... ) 145

Subqueries A subquery is a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquery may occur in a: - SELECT clause - FROM clause - WHERE clause - HAVING clause important! Rule of thumb: avoid writing nested queries when possible; keep in mind that sometimes it s impossible 146

1. Subqueries in SELECT 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Q: For each product return the city where it is manufactured! SELECT P.pname, ( SELECT C.city FROM Company2 C WHERE C.cid = P.cid) FROM Product2 P What happens if the subquery returns more than one city? Runtime error "Scalar subqueries" 147

1. Subqueries in SELECT 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Q: For each product return the city where it is manufactured! SELECT P.pname, ( SELECT C.city FROM Company2 C WHERE C.cid = P.cid) FROM Product2 P "unnesting the query" SELECT P.pname, C.city FROM Product2 P, Company2 C WHERE C.cid = P.cid Whenever possible, don't use nested queries 148

1. Subqueries in SELECT 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Q: Compute the number of products made by each company! SELECT C.cname, ( SELECTcount (*) FROM Product2 P WHERE P.cid = C.cid) FROM Company2 C Better: we can unnest by using a GROUP BY: SELECT C.cname, count(*) FROM Company2 C, Product2 P WHERE C.cid=P.cid GROUP BY C.cname 149

2. Subqueries in FROM clause 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Q: Find all products whose prices are > 20 and < 30! SELECT X.pname FROM ( SELECT * FROM Product2 as P WHERE price >20 ) as X WHERE X.price < 30 unnesting SELECT pname FROM Product2 WHERE price > 20 and price < 30 X PName Price cid Powergizmo $29.99 1 MultiTouch $203.99 3 150

Subqueries in WHERE clause IN, ANY, ALL 151

3. Subqueries in WHERE What do these queries compute? R a 1 2 U a 2 3 305 4 SELECT FROM WHERE SELECT FROM WHERE SELECT FROM WHERE a R a IN (SELECT * from U) a R a < ANY (SELECT * from U) a R a < ALL (SELECT * from U)??? 152

3. Subqueries in WHERE What do these queries compute? R a 1 2 U a 2 3 305 4 SELECT FROM WHERE a R a IN (SELECT * from U) a 2 Since 2 is in the set (2, 3, 4) SELECT FROM WHERE a R a < ANY (SELECT * from U) a 1 2 Since 1 and 2 are < than at least one ("any") of 2, 3 or 4 SELECT FROM WHERE a R a < ALL (SELECT * from U) a 1 Since 1 is < than each ("all") of 2, 3, and 4 153

Something tricky about Nested Queries SELECT c.city FROM Company c WHERE c.name IN ( SELECT pr.maker FROM Purchase p, Product pr WHERE p.name = pr.product AND p.buyer = Joe B ) Are these queries 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 B Beware of duplicates! 154

Something tricky about Nested Queries SELECT DISTINCT c.city FROM Company c WHERE c.name IN ( SELECT pr.maker FROM Purchase p, Product pr WHERE p.name = pr.product AND p.buyer = Joe B ) Are these queries equivalent? 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 B Now they are equivalent (both use set semantics) 155

Correlated subqueries In all previous cases, the nested subquery in the inner select block could be entirely evaluated before processing the outer select block. This is no longer the case for correlated nested queries. Whenever a condition in the WHERE clause of a nested query references some column of a table declared in the outer query, the two queries are said to be correlated. The nested query is then evaluated once for each tuple (or combination of tuples) in the outer query. 156

Correlated Queries (Using External Vars in Internal Subquery) Movie(title, year, director, length) SELECT DISTINCT title FROM Movie AS m WHERE year <> ANY( SELECT year FROM Movie WHERE title = m.title) Find movies whose title appears in more than one year. Note the scoping of the variables! Note also: this can still be expressed as single SFW query 157

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

3. Subqueries in WHERE (existential) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Existential quantifiers $ Q: Find all companies that make some products with price < 25! Using IN: SELECT DISTINCT C.cname FROM Company2 C WHERE C.cid IN ( 1, 2 ) cid CName City 1 GizmoWorks Oslo 2 Canon Osaka 3 Hitachi Kyoto PName Price cid Gizmo $19.99 1 Powergizmo $29.99 1 SingleTouch $14.99 2 MultiTouch $203.99 3 159

3. Subqueries in WHERE (existential) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Existential quantifiers $ Q: Find all companies that make some products with price < 25! Using IN: "Set membership" SELECT DISTINCT C.cname FROM Company2 C WHERE C.cid IN ( SELECT P.cid FROM Product2 P WHERE P.price < 25) cid CName City 1 GizmoWorks Oslo 2 Canon Osaka 3 Hitachi Kyoto PName Price cid Gizmo $19.99 1 Powergizmo $29.99 1 SingleTouch $14.99 2 MultiTouch $203.99 3 160

3. Subqueries in WHERE (existential) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Existential quantifiers $ Q: Find all companies that make some products with price < 25! Using EXISTS: "Test for empty relations" SELECT DISTINCT C.cname FROM Company2 C WHERE EXISTS ( SELECT * FROM Product2 P WHERE C.cid = P.cid and P.price < 25) cid CName City 1 GizmoWorks Oslo 2 Canon Osaka 3 Hitachi Kyoto PName Price cid Gizmo $19.99 1 Powergizmo $29.99 1 SingleTouch $14.99 2 MultiTouch $203.99 3 Correlated subquery 161

3. Subqueries in WHERE (existential) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Existential quantifiers $ Q: Find all companies that make some products with price < 25! Using ANY (also some): "Set comparison" SELECT DISTINCT C.cname FROM Company2 C WHERE 25 > ANY ( SELECT price FROM Product2 P WHERE P.cid = C.cid) cid CName City 1 GizmoWorks Oslo 2 Canon Osaka 3 Hitachi Kyoto PName Price cid Gizmo $19.99 1 Powergizmo $29.99 1 SingleTouch $14.99 2 MultiTouch $203.99 3 Correlated subquery SQLlite does not support "ANY" L 162

3. Subqueries in WHERE (existential) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Existential quantifiers $ Q: Find all companies that make some products with price < 25! Now, let's unnest: SELECT DISTINCT C.cname FROM Company2 C, Product2 P WHERE C.cid = P.cid and P.price < 25 cid CName City 1 GizmoWorks Oslo 2 Canon Osaka 3 Hitachi Kyoto PName Price cid Gizmo $19.99 1 Powergizmo $29.99 1 SingleTouch $14.99 2 MultiTouch $203.99 3 Existential quantifiers are easy! J 163

3. Subqueries in WHERE (universal) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Universal quantifiers " Q: Find all companies that make only products with price < 25! same as: Q: Find all companies for which all products have price < 25! Universal quantifiers are more complicated! L (Think about the companies that should not be returned) 164

3. Subqueries in WHERE (exist not -> universal) Q: Find all companies that make only products with price < 25! 1. Find the other companies: i.e. they have some product ³ 25! SELECT DISTINCT C.cname FROM Company2 C WHERE C.cid IN ( SELECT P.cid FROM Product2 P WHERE P.price >= 25) 315 2. Find all companies s.t. all their products have price < 25! SELECT DISTINCT C.cname FROM Company2 C WHERE C.cid NOT IN ( SELECT P.cid FROM Product2 P WHERE P.price >= 25) 165

3. Subqueries in WHERE (exist not -> universal) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Universal quantifiers " Q: Find all companies that make only products with price < 25! Using NOT EXISTS: SELECT DISTINCT C.cname FROM Company2 C WHERE NOT EXISTS ( SELECT * FROM Product2 P WHERE C.cid = P.cid and P.price >= 25) 166

3. Subqueries in WHERE (exist not -> universal) 315 Product2 (pname, price, cid) Company2 (cid, cname, city) Universal quantifiers " Q: Find all companies that make only products with price < 25! Using ALL: SELECT DISTINCT C.cname FROM Company2 C WHERE 25 > ALL ( SELECT price FROM Product2 P WHERE P.cid = C.cid) SQLlite does not support "ALL" L 167

Question for Database Fans & Friends How can we unnest the universal quantifier query? This topic goes beyond the course objectives; only for those who are really interested 168

Queries that must be nested Definition: A query Q is monotone if: - Whenever we add tuples to one or more of the tables - the answer to the query cannot contain fewer tuples Fact: all unnested queries are monotone - Proof: using the "nested for loops" semantics Fact: Query with universal quantifier is not monotone This topic goes beyond the course objectives; only for those who are really interested - Add one tuple violating the condition. Then "all" returns fewer tuples Consequence: we cannot unnest a query with a universal quantifier 169

The drinkers-bars-beers example Likes(drinker, beer) Frequents(drinker, bar) Serves(bar, beer) Challenge: write these in SQL. Solutions: http://queryviz.com/online/ 331 Find drinkers that frequent some bar that serves some beer they like. x: $y. $z. Frequents(x, y)ùserves(y,z)ùlikes(x,z) Find drinkers that frequent only bars that serve some beer they like. x: "y. Frequents(x, y)þ ($z. Serves(y,z)ÙLikes(x,z)) Find drinkers that frequent some bar that serves only beers they like. x: $y. Frequents(x, y)ù"z.(serves(y,z) Þ Likes(x,z)) Find drinkers that frequent only bars that serve only beer they like. x: "y. Frequents(x, y)þ "z.(serves(y,z) Þ Likes(x,z)) 170

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

WITH clause 172

WITH clause: temporary relations SELECT pname, price FROM Product2 WHERE price = (SELECT max(price) FROM Product2) WITH max_price(value) as (SELECT max(price) FROM Product2) SELECT pname, price FROM Product2, max_price WHERE price = value 315 Product (pname, price, cid) The WITH clause defines a temporary relation that is available only to the query in which it occurs. Sometimes easier to read. Very useful for queries that need to access the same intermediate result multiple times 173

WITH clause: temporary relations SELECT pname, price FROM Product2 WHERE price = (SELECT max(price) FROM Product2) WITH max_price as (SELECT max(price) as value FROM Product2) SELECT pname, price FROM Product2, max_price WHERE price = value 315 Product (pname, price, cid) The WITH clause defines a temporary relation that is available only to the query in which it occurs. Sometimes easier to read. Very useful for queries that need to access the same intermediate result multiple times 174

Witnesses 175

Motivation: What are these queries supposed to return? Product2 PName Price cid Gizmo 15 1 SuperGizmo 20 1 itouch1 300 2 itouch2 300 2 Company2 cid cname city 1 GizmoWorks Oslo 2 Apple MountainView Find for each company id, the maximum? price amongst its products 176

Motivation: What are these queries supposed to return? Product2 PName Price cid Gizmo 15 1 SuperGizmo 20 1 itouch1 300 2 itouch2 300 2 Company2 cid cname city 1 GizmoWorks Oslo 2 Apple MountainView Find for each company id, the maximum price amongst its products cid mp 1 20 2 300 Find for each company id, the product? with maximum price amongst its products 177

Motivation: What are these queries supposed to return? Product2 PName Price cid Gizmo 15 1 SuperGizmo 20 1 itouch1 300 2 itouch2 300 2 Company2 cid cname city 1 GizmoWorks Oslo 2 Apple MountainView Find for each company id, the maximum price amongst its products cid mp 1 20 2 300 Find for each company id, the product with maximum price amongst its products (Recall that "group by cid" can just give us one single tuple per cid) cid mp pname 1 20 SuperGizmo 2 300 itouch1 2 300 itouch2 178

Witnesses: simple (1/4) 315 Q: Find the most expensive product + its price (Finding the maximum price alone would be easy) Product2 (pname, price, cid) 179

Witnesses: simple (2/4) 315 Q: Find the most expensive product + its price (Finding the maximum price alone would be easy) Our Plan: 1. Compute max price in a subquery Product2 (pname, price, cid) 1. SELECT max(p1.price) FROM Product2 P1 But we want the "witnesses," i.e. the product(s) with the max price. How do we do that? 180

Witnesses: simple (3/4) 315 Q: Find the most expensive product + its price (Finding the maximum price alone would be easy) Our Plan: 1. Compute max price in a subquery 2. Compute each product and its price... Product2 (pname, price, cid) 2. SELECT P2.pname, P2.price FROM Product2 P2 1. SELECT max(p1.price) FROM Product2 P1 But we want the "witnesses," i.e. the product(s) with the max price. How do we do that? 181

Witnesses: simple (4/4) 315 Q: Find the most expensive product + its price (Finding the maximum price alone would be easy) Our Plan: 1. Compute max price in a subquery 2. Compute each product and its price... and compare the price with the max price SELECT P2.pname, P2.price FROM Product2 P2 WHERE P2.price = (SELECT max(p1.price) FROM Product2 P1) Product2 (pname, price, cid) 182