CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Similar documents
CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #2: The Rela0onal Model, and SQL/Rela0onal Algebra

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: Subqueries in SQL

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra

Database Applications (15-415)

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

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

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

Database Applications (15-415)

SQL: Queries, Constraints, Triggers

Database Management Systems. Chapter 5

SQL - Lecture 3 (Aggregation, etc.)

Basic form of SQL Queries

CIS 330: Applied Database Systems

SQL Subqueries. T. M. Murali. September 2, T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

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

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

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

SQL: Queries, Programming, Triggers

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

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

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Database Systems ( 資料庫系統 )

SQL. Chapter 5 FROM WHERE

Rela+onal Algebra. Rela+onal Query Languages. CISC437/637, Lecture #6 Ben Cartere?e

MIS Database Systems Relational Algebra

Relational Algebra and SQL

Database Management System. Relational Algebra and operations

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

Relational Algebra 1

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

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

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

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #10: Query Processing

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

Relational Model, Relational Algebra, and SQL

More on SQL Nested Queries Aggregate operators and Nulls

4/10/2018. Relational Algebra (RA) 1. Selection (σ) 2. Projection (Π) Note that RA Operators are Compositional! 3.

Relational Algebra. Relational Query Languages

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

Keys, SQL, and Views CMPSCI 645

Introduction to Data Management. Lecture #11 (Relational Algebra)

Database Applications (15-415)

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

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

Database Applications (15-415)

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Relational Query Languages

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

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

Database Management Systems. Chapter 5

Lecture 16. The Relational Model

L22: The Relational Model (continued) CS3200 Database design (sp18 s2) 4/5/2018

SQL - Data Query language

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

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

CSC 261/461 Database Systems Lecture 13. Fall 2017

Review: Where have we been?

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

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Query and Join Op/miza/on 11/5

SQL: The Query Language Part 1. Relational Query Languages

CompSci 516: Database Systems

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Database Management Systems. Chapter 5

Database Applications (15-415)

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

CompSci 516 Data Intensive Computing Systems

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

CS 582 Database Management Systems II

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Enterprise Database Systems

ECS 165B: Database System Implementa6on Lecture 14

Overview of Query Processing

CSIT5300: Advanced Database Systems

15-415/615 Faloutsos 1

Evaluation of Relational Operations

Relational Algebra 1. Week 4

QUERIES, PROGRAMMING, TRIGGERS

Relational Query Optimization. Highlights of System R Optimizer

SQL. SQL Queries. CISC437/637, Lecture #8 Ben Cartere9e. Basic form of a SQL query: 3/17/10

Implementation of Relational Operations

This lecture. Step 1

Relational Algebra 1

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

CSEN 501 CSEN501 - Databases I

CSCC43H: Introduction to Databases. Lecture 3

Relational Model and Relational Algebra

SQL: Queries, Programming, Triggers

Evaluation of relational operations

Chapter 2: The Relational Algebra

Administriva. CS 133: Databases. General Themes. Goals for Today. Fall 2018 Lec 11 10/11 Query Evaluation Prof. Beth Trushkowsky

The Database Language SQL (i)

Relational Algebra for sets Introduction to relational algebra for bags

Transcription:

CS 4604: Introduc0on to Database Management Systems B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Reminder: Rela0onal Algebra Rela2onal algebra is a nota2on for specifying queries about the contents of rela2ons Nota2on of rela2onal algebra eases the task of reasoning about queries Opera2ons in rela2onal algebra have counterparts in SQL Prakash 2014 VT CS 4604 2

What is an Algebra? An algebra is a set of operators and operands Arithme2c: operands are variables and constants, operators are +,,,, /, etc. Set algebra: operands are sets and operators are, U, - An algebra allows us to construct expressions by combining operands and expression using operators has rules for reasoning about expressions a² + 2 a b + 2b, (a + b)² R (R S), R S Prakash 2014 VT CS 4604 3

Rela0onal operators selec2on Projec2on σ condition π att list (R) (R) set union set difference R U S R - S Prakash 2014 VT CS 4604 #4

