Docsity

SQL DML: SELECT, INSERT, UPDATE, DELETE, Nested Queries, Set-Comparison, Division, Slides of Introduction to Database Management Systems

Partial preview of the text

Download SQL DML: SELECT, INSERT, UPDATE, DELETE, Nested Queries, Set-Comparison, Division and more Slides Introduction to Database Management Systems in PDF only on Docsity!

CS 186, Spring 2007, Lecture 8 R&G, Chapter 5 Mary Roth

The important thing is not to stop questioning. Albert Einstein Life is just a bowl of queries. -Anon (not Forrest Gump)

SQL 2 – The Sequel

Review: SQL DML

  • DML includes 4 main statements:

SELECT (query), INSERT, UPDATE and DELETE

  • e.g: SELECT S.name, E.cid

FROM Students S, Enrolled E

WHERE S.sid = E.sid AND

S.age=

SELECT PROJECT JOIN

SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R WHERE B1.color = ‘red’ AND B1.bid=R1.bid AND B2.color = ‘green’ AND B2.bid=R2.bid AND R1.sid=R2.sid Find sids of sailors who’ve reserved a red and a green boat 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 103 Santa Maria red 105 Titanic green 102 Pinta blue 101 Nina red bid bname color Find red reserved boats Find green reserved boats Find matching green and red reserved boats 2 1 sid 1 sid = (^1) sid

  • The WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses can too)

Nested Queries

SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) e.g. Find the names of sailors who’ve reserved boat #103: …and then check each the sid of each tuple in Sailors to see if it is in S First compute the set of all sailors that have reserved boat 103… 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S 2 sid S S Bilbo sname

  • Nested queries can also be correlated; the subquery refers to the enclosing query

Nested Queries with correlation

The subquery must be re-evaluated for each tuple in

enclosing query SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid)  EXISTS is a set operator that is true if result of set expression has at least one tuple

Nested Queries

e.g. Find the names of sailors who’ve reserved boat #103: 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S S S SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) sid bid day Notice that this query computes the same answer as the previous query! 321 2 103 9/

Use NOT Exists for Division

SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Find Sailors S such that ... there is no boat B... without a reservation by Sailor S

Find sailors who’ve reserved all boats.

X = set of sailors and Y = set of all boats with reservations. Recall: X/Y means only give me X tuples that have a match in Y.

2

Division

3 103 9/ 3 101 9/ 2 103 9/ 1 103 9/ 1 103 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S S S SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) 103 Pinta blue 101 Nina red bid bname color Boats 1 101 R R R B B 3 103 R R

ANY

SELECT *

FROM Sailors S WHERE S.rating > ANY(SELECT S2.rating FROM Sailors S WHERE S2.sname=‘Bilbo’) Find sailors whose rating is greater than that of some sailor called Bilbo: 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age S 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age S 2 Bilbo 2 39 sid sname rating age Correlated or uncorrelated? Uncorrelated!

Intermission

Find name and age of the oldest sailor(s)

SELECT S.sname, MAX (S.age) FROM Sailors S

Aggregate Operators

What will the result be? 3 Sam 8 27

2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Sam 39 Bilbo 39 Frodo 39 sname age X Not legal syntax; no other columns allowed in SELECT clause without a GROUP BY clause (we’ll learn about those next)

Find name and age of the oldest sailor(s)

SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2)

Aggregate Operators

Instead:

Find the maximum age… And then find the sailors(s) of that age…

Queries With GROUP BY

target-list contains:

  • list of column names from grouping–list
  • terms with aggregate operations (e.g., MIN ( S.age)). SELECT [DISTINCT] target-list FROM relation-list [WHERE qualification ] GROUP BY grouping-list
    • To generate values for a column based on groups of rows, use aggregate functions in SELECT statements with the GROUP BY clause First select these rows… Then group them by the values in these columns… And finally compute aggregate function over each group… Returning 1 row per group

Group By Example

For each rating, find the age of the youngest

sailor with age ≥ 18

SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating 3 Sam 8 27 4 Pippin 2 21 5 Merry 8 17 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Group 1 1 Frodo 7 22 4 Pippin 2 21 2 Bilbo 2 39 Group 2 Group 3 3 Sam 8 27 2 21 8 27 7 22