Database Systems CSE 414

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

CSE 344 JANUARY 19 TH SUBQUERIES 2 AND RELATIONAL ALGEBRA

Introduction to Database Systems CSE 414

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

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

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

Introduction to Database Systems CSE 444

CSE 544 Principles of Database Management Systems

CSE 544 Principles of Database Management Systems

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

Introduction to Database Systems CSE 414

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

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

CSE344 Midterm Exam Fall 2016

Database Systems CSE 414

Introduction to Data Management CSE 414

Database Management Systems CSEP 544. Lecture 3: SQL Relational Algebra, and Datalog

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

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

CSC 261/461 Database Systems Lecture 5. Fall 2017

SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON Product.name = Purchase.prodName

Introduction to Data Management CSE 344

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

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

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

SQL-Nested Queries & Aggregate functions. Lecture By Binu Jasim 02-Aug-2016

Introduction to Data Management CSE 344

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

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

Database Systems CSE 414

Announcements. From SQL to RA. Query Evaluation Steps. An Equivalent Expression

Announcements. Joins in SQL. Joins in SQL. (Inner) joins. Joins in SQL. Introduction to Database Systems CSE 414

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

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

Lecture 4: Advanced SQL Part II

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

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

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

Database Systems CSE 414

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

SQL. Structured Query Language

CSE 344 MAY 7 TH EXAM REVIEW

CS 186, Fall 1999 Midterm 2 Professor Hawthorn

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Introduction to Data Management CSE 344. Lectures 9: Relational Algebra (part 2) and Query Evaluation

Introduction to Database Systems CSE 444

CS 186, Fall 1999 Midterm 2 Professor Hawthorn

Querying Data with Transact SQL

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

Database Systems CSE 414

CSE 544 Principles of Database Management Systems

Administrivia. Physical Database Design. Review: Optimization Strategies. Review: Query Optimization. Review: Database Design

Announcements. What is Datalog? Why Do We Learn Datalog? Database Systems CSE 414. Midterm. Datalog. Lecture 13: Datalog (Ch

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)

Please pick up your name card

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

SQL Aggregation and Grouping. Outline

SQL Aggregation and Grouping. Davood Rafiei

8. Relational Calculus (Part II)

Optimization of Nested Queries in a Complex Object Model

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

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

Nested Queries. Dr Paolo Guagliardo. Aggregate results in WHERE The right way. Fall 2018

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

CS 564 Midterm Review

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

CMPT 354: Database System I. Lecture 4. SQL Advanced

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

CSC 261/461 Database Systems Lecture 19

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Lectures 2&3: Introduction to SQL

Lecture 04: SQL. Monday, April 2, 2007

CS 2451 Database Systems: More SQL

SQL. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

Querying Data with Transact-SQL

CSE 414 Midterm. April 28, Name: Question Points Score Total 101. Do not open the test until instructed to do so.

Lecture 04: SQL. Wednesday, October 4, 2006

Database Design and Tuning

Announcements. Two Classes of Database Applications. Class Overview. NoSQL Motivation. RDBMS Review: Serverless

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Announcements. JSon Data Structures. JSon Syntax. JSon Semantics: a Tree! JSon Primitive Datatypes. Introduction to Database Systems CSE 414

Detecting Logical Errors in SQL Queries

Introduction to Database Systems CSE 344

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 7 - Query execution

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

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

CSE 344 Introduction to Data Management. Section 2: More SQL

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

Querying Data with Transact-SQL

LAB 3 Part 1 ADBC - Interactive SQL Queries-Advanced

Introduction to Data Management CSE 344

Introduction to Database Systems CSE 414

Introduction to Data Management CSE 344

Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

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

Transcription:

Database Systems CSE 414 Lecture 6: Nested Queries in SQL 1

Announcements WQ2 is due on Sunday 11pm no late days HW2 is due on Tuesday 11pm 2

Lecture Goals Today we will learn how to write (even) more powerful SQL queries Reading: Ch. 6.3 3

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 A FROM clause A WHERE clause Rule of thumb: avoid nested queries when possible; keep in mind that sometimes it s impossible (though use in FROM is often not as bad) 4

Subqueries Can return a single constant and this constant can be compared with another value in a WHERE clause Can return relations that can be used in various ways in WHERE clauses Can appear in FROM clauses, followed by a tuple variable that represents the tuples in the result of the subquery Can appear as computed values in a SELECT clause 5

1. Subqueries in SELECT Product (pname, price, cid) For each product return the city where it is manufactured SELECT X.pname, (SELECT Y.city FROM Company Y WHERE Y.cid=X.cid) as City FROM Product X What happens if the subquery returns more than one city? We get a runtime error (SQLite simply ignores the extra values) 6

1. Subqueries in SELECT Product (pname, price, cid) For each product return the city where it is manufactured SELECT X.pname, (SELECT Y.city FROM Company Y WHERE Y.cid=X.cid) as City FROM Product X What happens if the subquery returns more than one city? We get a runtime error (SQLite simply ignores the extra values) correlated subquery 7

1. Subqueries in SELECT Whenever possible, don t use a nested queries: SELECT X.pname, (SELECT Y.city FROM Company Y WHERE Y.cid=X.cid) as City FROM Product X = DBMS also does this SELECT X.pname, Y.city FROM Product X, Company Y WHERE X.cid=Y.cid We have unnested the query 8