Clarifica0on: Projec0on The projec2on operator produces from a rela2on R a new rela2on containing only some of R s columns Delete (i.e. not show) a`ributes not in projec2on list Duplicates eliminated (sets vs mul2sets) To obtain a rela2on containing only the columns A1,A2,... An of R RA: π A1,A2,... An (R) SQL: SELECT A1,A2,... An FROM R; Prakash 2014 VT CS 4604 5

Clarifica0on: Projec0on The projec2on operator produces from a rela2on R a new rela2on containing only some of R s columns Delete (i.e. not show) a`ributes not in projec2on list Duplicates eliminated (sets vs mul2sets) To obtain a rela2on containing only the columns A1,A2,... An of R RA: π A1,A2,... An (R) SQL: SELECT DISTINCT A1,A2,... An FROM R; Prakash 2014 VT CS 4604 6

What about strings? find student ssns who live on main (st or str or street - ie., main st or main str ) Prakash 2014 VT CS 4604 7

What about strings? find student ssns who live on main (st or str or street) select ssn from student where address like main% %: variable- length don t care _: single- character don t care Prakash 2014 VT CS 4604 8

Rela0onal operators Are we done yet? Q: Give a query we can not answer yet! Prakash 2014 VT CS 4604 #9

Rela0onal operators A: any query across two or more tables, eg., find names of students in 4604 Q: what extra operator do we need?? STUDENT Ssn Name Address 123 smith main str 234 jones forbes ave SSN c-id grade 123 4604 A 234 3114 B Prakash 2014 VT CS 4604 #10

Rela0onal operators A: any query across two or more tables, eg., find names of students in 4604 Q: what extra operator do we need?? A: surprisingly, cartesian product is enough! STUDENT Ssn Name Address 123 smith main str 234 jones forbes ave SSN c-id grade 123 4604 A 234 3114 B Prakash 2014 VT CS 4604 #11

Cartesian Product The Cartesian product (or cross- product or product) of two rela2ons R and S is a the set of pairs that can be formed by pairing each tuple of R with each tuple of S. The result is a rela2on whose schema is the schema for R followed by the schema for S. RA: R X S SQL: SELECT * FROM R, S ; Prakash 2014 VT CS 4604 12

Cartesian Product S1 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid bid R1 day 22 101 10/10/96 58 103 11/12/96 S1 X R1 (sid) sname rating age (sid) bid day? 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 We rename a`ributes to avoid ambiguity or we prefix a`ribute with the name of the rela2on it belongs to. Prakash 2014 VT CS 4604 13

FUNDAMENTAL Rela0onal operators selec2on projec2on cartesian product set union set difference σ condition π att list R X S R U S R - S (R) (R) Prakash 2014 VT CS 4604 #14

Rela0onal ops Surprisingly, they are enough, to help us answer almost any query we want! derived/convenience operators: set intersec2on - - - (We have seen this) join (theta join, equi- join, natural join) rename operator division R S ρ ( R) R' Prakash 2014 VT CS 4604 #15

Theta- Join The theta- join of two rela2ons R and S is the set of tuples in the Cartesian product of R and S that sa2sfy some condi2on C. RA: R S SQL: SELECT * FROM R, S WHERE C; R S = σ (R x S) C Prakash 2014 VT CS 4604 16

Theta- Join S1 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 R1 sid bid day 22 101 10/10/96 58 103 11/12/96 S1 S1.sid<R1.sid R1 (sid) sname rating age (sid) bid day 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 103 11/12/96 R c S = σ c ( R S) Prakash 2014 VT CS 4604 17

Natural Join The natural join of two rela2ons R and S is a set of pairs of tuples, one from R and one from S, that agree on whatever a`ributes are common to the schemas of R and S. The schema for the result contains the union of the awributes of R and S. (so duplicate cols. are dropped) Assume the schemas R(A,B, C) and S(B, C,D)./ RA: R S SQL: SELECT R.A, R.B, R.C, S.D FROM R, S WHERE R.B = S.B AND R.C = S.C; Prakash 2014 VT CS 4604 18

Natural Join: Nit- picking What if R and S have not a`ributes in common? natural join à cartesian product Some (like Oracle) provide a special single NATURAL JOIN operator, but some (like IBM DB2) don t. So assume there is no special SQL natural join operator Prakash 2014 VT CS 4604 19

