Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

Similar documents
CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

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

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

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

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Relational data model

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

The Relational Data Model. Data Model

Database Management Systems. Chapter 3 Part 1

SQL: The Query Language Part 1. Relational Query Languages

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Chapter 3

Keys, SQL, and Views CMPSCI 645

The Relational Model of Data (ii)

Database Applications (15-415)

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Review: Where have we been?

SQL. Chapter 5 FROM WHERE

The Relational Model. Week 2

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

SQL: Queries, Constraints, Triggers

CIS 330: Applied Database Systems

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

SQL: Queries, Programming, Triggers

The Relational Model 2. Week 3

Enterprise Database Systems

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

Basic form of SQL Queries

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

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

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

Introduction to Data Management. Lecture #14 (Relational Languages IV)

Database Systems ( 資料庫系統 )

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

CIS 330: Applied Database Systems

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

CompSci 516: Database Systems

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

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

The Relational Model

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

DBMS. Relational Model. Module Title?

The Relational Model

CSEN 501 CSEN501 - Databases I

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Database Applications (15-415)

Relational Query Languages

The Relational Model. Database Management Systems

Database Applications (15-415)

CompSci 516 Data Intensive Computing Systems

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

SQL - Lecture 3 (Aggregation, etc.)

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

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

CS 2451 Database Systems: Relational Data Model

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

Database Systems. Course Administration

Database Systems ( 資料庫系統 )

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

The Database Language SQL (i)

Introduction to Data Management. Lecture #4 E-R Model, Still Going

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

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

SQL: Queries, Programming, Triggers

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

QUERIES, PROGRAMMING, TRIGGERS

Modern Database Systems CS-E4610

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Lecture 5 Design Theory and Normalization

SQL: The Query Language (Part 1)

Outer Join, More on SQL Constraints

Overview of the Class and Introduction to DB schemas and queries. Lois Delcambre

CompSci 516 Data Intensive Computing Systems. Lecture 11. Query Optimization. Instructor: Sudeepa Roy

MIS Database Systems Relational Algebra

Modern Database Systems Lecture 1

CS425 Midterm Exam Summer C 2012

Database Management Systems. Chapter 5

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

CSE 544 Principles of Database Management Systems

Principles of Data Management. Lecture #9 (Query Processing Overview)

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Lecture 2: Introduction to SQL

SQL: Queries, Constraints, Triggers (iii)

Database Management Systems. Session 2. Instructor: Vinnie Costa

Relational Query Optimization. Highlights of System R Optimizer

Midterm Exam #2 (Version A) CS 122A Winter 2017

Transcription:

CompSci 516 Database Systems Lecture 2 SQL (Incomplete Notes) Instructor: Sudeepa Roy Announcements If you are enrolled to the class, but have not received the email from Piazza, please send me an email If you are on the waitlist and want to enroll, please send me an email HW1 will be released soon (~tomorrow) TA office hours: Yuchao: LSRC D309, Mondays 1:30-2:30 pm Tianpeng: LSRC D344, Wednesdays 1:30-2:30 pm Duke CS, Fall 2018 1 Duke CS, Fall 2018 2 Why use a DBMS Recap: Lecture 1 Structured data model: Relational data model table, schema, instance, tuples, attributes bag and set semantic Logical and physical data independence Today s topic Overview of XML SQL in a nutshell Reading material: [RG] Chapters 3 and 5 Additional reading for practice: [GUW] Chapter 6 Duke CS, Fall 2018 3 Acknowledgement: The following slides have been created adapting the instructor material of the [RG] book provided by the authors Dr. Ramakrishnan and Dr. Gehrke. Duke CS, Fall 2018 4 Semi-structured Data and XML XML: Extensible Markup Language XML: an overview Will not be covered in detail in class, but many datasets available to download are in this form You will download the DBLP dataset in XML format and transform into relational form (in HW1) Data does not have a fixed schema Attributes are part of the data The data is self-describing Tree-structured Duke CS, Fall 2018 5 Duke CS, Fall 2018 6 1

