SQL The Query Language R & G - Chapter 5

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SQL: The Query Language CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5 Life is just a bowl of queries. -Anon.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
SQL: Queries, Constraints, Triggers
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Database Systems October 14, 2009 Lecture #5.
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
SQL: Queries, Constraints, Triggers
SQL: The Query Language
SQL: Queries, Programming, Triggers
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: The Query Language (Part III)
SQL: Queries, Programming, Triggers
Presentation transcript:

SQL The Query Language R & G - Chapter 5 Based on Slides from UC Berkeley and book.

Query Execution Declarative Query (SQL) 2 Query Execution Declarative Query (SQL) We start from here Query Optimization and Execution (Relational) Operators File and Access Methods Buffer Management Disk Space Management

GROUP BY 3 SELECT [DISTINCT] AVG(S.gpa), S.dept FROM students S [WHERE <predicate>] GROUP BY S.dept [HAVING <predicate>] [ORDER BY <column list>] ; Partition table into groups with same GROUP BY column values Can group by a list of columns Produce an aggregate result per group Cardinality of output = # of distinct group values Note: can put grouping columns in SELECT list For aggregate queries, SELECT list can contain aggs and GROUP BY columns only! What would it mean if we said SELECT S.name, AVG(S.gpa) above?? SELECT AVG(S.gpa), S.dept FROM students S GROUP BY S.dept;

4 HAVING SELECT [DISTINCT] AVG(S.gpa), S.dept FROM students S [WHERE <predicate>] GROUP BY S.dept HAVING COUNT(*) > 5 [ORDER BY <column list>] ; The HAVING predicate is applied after grouping and aggregation Hence can contain anything that could go in the SELECT list That is, aggs or GROUP BY columns HAVING can only be used in aggregate queries It’s an optional clause SELECT AVG(S.gpa), S.dept FROM students S GROUP BY S.dept HAVING COUNT(*) > 5

Putting it all together 5 Putting it all together SELECT S.dept, AVG(S.gpa), COUNT(*) FROM students S WHERE S.gender = 'F' GROUP BY S.dept HAVING COUNT(*) > 2 ORDER BY S.dept ; SELECT S.dept, AVG(S.gpa), COUNT(*) FROM students S WHERE S.gender = 'F' GROUP BY S.dept HAVING COUNT(*) > 2 ORDER BY S.dept ;

Conceptual SQL Evaluation 6 SELECT S.dept, AVG(S.gpa), COUNT(*) FROM students S WHERE S.gender = 'F' GROUP BY S.dept HAVING COUNT(*) > 2 ORDER BY S.dept ; Project away columns (just keep those used in SELECT, GBY, HAVING) SELECT [DISTINCT] Eliminate duplicates Apply selections (eliminate rows) WHERE HAVING Eliminate groups Access Relation FROM GROUP BY

Multi-Relation Queries

Querying Multiple Relations 8 Querying Multiple Relations SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 102 Sailors Reserves sid sname rating age 1 Popeye 10 22 2 OliveOyl 11 39 3 Garfield 27 4 Bob 5 19 sid bid day 1 102 9/12 2 9/13 101 10/01 CREATE TABLE Sailors2 (sid INTEGER, sname CHAR(20), rating INTEGER, age REAL, PRIMARY KEY (sid)); CREATE TABLE Boats2 (bid INTEGER, bname CHAR (20), color CHAR(10), PRIMARY KEY (bid)); CREATE TABLE Reserves2 (sid INTEGER, bid INTEGER, day DATE, PRIMARY KEY (sid, bid, day), FOREIGN KEY (sid) REFERENCES Sailors, FOREIGN KEY (bid) REFERENCES Boats); INSERT INTO Sailors2 VALUES (1, 'Popeye', 10, 22), (2, 'OliveOyl', 11, 39), (3, 'Garfield', 1, 27), (4, 'Bob', 5, 19); INSERT INTO Reserves2 VALUES (1, 102, '2016-08-30'), (2, 102, '2016-08-31'), (1, 101, '2016-09-01');

Querying Multiple Relations 9 Querying Multiple Relations SELECT S.sname FROM Sailors S, Reserves R Cartesian product Popeye OliveOyl Garfield Bob (1, 102, 9/12) (2, 102, 9/13) (1, 101, 10/1) X

Meaning (Semantics) of SQL Queries SELECT x1.a1, x1.a2, …, xn.ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE Conditions(x1,…, xn) Almost never the fastest way to compute it! Answer = {} for x1 in R1 do for x2 in R2 do ….. for xn in Rn do if Conditions(x1,…, xn) then Answer = Answer  {(x1.a1, x1.a2, …, xn.ak)} return Answer Note: this is a multiset union

11 Join Queries SELECT [DISTINCT] <column expression list> FROM <table1 [AS t1], ... , tableN [AS tn]> [WHERE <predicate>] [GROUP BY <column list>] [HAVING <predicate>] [ORDER BY <column list>] ;