Operators so far Remove parts of single rela0ons Projec2on: Selec2on: Combining Projec2on and Selec2on: (A,B) (R) and SELECT A, B FROM R C(R) and SELECT * FROM R WHERE C (A,B) ( C (R)) SELECT A, B FROM R WHERE C Prakash 2014 VT CS 4604 20

Opera0ons so far Set opera0ons R and S must have the same a`ributes, same a`ribute types, and same order of a`ributes Union: R U S and (R) UNION (S) Intersec2on: R S and (R) INTERSECT (S) Difference: R S and (R) EXCEPT (S) Prakash 2014 VT CS 4604 21

Opera0ons so far Combine the tuples of two rela0ons Cartesian Product: R X S,. FROM R, S.. Theta Join: R S,. FROM R, S WHERE C./ Natural Join: R S Prakash 2014 VT CS 4604 22

Ordering find student records, sorted in name order select * from student order by name asc asc is the default Prakash 2014 VT CS 4604 23

Ordering find student records, sorted in name order; break 2es by reverse ssn select * from student order by name, ssn desc Prakash 2014 VT CS 4604 24

Q: why? Rename op. ρ AFTER (BEFORE) A: shorthand; self- joins; for example, find the grand- parents of Tom, given PC (parent- id, child- id) Prakash 2014 VT CS 4604 #25

Rename op. PC (parent- id, child- id) PC PC PC p-id Mary Peter John c-id Tom Mary Tom PC p-id Mary Peter John c-id Tom Mary Tom Prakash 2014 VT CS 4604 #26

Rename op. first, WRONG a`empt: PC PC (why? how many columns?) Second WRONG a`empt: PC PC. c id= PC. p id PC Prakash 2014 VT CS 4604 #27

Rename op. we clearly need two different names for the same table - hence, the rename op. ρ PC ( PC) 1 PC1. c id= PC. p id PC Prakash 2014 VT CS 4604 #28

Disambigua0on and Renaming RA: give R the name S; R has n a`ributes, which are ρ (R) S (A1,A2,... An) called A1, A2,..., An in S SQL: Use the AS keyword in the FROM clause: Students AS Students1 renames Students to Students1. SQL: Use the AS keyword in the SELECT clause to rename a`ributes. Prakash 2014 VT CS 4604 29

Disambigua0on and Renaming Name pairs of students who live at the same address: Students (Name, Address) RA: S1.Name,S2.Name ( S1.Address=S2.Address ( S1 (Students) S2 (Students))) SQL: SELECT S1.name, S2.name FROM Students AS S1, Students AS S2 WHERE S1.address = S2.address Prakash 2014 VT CS 4604 30

Disambigua0on and Renaming Name pairs of students who live at the same address: SQL: SELECT S1.name, S2.name FROM Students AS S1, Students AS S2 WHERE S1.address = S2.address Are these correct? No!!! the result includes tuples where a student is paired with himself/herself Solu2on: Add the condi2on S1.name <> S2.name. Prakash 2014 VT CS 4604 31

Division Rarely used, but powerful. Example: find suspicious suppliers, ie., suppliers that supplied all the parts in A_BOMB Prakash 2014 VT CS 4604 #32

Division SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 ABOMB p# p1 p2 = BAD_S s# s1 Prakash 2014 VT CS 4604 #33

Division Observa2ons: ~reverse of cartesian product It can be derived from the 5 fundamental operators (!!) How? Prakash 2014 VT CS 4604 #34

Division Answer: r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] Observa2on: find good suppliers, and subtract! (double nega0on) Prakash 2014 VT CS 4604 #35

Division Answer: r SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 s ABOMB p# p1 p2 = BAD_S s# s1 r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] R: a5ributes of r S: a5ributes of s Observa2on: find good suppliers, and subtract! (double nega0on) Prakash 2014 VT CS 4604 #36

Division Answer: r SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 s ABOMB p# p1 p2 = BAD_S s# s1 r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] All suppliers All bad parts Prakash 2014 VT CS 4604 #37

Division Answer: SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 ABOMB p# p1 p2 = BAD_S s# s1 r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] all possible suspicious shipments Prakash 2014 VT CS 4604 #38

Division Answer: SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 ABOMB p# p1 p2 = BAD_S s# s1 r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] all possible suspicious shipments that didn t happen Prakash 2014 VT CS 4604 #39

Division Answer: SHIPMENT s# p# s1 p1 s2 p1 s1 p2 s3 p1 s5 p3 ABOMB p# p1 p2 = BAD_S s# s1 r s = π ( R S )( r) π ( R S )[( π ( R S )( r) s) r] all suppliers who missed at least one suspicious shipment, i.e.: good suppliers Prakash 2014 VT CS 4604 #40

Quick Quiz: Independence of Operators R \ S = R (R S) R./ C = C (R S) R./ S =?? Prakash 2014 VT CS 4604 41

Quick Quiz: Independence of R./ S Operators Suppose R and S share the a`ributes A1,A2,..An Let L be the list of a`ributes in R \Union list of a`ributes in S (so no duplicate a`ributes) Let C be the condi2on R.A1 = S.A1 AND R.A2 = S.A2 AND.. R.An = S.An R./ S = L ( C (R S)) Prakash 2014 VT CS 4604 42

Linear Nota0on for Rela0onal Algebra Rela2onal algebra expressions can become very long. Use linear nota2on to store results of intermediate expressions. A rela2on name and a parenthesized list of a`ributes for that rela2on Use Answer as the conven2onal name for the final result The assignment symbol := Any expression in rela2onal algebra on the right Prakash 2014 VT CS 4604 43

Example of Linear Nota0on Name pairs of students who live at the same address. Normal expression: S1.Name,S2.Name ( S1.Address=S2.Address ( S1 (Students) S2 (Students))) Prakash 2014 VT CS 4604 44

Example of Linear Nota0on Normal expression: S1.Name,S2.Name ( S1.Address=S2.Address ( S1 (Students) S2 (Students))) Linear Nota2on: Pairs(P1, N1, A1, P2, N2, A2) := S1 (Students) S2 (Students) Matched(P1, N1, A1, P2, N2, A2) := A1=A2(Pairs(P1, N1, A1, P2, N2, A2)) Answer(Name1, Name2) := N1,N2 (Matched(P1, N1, A1, P2, N2, A2)) Prakash 2014 VT CS 4604 45

Interpre0ng Queries Involving Mul0ple Rela0ons SELECT A, B FROM R, S WHERE C; Nested loops: for each tuple t1 in R for each tuple t2 in S if the a`ributes in t1 and t2 sa2sfy C output the tuples involving a`ributes A and B Prakash 2014 VT CS 4604 46

Interpre0ng Queries Involving Mul0ple Rela0ons SELECT A, B FROM R, S WHERE C; Conversion to rela2onal algebra: A,B ( C (R S)) Compute R X S Apply selec2on operator σ() to R X S Project the result tuples to a`ributes A and B Prakash 2014 VT CS 4604 47

Aggregate func0ons find avg grade, across all students select?? from takes takes SSN c-id grade 123 15-413 4 234 15-413 3 Prakash 2014 VT CS 4604 48

Aggregate func0ons find avg grade, across all students select avg(grade) from takes result: a single number Which other func2ons? takes SSN c-id grade 123 15-413 4 234 15-413 3 Prakash 2014 VT CS 4604 49

Aggregate Operators COUNT (*) COUNT ( [DISTINCT] A) A is a column SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) Prakash 2014 VT CS 4604 50

Aggregate func0ons find total number of enrollments select count(*) from takes takes SSN c-id grade 123 15-413 4 234 15-413 3 Prakash 2014 VT CS 4604 51

Aggregate func0ons find total number of students in 15-413 select count(*) from takes takes where c- id= 15-413 SSN c-id grade 123 15-413 4 234 15-413 3 Prakash 2014 VT CS 4604 52

Sailors Find name and age of the oldest sid name age ra2ngs 45 Tom 34 5.0.. sailor(s) SELECT S.sname, MAX (S.age) FROM Sailors S This is illegal, but why? Cannot combine a column with a value SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2) Prakash 2014 VT CS 4604 53

GROUP BY and HAVING So far, aggregate operators are applied to all (qualifying) tuples. Can we apply them to each of several groups of tuples? Example: find the age of the youngest sailor for each ra2ng level. In general, we don t know how many ra2ng levels exist, and what the ra2ng values for these levels are! Suppose we know that ra2ng values go from 1 to 10; we can write 10 queries that look like this: For i = 1, 2,..., 10: SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i Prakash 2014 VT CS 4604 54

Find the age of the youngest sailor for each ra0ng level Sid Sname Ra2ng Age SELECT S.ra2ng, MIN (S.age) as age FROM Sailors S 22 Dus2n 7 45.0 GROUP BY S.ra2ng 31 Lubber 8 55.5 (1) The sailors tuples are put into same 85 Art 3 25.5 ra2ng groups. 32 Andy 8 25.5 (2) Compute the Minimum age for each 95 Bob 3 63.5 ra2ng group. Ra2ng Age Ra2ng Age 3 25.5 7 45.0 8 25.5 (2) Prakash 2014 VT CS 4604 3 25.5 3 63.5 7 45.0 8 55.5 8 25.5 (1) 55

Find the age of the youngest sailor for each ra0ng level that has at least 2 members SELECT S.ra2ng, MIN (S.age) as minage FROM Sailors S GROUP BY S.ra2ng HAVING COUNT(*) > 1 1. The sailors tuples are put into same ra2ng groups. 2. Eliminate groups that have < 2 members. 3. Compute the Minimum age for each ra2ng group. Ra2ng Sid Sname Ra2ng Age 22 Dus2n 7 45.0 31 Lubber 8 55.5 85 Art 3 25.5 32 Andy 8 25.5 95 Bob 3 63.5 Minage 3 25.5 8 25.5 Ra2ng Age 3 25.5 3 63.5 7 45.0 8 55.5 8 25.5 Prakash 2014 VT CS 4604 56

Drill find total number of students in each course select c- id, count(*) from takes group by c- id order by c- id SSN c-id grade 123 15-413 4 234 15-413 3 c-id count 15-413 2 Prakash 2014 VT CS 4604 57

Drill find total number of students in each course select c- id, count(*) from takes group by c- id order by c- id SSN c-id grade 123 15-413 4 234 15-413 3 c-id count 15-413 2 Prakash 2014 VT CS 4604 58

Drill find total number of students in each course, and sort by count, decreasing select c- id, count(*) as pop from takes group by c- id order by pop desc SSN c-id grade 123 15-413 4 234 15-413 3 c-id pop 15-413 2 Prakash 2014 VT CS 4604 59

Drill find total number of students in each course, and sort by count, decreasing select c- id, count(*) as pop from takes group by c- id order by pop desc SSN c-id grade 123 15-413 4 234 15-413 3 c-id pop 15-413 2 Prakash 2014 VT CS 4604 60

Queries With GROUP BY and HAVING SELECT [DISTINCT] target- list FROM rela2on- list WHERE qualifica2on GROUP BY grouping- list HAVING group- qualifica2on SELECT S.rating, MIN (S.age) as age FROM Sailors S GROUP BY S.rating HAVING S.rating > 5 The target- list contains (i) a`ribute names (ii) terms with aggregate opera2ons (e.g., AVG (S.age)). The a`ribute list (e.g., S.ra2ng) in target- list must be in grouping- list. The a`ributes in group- qualifica2on must be in grouping- list. Prakash 2014 VT CS 4604 61

Mo0va0on for Subqueries Find the name of the professor who teaches CS 4604. SELECT Name FROM Professors, Teach WHERE (PID = ProfessorPID) AND (Number = 4604 ) AND (DeptName = CS ); Do we need to take the natural join of two big rela2ons just to get a rela2on with one tuple? Can we rewrite the query without using a join? Prakash 2014 VT CS 4604 62

Nes0ng A query can be put inside another query Most commonly in the WHERE clause Some2mes in the FROM clause (depending on the so ware) This subquery is executed first (if possible) Prakash 2014 VT CS 4604 63