XML: Example Attributes <article mdate="2011-01-11 key="journals/acta/saxena96"> <author>sanjeev Saxena</author> <title>parallel Integer Sorting and Simulation Amongst CRCW Models.</title> <pages>607-619</pages> Elements <year>1996</year> <volume>33</volume> <journal>acta Inf.</journal> <number>7</number> <url>db/journals/acta/acta33.html#saxena96</url> <ee>http://dx.doi.org/10.1007/bf03036466</ee> </article> Attribute vs. Elements Elements can be repeated and nested Attributes are unique and atomic Duke CS, Fall 2018 7 Duke CS, Fall 2018 8 XML vs. Relational Databases Which one is easier? XML (semi-structured) to relational (structured) or relational (structured) to XML (semi-structured)? XML to Relational Model Problem 1: Repeated attributes <book> <author>ramakrishnan</author> <author>gehrke</author> <title>database Management Systems</title> <publisher> McGraw Hill </book> What is a good relational schema? Duke CS, Fall 2018 9 Duke CS, Fall 2018 10 XML to Relational Model Problem 1: Repeated attributes <book> <author>ramakrishnan</author> <author>gehrke</author> <title>database Management Systems</title> <pubisher> McGraw Hill</publisher> </book> Title Publisher Author1 Author2 XML to Relational Model Problem 1: Repeated attributes <book> <author>garcia-molina</author> <author>ullman</author> <author>widom</author> <title>database Systems The Complete Book</title> <pubisher>prentice Hall</publisher> </book> Does not work Title Publisher Author1 Author2 What if the paper has a single author? Duke CS, Fall 2018 11 Duke CS, Fall 2018 12 2

XML to Relational Model XML to Relational Model Book BookId Title Publisher b1 b2 Database Management Systems Database Systems The Complete Book McGraw Hill Prentice Hall BookAuthoredBy BookId Author b1 Ramakrishnan b1 Gehrke b2 Garcia-Molina b2 Ullman b2 Widom Problem 2: Missing attributes <book> <author>ramakrishnan</author> <author>gehrke</author> <title>database Management Systems</title> <pubisher> McGraw Hill <edition>third</edition> </book> <book> <author>garcia-molina</author> <author>ullman</author> <author>widom</author> <title>database Systems The Complete Book</title> <pubisher>prentice Hall</publisher> </book> BookI d b1 b2 Title Publisher Edition Database Manageme nt Systems Database Systems The Complete Book McGraw Hill Prentice Hall Third null Duke CS, Fall 2018 13 Duke CS, Fall 2018 14 Summary: Data Models Relational data model is the most standard for database managements and is the main focus of this course Semi-structured model/xml is also used in practice you will use them in hw assignments SQL (Stuctured Query Language) Unstructured data (text/photo/video) is unavoidable, but won t be covered in this class Duke CS, Fall 2018 15 Duke CS, Fall 2018 16 Relational Query Languages The SQL Query Language A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible for an efficient evaluation The key: precise semantics for relational queries Based on a sound theory! Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change. Developed by IBM (systemr) in the 1970s based on Ted Codd s relational model First called SEQUEL (Structured English Query Language) First commercialized by Oracle (then Relational Software)in 1979 Standards by ANSI and ISO since it is used by many vendors SQL-86, -89 (minor revision), -92 (major revision), -96, -99 (major extensions), -03, -06, -08, -11, -16 Duke CS, Fall 2018 17 Duke CS, Fall 2018 18 3

Purposes of SQL Data Manipulation Language (DML) Querying: SELECT-FROM-WHERE Modifying: INSERT/DELETE/UPDATE Data Definition Language (DDL) CREATE/ALTER/DROP The SQL Query Language To find all 18 year old students, we can write: all attributes SELECT * FROM Students S WHERE S.age=18 sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@ee 18 3.2 To find just names and logins, replace the first line: SELECT S.name, S.login Duke CS, Fall 2018 19 Duke CS, Fall 2018 20 Querying Multiple Relations What does the following query compute? SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= A Querying Multiple Relations What does the following query compute? SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade= A Given the following instances of Enrolled and Students: Students sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Enrolled sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B we get:?? Given the following instances of Enrolled and Students: Students sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B we get: Enrolled Creating Relations in SQL Creates the Students relation the type (domain) of each field is specified enforced by the DBMS whenever tuples are added or modified As another example, the Enrolled table holds information about courses that students take sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Students CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2)) sid cid grade 53831 Carnatic101 C 53831 Reggae203 B 53650 Topology112 A 53666 History105 B Enrolled Duke CS, Fall 2018 23 Destroying and Altering Relations DROP TABLE Students Destroys the relation Students The schema information and the tuples are deleted. ALTER TABLE Students ADD COLUMN firstyear: integer The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a NULL value in the new field. Duke CS, Fall 2018 24 4