Query Semantics SELECT [DISTINCT] target-list FROM relation-list 12 Query Semantics SELECT [DISTINCT] target-list FROM relation-list WHERE qualification FROM: compute cross product of tables. WHERE: Check conditions, discard tuples that fail. SELECT: Specify desired fields in output. DISTINCT (optional): eliminate duplicate rows. Note: this is likely a terribly inefficient strategy! Query optimizer will find more efficient plans.

Conceptual SQL Evaluation 13 SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Project away columns (just keep those used in SELECT, GBY, HAVING) SELECT [DISTINCT] Eliminate duplicates Apply selections (eliminate rows) WHERE HAVING Eliminate groups Relation cross-product FROM GROUP BY

Find sailors who have reserved at least one boat 14 Find sailors who have reserved at least one boat Will DISTINCT make a difference here? SELECT S.sid FROM Sailors AS S, Reserves AS R WHERE S.sid = R.sid SELECT S.sid FROM Sailors2 AS S, Reserves2 AS R WHERE S.sid = R.sid

About Range Variables SELECT x.sname, x.age, y.sname, y.age 15 About Range Variables Needed when ambiguity could arise. e.g., same table used multiple times in FROM (“self-join”) SELECT x.sname, x.age, y.sname, y.age FROM Sailors AS x, Sailors AS y WHERE x.age > y.age Sailors Self-join use case: Employees table containing employees and their managers. SELECT x.sname, x.age, y.sname, y.age FROM Sailors2 AS x, Sailors2 AS y WHERE x.age > y.age; sid sname rating age 1 Popeye 10 22 2 OliveOyl 11 39 3 Garfield 27 4 Bob 5 19

Arithmetic Expressions 16 Arithmetic Expressions SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors AS S WHERE S.sname = 'Popeye' SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors AS S1, Sailors AS S2 WHERE 2*S1.rating = S2.rating - 1 SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors2 AS S WHERE S.sname = 'Popeye'; SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors2 AS S1, Sailors2 AS S2 WHERE 2*S1.rating = S2.rating - 1;

String Comparisons SELECT S.sname FROM Sailors s 17 String Comparisons SELECT S.sname FROM Sailors s WHERE S.sname LIKE 'P_p%' '_' stands for any one character and '%' stands for 0 or more arbitrary characters. Most DBMSs now support standard regex as well (incl. PostgreSQL) SELECT S.sname FROM Sailors2 s WHERE S.sname LIKE 'P_p%';

Find sid of sailors who’ve reserved a red or green boat 18 SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color='red' OR B.color='green') ... or: SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color='red' UNION ALL WHERE R.bid=B.bid AND B.color='green' Do these two queries have the same execution plan? SELECT R.sid, s.sname FROM Boats B, Reserves2 R, Sailors s WHERE R.bid=B.bid AND s.sid = R.sid AND (B.color='red' OR B.color='green'); SELECT R.sid FROM Boats B, Reserves2 R WHERE R.bid=B.bid AND B.color='red' UNION ALL WHERE R.bid=B.bid AND B.color='green';

Find sid of sailors who’ve reserved a red AND a green boat 19 SELECT R.sid FROM Boats B,Reserves R WHERE R.bid=B.bid AND (B.color='red' AND B.color='green') SELECT R.sid FROM Boats B,Reserves2 R WHERE R.bid=B.bid AND (B.color='red' AND B.color='green') ;

Find sid of sailors who’ve reserved a red AND a green boat 20 SELECT R.sid FROM Boats B,Reserves R WHERE R.bid=B.bid AND (B.color='red' AND B.color='green')

Find sid of sailors who’ve reserved a red AND a green boat 21 SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color='red' INTERSECT AND B.color='green' SELECT S.sid FROM Sailors2 S, Boats B, Reserves2 R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color='red' INTERSECT AND B.color='green' ;

Find sid of sailors who’ve reserved a red AND a green boat 22 Could use a self-join: SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE R1.sid=R2.sid AND R1.bid=B1.bid AND R2.bid=B2.bid AND (B1.color='red' AND B2.color='green') SELECT R1.sid FROM Boats B1, Reserves2 R1, Boats B2, Reserves2 R2 WHERE R1.sid=R2.sid AND R1.bid=B1.bid AND R2.bid=B2.bid AND (B1.color='red' AND B2.color='green') ;

Find sids of sailors who have not reserved a boat 23 Find sids of sailors who have not reserved a boat SELECT S.sid FROM Sailors S EXCEPT FROM Sailors S, Reserves R WHERE S.sid=R.sid Set Difference SELECT S.sid FROM Sailors2 S EXCEPT FROM Sailors2 S, Reserves2 R WHERE S.sid=R.sid ;

Nested Queries: IN Names of sailors who’ve reserved boat #102 24 Nested Queries: IN Names of sailors who’ve reserved boat #102 SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=102) SELECT S.sname FROM Sailors2 S WHERE S.sid IN (SELECT R.sid FROM Reserves2 R WHERE R.bid=102) ;