1. Subqueries in SELECT Compute the number of products made by each company SELECT DISTINCT C.cname, (SELECT count(*) FROM Product P WHERE P.cid=C.cid) FROM Company C Better: we can unnest by using a GROUP BY SELECT C.cname, count(*) FROM Company C, Product P WHERE C.cid=P.cid GROUP BY C.cname 9

1. Subqueries in SELECT But are these really equivalent? SELECT DISTINCT C.cname, (SELECT count(*) FROM Product P WHERE P.cid=C.cid) FROM Company C SELECT C.cname, count(*) FROM Company C, Product P WHERE C.cid=P.cid GROUP BY C.cname No! Different results if a company has no products SELECT C.cname, count(pname) FROM Company C LEFT OUTER JOIN Product P ON C.cid=P.cid GROUP BY C.cname 10

2. Subqueries in FROM Find all products whose prices is > 20 and < 500 SELECT X.pname FROM (SELECT * FROM Product AS Y WHERE price > 20) as X WHERE X.price < 500 Unnest this query! SELECT pname FROM Product WHERE price > 20 AND price < 500 11

2. Subqueries in FROM We will see that sometimes we really need a subquery will see most compelling examples next lecture in that case, we can put it in the FROM clause 12

3. Subqueries in WHERE Find all companies that make some products with price < 100 Existential quantifiers Using EXISTS: SELECT DISTINCT C.cname FROM Company C WHERE EXISTS (SELECT * FROM Product P WHERE C.cid = P.cid and P.price < 100) 13

3. Subqueries in WHERE Find all companies that make some products with price < 100 Existential quantifiers Using IN SELECT DISTINCT C.cname FROM Company C WHERE C.cid IN (SELECT P.cid FROM Product P WHERE P.price < 100) 14

3. Subqueries in WHERE Find all companies that make some products with price < 100 Existential quantifiers Using ANY: SELECT DISTINCT C.cname FROM Company C WHERE 100 > ANY (SELECT price FROM Product P WHERE P.cid = C.cid) Not supported in sqlite 15

3. Subqueries in WHERE Find all companies that make some products with price < 100 Existential quantifiers Now let s unnest it: SELECT DISTINCT C.cname FROM Company C, Product P WHERE C.cid= P.cid and P.price < 100 Existential quantifiers are easy! J 16

3. Subqueries in WHERE Find all companies where all their products have price < 100 same as: Find all companies that make only products with price < 100 Universal quantifiers Universal quantifiers are hard! L 17

3. Subqueries in WHERE Find all companies where all their products have price < 100 1. Find the other companies: i.e. with some product >= 100 SELECT DISTINCT C.cname FROM Company C WHERE C.cid IN (SELECT P.cid FROM Product P WHERE P.price >= 100) 2. Find all companies where all their products have price < 100 SELECT DISTINCT C.cname FROM Company C WHERE C.cid NOT IN (SELECT P.cid FROM Product P WHERE P.price >= 100) 18

3. Subqueries in WHERE Find all companies where all their products have price < 100 Universal quantifiers Using EXISTS: SELECT DISTINCT C.cname FROM Company C WHERE NOT EXISTS (SELECT * FROM Product P WHERE P.cid = C.cid and P.price >= 100) 19

3. Subqueries in WHERE Find all companies where all their products have price < 100 Universal quantifiers Using ALL: SELECT DISTINCT C.cname FROM Company C WHERE 100 >= ALL (SELECT price FROM Product P WHERE P.cid = C.cid) Not supported in sqlite 20

Question for Database Fans and their Friends Can we unnest the universal quantifier query? 21

Monotone Queries Definition A query Q is monotone if: Whenever we add tuples to one or more input tables, the answer to the query will not lose any of the tuples Product Company pname price cid Gizmo 19.99 c001 cid cname city c002 Sunworks Bonn Q A Gizmo B Lyon Gadget 999.99 c004 c001 DB Inc. Lyon Camera Lodtz Camera 149.99 c003 c003 Builder Lodtz Product pname price cid Gizmo 19.99 c001 Gadget 999.99 c004 Camera 149.99 c003 Company cid cname city c002 Sunworks Bonn c001 DB Inc. Lyon c003 Builder Lodtz Q A Gizmo Camera ipad B Lyon Lodtz Lyon ipad 499.99 c001

Monotone Queries Theorem: If Q is a SELECT-FROM-WHERE query that does not have subqueries, and no aggregates, then it is monotone. Proof. We use the nested loop semantics: if we insert a tuple in a relation R i, this will not remove any tuples from the answer 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 for x 1 in R 1 do for x 2 in R 2 do.. for x n in R n do if Conditions output (a 1,,a k ) 23

The query: is not monotone Monotone Queries Find all companies where all their products have price < 100 pname price cid Gizmo 19.99 c001 cid cname city c001 Sunworks Bonn cname Sunworks pname price cid Gizmo 19.99 c001 Gadget 999.99 c001 cid cname city c001 Sunworks Bonn cname Consequence: we cannot write it as a SELECT- FROM-WHERE query without nested subqueries 24

Queries that must be nested (that is, cannot be SFW queries) Queries with universal quantifiers or negation Queries that use aggregates in certain ways Note: sum(..) etc. are NOT monotone select count(*) from R is not monotone! 25