Adding and Deleting Tuples Integrity Constraints (ICs) Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, Smith, smith@ee, 18, 3.2) Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = Smith IC: condition that must be true for any instance of the database e.g., domain constraints ICs are specified when schema is defined ICs are checked when relations are modified A legal instance of a relation is one that satisfies all specified ICs DBMS will not allow illegal instances If the DBMS checks ICs, stored data is more faithful to real-world meaning Avoids data entry errors, too! Duke CS, Fall 2018 25 Duke CS, Fall 2018 26 Keys in a Database Primary Key Constraints Key / Candidate Key Primary Key Super Key Foreign Key Primary key attributes are underlined in a schema Person(pid, address, name) Person2(address, name, age, job) A set of fields is a key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key Part 2 false? A superkey If there are > 1 keys for a relation, one of the keys is chosen (by DBA = DB admin) to be the primary key E.g., sid is a key for Students The set {sid, gpa} is a superkey. Any possible benefit to refer to a tuple using primary key (than any key)? Duke CS, Fall 2018 27 Duke CS, Fall 2018 28 Primary and Candidate Keys in SQL Possibly many candidate keys specified using UNIQUE one of which is chosen as the primary key. Primary and Candidate Keys in SQL Possibly many candidate keys specified using UNIQUE one of which is chosen as the primary key. For a given student and course, there is a single grade. CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY???) For a given student and course, there is a single grade. Duke CS, Fall 2018 29 Duke CS, Fall 2018 30 5