Nested Queries: NOT IN Names of sailors who’ve not reserved boat #103 25 Nested Queries: NOT IN Names of sailors who’ve not reserved boat #103 SELECT S.sname FROM Sailors S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) SELECT S.sname FROM Sailors2 S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves2 R WHERE R.bid=103);

Nested Queries with Correlation 26 Nested Queries with Correlation Names of sailors who’ve reserved boat #102 Subquery must be recomputed for each Sailors tuple. Think of subquery as a function call that runs a query SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=102 AND S.sid=R.sid) SELECT S.sname FROM Sailors2 S WHERE EXISTS (SELECT * FROM Reserves2 R WHERE R.bid=102 AND S.sid=R.sid);

More on Set-Comparison Operators 27 More on Set-Comparison Operators We have seen: IN, EXISTS can also have: NOT IN, NOT EXISTS Other forms: <op> ANY, <op> ALL Find sailors whose rating is greater than that of some sailor called ‘Popeye’ SELECT * FROM Sailors S WHERE S.rating > ANY (SELECT S2.rating FROM Sailors S2 WHERE S2.sname='Popeye') SELECT * FROM Sailors2 S WHERE S.rating > ANY (SELECT S2.rating FROM Sailors2 S2 WHERE S2.sname='Popeye') ;

A Tougher Query Sailors S such that… there is no boat B that … 28 Find sailors who’ve reserved ALL boats (relational division: no “counterexample boats”) SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Sailors S such that… there is no boat B that … SELECT S.sname FROM Sailors2 S WHERE NOT EXISTS (SELECT B.bid FROM Boats B (SELECT R.bid FROM Reserves2 R WHERE R.bid=B.bid AND R.sid=S.sid)) ; ... is missing a Reserves tuple showing that S reserved B

A Tougher Query Sailors S such that… there is no boat B that … 29 Find sailors who’ve reserved ALL boats (here we use set difference: from all the boats remove the ones that Sailor S has reserved. If empty, then S is good) SELECT S.sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) Sailors S such that… there is no boat B that … SELECT S.sname FROM Sailors2 S WHERE NOT EXISTS (SELECT B.bid FROM Boats B (SELECT R.bid FROM Reserves2 R WHERE R.bid=B.bid AND R.sid=S.sid)) ; that has not been reserved by S

A Tougher Query Sailors S such that… 30 Find sailors who’ve reserved ALL boats ( here we use count aggregates: count the total number of boats and the number of boats reserved by S) SELECT S.sname FROM Sailors S WHERE (SELECT COUNT(B.bid) FROM Boats B) = (SELECT COUNT (DISTINCT R.bid) FROM Reserves R WHERE R.sid=S.sid) Sailors S such that… SELECT S.sname FROM Sailors2 S WHERE NOT EXISTS (SELECT B.bid FROM Boats B (SELECT R.bid FROM Reserves2 R WHERE R.bid=B.bid AND R.sid=S.sid)) ; total number of boats equal to the number of distinct boats reserved by S

ARGMAX? The Sailor with the highest rating SELECT MAX(S.rating) 31 ARGMAX? The Sailor with the highest rating What about ties for highest? SELECT MAX(S.rating) FROM Sailors S; -- OK SELECT S.*, MAX(S.rating) FROM Sailors S; -- Not OK

ARGMAX? The Sailor with the highest rating SELECT * FROM Sailors S 32 ARGMAX? The Sailor with the highest rating What about ties for highest? SELECT * FROM Sailors S WHERE S.rating >= ALL (SELECT S2.rating FROM Sailors S2) SELECT * FROM Sailors S WHERE S.rating = (SELECT MAX(S2.rating) FROM Sailors S2) SELECT * FROM Sailors2 S WHERE S.rating >= ALL (SELECT S2.rating FROM Sailors2 S2); WHERE S.rating = (SELECT MAX(S2.rating) FROM Sailors2 S2); ORDER BY rating DESC LIMIT 1; SELECT * FROM Sailors S ORDER BY rating DESC LIMIT 1;

NULL Values Field values are sometimes unknown or inapplicable 33 NULL Values Field values are sometimes unknown or inapplicable SQL provides a special value null for such situations. The presence of null complicates many issues. E.g.: Special syntax “IS NULL” and “IS NOT NULL” Assume rating IS NULL. Consider predicate “rating>8”. True? False? What about AND, OR and NOT connectives? SUM? We need a 3-valued logic (true, false and unknown). Meaning of constructs must be defined carefully. (e.g., WHERE clause eliminates rows that don’t evaluate to true.) New operators (in particular, outer joins) possible/needed.

NULL Values: Truth table 34 NULL Values: Truth table p q p OR q p AND q p = q TRUE FALSE Unknown https://en.wikipedia.org/wiki/Null_(SQL)