Primary and Candidate Keys in SQL Possibly many candidate keys specified using UNIQUE one of which is chosen as the primary key. Primary and Candidate Keys in SQL Possibly many candidate keys specified using UNIQUE one of which is chosen as the primary key. For a given student and course, there is a single grade. vs. Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade. For a given student and course, there is a single grade. vs. Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade. Duke CS, Fall 2018 31 Duke CS, Fall 2018 32 Primary and Candidate Keys in SQL Possibly many candidate keys specified using UNIQUE one of which is chosen as the primary key. For a given student and course, there is a single grade. vs. Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade. Used carelessly, an IC can prevent the storage of database instances that arise in practice! Duke CS, Fall 2018 33 Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to `refer to a tuple in another relation Must correspond to primary key of the second relation Like a `logical pointer E.g. sid is a foreign key referring to Students: Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved i.e., no dangling references Duke CS, Fall 2018 34 Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses Enrolled sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) Students sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Duke CS, Fall 2018 35 Enforcing Referential Integrity Consider Students and Enrolled sid in Enrolled is a foreign key that references Students. What should be done if an Enrolled tuple with a non-existent student id is inserted? Reject it! What should be done if a Students tuple is deleted? Three semantics allowed by SQL 1. Also delete all Enrolled tuples that refer to it (cascade delete) 2. Disallow deletion of a Students tuple that is referred to 3. Set sid in Enrolled tuples that refer to it to a default sid 4. (in addition in SQL): Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown or `inapplicable Similar if primary key of Students tuple is updated Duke CS, Fall 2018 36 6

Referential Integrity in SQL SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR(20) DEFAULT 000, cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT ) Where do ICs Come From? ICs are based upon the semantics of the real-world enterprise that is being described in the database relations Can we infer ICs from an instance? We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance. An IC is a statement about all possible instances! From example, we know name is not a key, but the assertion that sid is a key is given to us. Key and foreign key ICs are the most common; more general ICs supported too Duke CS, Fall 2018 38 Example Instances What does the key (sid, bid, day) in mean? If the key for the relation contained only the attributes (sid, bid), how would the semantics differ? Next Querying using SQL semantic joins group bys and aggregates nested queries Duke CS, Fall 2018 40 Basic SQL Query SELECT FROM WHERE [DISTINCT] <target-list> <relation-list> <qualification> relation-list A list of relation names possibly with a range variable after each name target-list A list of attributes of relations in relation-list qualification Comparisons (Attr op const) or (Attr1 op Attr2) where op is one of =, <, >, <=, >= combined using AND, OR and NOT DISTINCT is an optional keyword indicating that the answer should not contain duplicates Default is that duplicates are not eliminated! Conceptual Evaluation Strategy SELECT FROM WHERE [DISTINCT] <target-list> <relation-list> <qualification> Can see the next few slides first Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of <relation-list> Discard resulting tuples if they fail <qualifications> Delete attributes that are not in <target-list> If DISTINCT is specified, eliminate duplicate rows This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers Duke CS, Fall 2018 41 Duke CS, Fall 2018 42 7

Example of Conceptual Evaluation FROM s S, R WHERE S.sid=R.sid AND R.bid=103 Step 1: Form cross product of and Example of Conceptual Evaluation FROM s S, R WHERE S.sid=R.sid AND R.bid=103 Step 2: Discard tuples that do not satisfy <qualification> Example of Conceptual Evaluation FROM s S, R WHERE S.sid=R.sid AND R.bid=103 Step 3: Select the specified attribute(s) A Note on Range Variables Really needed only if the same relation appears twice in the FROM clause sometimes used as a short-name The previous query can also be written as: OR FROM s S, R WHERE S.sid=R.sid AND bid=103 SELECT sname FROM s, WHERE s.sid=.sid AND bid=103 It is good style, however, to use range variables always! Duke CS, Fall 2018 46 Find sailor ids who ve reserved at least one boat SELECT???? FROM s S, R WHERE S.sid=R.sid Find sailor ids who ve reserved at least one boat Would adding DISTINCT to this query make a difference? Duke CS, Fall 2018 47 Duke CS, Fall 2018 48 8

Find sailors who ve reserved at least one boat Would adding DISTINCT to this query make a difference? Condition/Theta-Join Equi-Join Natural-Join Joins (Left/Right/Full) Outer-Join Duke CS, Fall 2018 49 Duke CS, Fall 2018 50 Condition/Theta Join SELECT * FROM s S, R WHERE S.sid=R.sid and age >= 40 Form cross product, discard rows that do not satisfy the condition Equi Join SELECT * FROM s S, R WHERE S.sid=R.sid and age = 45 A special case of theta join Join condition only has equality predicate = Duke CS, Fall 2018 51 Duke CS, Fall 2018 52 Natural Join Outer Join SELECT * FROM s S NATURAL JOIN R A special case of equi join Equality condition on ALL common predicates (sid) Duplicate columns are eliminated bid day 101 10/10/96 103 11/12/96 101 10/10/96 103 11/12/96 101 10/10/96 103 11/12/96 SELECT S.sid, R. bid FROM s S LEFT OUTER JOIN R ON S.sid=R.sid Preserves all tuples from the left table whether or not there is a match if no match, fill attributes from right with null Similarly RIGHT/FULL outer join sid bid 22 101 31 null 58 103 Duke CS, Fall 2018 53 Duke CS, Fall 2018 54 9

Expressions and Strings SELECT S.age, age1=s.age-5, 2*S.age AS age2 FROM s S WHERE S.sname LIKE B_%B Illustrates use of arithmetic expressions and string pattern matching Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters LIKE is used for string matching. `_ stands for any one character and `% stands for 0 or more arbitrary characters You will need these often Find sid s of sailors who ve reserved a red or a green boat Assume a Boats relation UNION: Can be used to compute the union of any two union-compatible sets of tuples can themselves be the result of SQL queries If we replace OR by AND in the first version, what do we get? Also available: EXCEPT (What do we get if we replace UNION by EXCEPT?) s (sid, sname, rating, age) (sid, bid, day) Boats(bid, bname, color) Duke CS, Fall 2018 55 Find sid s of sailors who ve reserved a red and a green boat s (sid, sname, rating, age) (sid, bid, day) Boats(bid, bname, color) Find sid s of sailors who ve reserved a red and a green boat s (sid, sname, rating, age) (sid, bid, day) Boats(bid, bname, color) INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples. Included in the SQL/92 standard, but some systems don t support it Nested Queries Find names of sailors who ve reserved boat #103: FROM s S WHERE S.sid IN (SELECT R.sid FROM R WHERE R.bid=103) s (sid, sname, rating, age) (sid, bid, day) Boats(bid, bname, color) A very powerful feature of SQL: a WHERE/FROM/HAVING clause can itself contain an SQL query To find sailors who ve not reserved #103, use NOT IN. To understand semantics of nested queries, think of a nested loops evaluation For each s tuple, check the qualification by computing the subquery Duke CS, Fall 2018 59 Nested Queries with Correlation Find names of sailors who ve reserved boat #103: FROM s S WHERE EXISTS (SELECT * FROM R WHERE R.bid=103 AND S.sid=R.sid) EXISTS is another set comparison operator, like IN Illustrates why, in general, subquery must be recomputed for each s tuple Duke CS, Fall 2018 60 10

Nested Queries with Correlation Find names of sailors who ve reserved boat #103: FROM s S WHERE UNIQUE (SELECT R.bid FROM R WHERE R.bid=103 AND S.sid=R.sid) If UNIQUE is used, and * is replaced by R.bid, finds sailors with at most one reservation for boat #103 UNIQUE checks for duplicate tuples Duke CS, Fall 2018 61 More on Set-Comparison Operators We ve already seen IN, EXISTS and UNIQUE Can also use NOT IN, NOT EXISTS and NOT UNIQUE. Also available: op ANY, op ALL, op IN where op : >, <, =, <=, >= Find sailors whose rating is greater than that of some sailor called Horatio similarly ALL SELECT * FROM s S WHERE S.rating > ANY (SELECT S2.rating FROM s S2 WHERE S2.sname= Horatio ) Duke CS, Fall 2018 62 11