SQL: Queries, Programming, Triggers

Chapter 5

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke



Example Instances 22 101 10/10/96
We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ?

sid bid


103 11/12/96


sid 22 31 58

sname rating age dustin 7 45.0 lubber 8 55.5 rusty 10 35.0


sid 28 31 44 58

sname rating age yuppy 9 35.0 lubber 8 55.5 guppy 5 35.0 rusty 10 35.0

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Basic SQL Query


[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!
¡ ¡  

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke


The previous query can also be written as: ¡ SELECT FROM WHERE S.sid AND bid=103 sname Sailors. Gehrke A Note on Range Variables Really needed only if the same relation appears twice in the FROM clause.sname Sailors S. R.sid=R. Ramakrishnan and J. Gehrke Example of Conceptual Evaluation S. to use range variables always! Database Management Systems 3ed. 4 Database Management Systems 3ed.Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:     ¡ ¡ Compute the cross-product of relation-list. If DISTINCT is specified. eliminate duplicate rows. Ramakrishnan and J.     This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Reserves R S. R.5 55.0 (sid) bid day 22 58 22 58 22 58 101 10/10/96 103 11/12/96 101 10/10/96 103 11/12/96 101 10/10/96 103 11/12/96 5 Database Management Systems 3ed. Reserves Sailors. Reserves R WHERE S.sname Sailors S.sid AND bid=103 OR SELECT FROM WHERE It is good style.sid=Reserves.0 55.0 45. Discard resulting tuples if they fail qualifications. Delete attributes that are not in target-list.0 35.5 35. however. Gehrke 6 . R.sid=R. Ramakrishnan and J.sid AND R.bid=103 SELECT FROM (sid) sname rating age 22 dustin 22 dustin 31 lubber 31 lubber 58 rusty 58 rusty 7 7 8 8 10 10 45.

bid=B. Gehrke 8 Find sid’s of sailors who’ve reserved a red or a green boat UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries). If we replace OR by AND in the first version.Find sailors who’ve reserved at least one boat SELECT S. R.sid by S. Boats B.color=‘green’) SELECT S.color=‘green’ 9 Database Management Systems 3ed. LIKE is used for string matching.sid=R. Boats B.bid AND B.sid R Would adding DISTINCT to this query make a difference? What is the effect of replacing S. 2*S.age-5. what do we get? Also available: EXCEPT (What do we get if we replace UNION by EXCEPT?)       SELECT S.sid AND R.sid FROM Sailors S. Reserves WHERE S. age1=S.color=‘red’ OR B.bid AND B.age AS FROM Sailors S WHERE S. Reserves R WHERE S. Ramakrishnan and J.sid AND R. Ramakrishnan and J.sid AND R.sid FROM Sailors S.color=‘red’ UNION SELECT S. Reserves R WHERE S. Boats B. Reserves R WHERE S.sid FROM Sailors S. R. Ramakrishnan and J.sid FROM Sailors S.age.     Database Management Systems 3ed.bid AND (B.bid=B. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters.sid=R.bid=B. Gehrke 7 Expressions and Strings SELECT S.sid=R. Gehrke .sname LIKE ‘B_%B’ ¡ age2 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. R. AS and = are two ways to name fields in result.sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference? ¡ ¡ Database Management Systems 3ed.sid=R.

bid. Key field! Included in the SQL/92 SELECT S. Ramakrishnan and J. subquery must be recomputed for each Sailors tuple. but some WHERE S.sid=R. Ramakrishnan and J. Boats B. Reserves R2 compute the intersection WHERE S.color=‘green’ Database Management Systems 3ed. Boats B. To understand semantics of nested queries. Ramakrishnan and J.sname FROM Sailors S WHERE S. * denotes all attributes. R.       ¡ ¡ ¡ INTERSECT: AND B. R. and * is replaced by R.sid FROM Reserves WHERE Find names of sailors who’ve reserved boat # 103: R R.sid AND R.sid=R. use NOT IN.) To find sailors who’ve not reserved # 103. AND B. Reserves R1.sid IN (SELECT R.bid?) Illustrates why.bid=B2.Find sid’s of sailors who’ve reserved a red and a green boat SELECT S.sid=R.bid=103 AND Find names of sailors who’ve reserved boat # 103: S. 12 . check the qualification by computing the subquery. think of a nested loops evaluation: For each Sailors tuple.sid UNION and INTERSECT FROM Sailors S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R. Reserves R queries with how much WHERE S.sid) EXISTS is another set comparison operator. in general.sid AND R2. Gehrke If UNIQUE is used.sid AND R1.bid=B. Reserves R standard.sid FROM Sailors S. Database Management Systems 3ed.bid the other versions differ.sid FROM Sailors S. Gehrke 11 Nested Queries with Correlation SELECT S.color=‘green’) compatible sets of tuples. Why do we have to replace * by R.   ¡ ¡ Database Management Systems 3ed. Can be used to Boats B2.sid=R2.bid=B1. like IN.bid systems don’t support it.bid=103) A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually.sid=R1.bid AND S.bid of any two unionAND (B1. (UNIQUE checks for duplicate tuples.sid AND R. Gehrke 10 Nested Queries SELECT S. finds sailors with at most one reservation for boat # 103.color=‘red’ AND B2.bid=B. so can FROM and HAVING clauses. Boats B1. R.color=‘red’ Contrast symmetry of the INTERSECT SELECT S.

. Gehrke 14 (1) Division in SQL Find sailors who’ve reserved all boats. Ramakrishnan and J. Reserves R2 WHERE S2. Boats B2.More on Set-Comparison Operators We’ve already seen IN.sname=‘Horatio’) 13 Database Management Systems 3ed.rating FROM Sailors S2 WHERE S2.bid AND B2. =.sid IN (SELECT S2. op IN >.bid FROM Reserves R WHERE R.bid=B.sid=S. Ramakrishnan and J.color=‘red’ AND S. ≠ Find sailors whose rating is greater than that of some sailor called Horatio: ¡ ¡ ¡ ¡ ¡ SELECT * FROM Sailors S WHERE S.sname in SELECT clause. ≥.sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.sid=R2.color=‘green’) Similarly. EXCEPT queries re-written using NOT IN. op ALL.. R. (What about INTERSECT query?) Database Management Systems 3ed.sid FROM Sailors S2. AND R. Let’s do it the hard way. Reserves R S. Gehrke 15 .bid FROM Boats B WHERE NOT EXISTS (SELECT R.sid by S.bid=B.sid)) a Reserves tuple showing S reserved B Database Management Systems 3ed. NOT EXISTS and NOT UNIQUE. without EXCEPT: ¡ SELECT S.sid=R.sid)) (2) SELECT S. Gehrke Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red and a green boat: SELECT S.sid AND R. Ramakrishnan and J.sid FROM Sailors WHERE S. Boats B.. just replace S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.. R.sid AND R2. Can also use NOT IN.<. R. To find names (not sid’s) of Sailors who’ve reserved both red and green boats.rating > ANY (SELECT S2. FROM Reserves R WHERE R. Also available: op ANY.bid=B2.bid Sailors S such that .bid FROM Boats B) EXCEPT (SELECT R.bid there is no boat B without .≤.sid=S.bid AND B. EXISTS and UNIQUE.

Gehrke For i = 1.. 10: 18 .age FROM Sailors S WHERE (SELECT MAX (S2. MAX (S. . we don’t know how many rating levels exist.rating= (SELECT MAX(S2. Ramakrishnan and J.age = (SELECT MAX (S2. Ramakrishnan and WHERE S. we can write 10 queries that look like this (!):     SELECT MIN (S. and is allowed in the SQL/92 standard. we want to apply them to each of several groups of tuples.age) FROM Sailors S2) SELECT S.sname=‘Bob’ Database Management Systems 3ed.sname FROM Sailors S WHERE S.Aggregate Operators Significant extension of relational algebra.) The third query is equivalent to the second query.age) FROM Sailors S SELECT S.sname.age) FROM Sailors S Database Management Systems 3ed. Gehrke Find name and age of the oldest sailor(s) The first query is illegal! (We’ll look into the reason a bit later.sname.sname. Consider: Find the age of the youngest sailor for each rating level. Gehrke 17 GROUP BY and HAVING So far.. Ramakrishnan and J. 2. . SELECT S.age) FROM Sailors S2) = S.rating=10 16 SELECT COUNT (DISTINCT FROM Sailors S WHERE S. Sometimes.age Database Management Systems 3ed. R. S. we’ve applied aggregate operators to all (qualifying) tuples. R.age FROM Sailors S WHERE S. and what the rating values for these levels are! Suppose we know that rating values go from 1 to 10. R.rating = i J.rating=10 S.rating) SELECT AVG ( DISTINCT S. S. but is not supported in some systems.rating) FROM Sailors S2) S. ¡ ¡ ¡ ¡ ¡ COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) SELECT COUNT (*) FROM Sailors S SELECT AVG (S. In general. when we discuss GROUP BY.age) FROM Sailors S WHERE single column SELECT S.age) FROM Sailors S WHERE S.

. MIN (S. Ramakrishnan and J. (Use AS to name it. for each rating with at least 2 such sailors sid sname rating age 22 dustin 7 45.5 Answer relation 10 35. R. `unnecessary’ fields are deleted. Gehrke 20 Find the age of the youngest sailor with age ≥ 18.age)). Ramakrishnan and J.0 Database Management Systems 3ed. R. The group-qualification is then applied to eliminate some groups. 1 33. (A group is a set of tuples that have the same value for all attributes in grouping-list.) 8 55.0 Only S.age are 58 rusty 10 35. Ramakrishnan and J. Expressions in group-qualification must have a single value per group! In effect. tuples that fail qualification are discarded.rating and S.0 31 lubber 8 55.age >= 18 71 zorba 10 16. Gehrke Conceptual Evaluation The cross-product of relation-list is computed. and these attributes must have a single value per group. each answer tuple corresponds to a group.) 19 Database Management Systems 3ed.0 mentioned in the SELECT.0 other attributes `unnecessary’. and the remaining tuples are partitioned into groups by the value of attributes in grouping-list. Gehrke 21 SELECT S.0 HAVING COUNT (*) > 1 29 brutus 1 33.age) FROM Sailors S .5 WHERE S. rating 7 45.g.0 GROUP BY S. Intuitively.rating. Database Management Systems 3ed.0 2nd column of result is 7 35. rating age GROUP BY or HAVING clauses. MIN (S.rating 64 horatio 7 35. (SQL does not exploit primary key semantics here!)       One answer tuple is generated per qualifying group.Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification The target-list contains (i) attribute names (ii) terms with aggregate operations (e.   ¡ ¡ ¡ ¡ The attribute list (i) must be a subset of grouping-list.0 7 35.0 unnamed. an attribute in group-qualification that is not an argument of an aggregate op also appears in grouping-list. R.

color=‘red’ from the WHERE clause and add a HAVING clause with this condition? What if we drop Sailors and the condition involving S.bid.age) FROM Sailors S WHERE S. Gehrke Find those ratings for which the average age is the minimum over all ratings Aggregate operations cannot be nested! WRONG: SELECT S. Gehrke 22 Find the age of the youngest sailor with age > 18.sid=R.age)) FROM Sailors S2) v Correct solution (in SQL/92): SELECT Temp.age = (SELECT MIN (AVG (S2. Reserves R WHERE S. R. What do we get if we remove B.rating. Ramakrishnan and J.rating=S2.age) AS avgage FROM Sailors S GROUP BY S. Temp. COUNT (*) AS scount FROM Sailors S.color=‘red’ Grouping over a join of three relations. for each rating with at least 2 sailors (of any age) SELECT S.rating) Shows HAVING clause can also contain a subquery.rating. find the number of reservations for this boat SELECT B.For each red boat. Ramakrishnan and J.age > 18 GROUP BY S. Ramakrishnan and J.avgage = (SELECT MIN (Temp. MIN (S.rating.avgage) FROM Temp) Database Management Systems 3ed. Boats B. AVG (S. R.avgage FROM (SELECT S.sid? ¡ ¡ Database Management Systems 3ed.bid=B. R.rating FROM Sailors S WHERE S.rating) AS Temp WHERE Temp.bid ¡ R.sid AND GROUP BY B. Gehrke 24 . Compare this with the query where we considered only ratings with 2 sailors over 18! What if HAVING clause is replaced by:   ¡ ¡ ¡ ¡ HAVING COUNT(*) >1 23 Database Management Systems 3ed.bid AND B.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.

. sname has to be a string. bid INTEGER. (e. false and unknown). Is rating>8 true or false when rating is equal to null? What about AND. 26 Database Management Systems 3ed. Constraints can be named. Gehrke General Constraints Useful when more general ICs than keys are involved. Can be used to ensure application semantics (e. Gehrke 25 Integrity Constraints (Review) An IC describes conditions that every legal instance of a relation must satisfy.:     ¡   ¡ ¡ ¡ ¡ ¡ ¡ SQL provides a special value null for such situations.g. WHERE clause eliminates rows that don’t evaluate to true. PRIMARY KEY (sid). or prevent inconsistencies (e.bname FROM Boats B WHERE B. CHECK ( rating >= 1 AND rating <= CREATE TABLE Reserves ( sname CHAR(10)..g..) New operators (in particular.g. Meaning of constructs must be defined carefully. Can use queries to express constraint. sname CHAR(10). E.             Database Management Systems 3ed.g. Ramakrishnan and J.. Ramakrishnan and J. age REAL. R. CREATE TABLE Sailors ( sid INTEGER.g.Null Values Field values in a tuple are sometimes unknown (e. R. day DATE. Domain constraints: Field values must be of right type. primary key constraints. Inserts/deletes/updates that violate IC’s are disallowed. no spouse’s name). Always enforced. outer joins) possible/needed.. The presence of null complicates many issues. a rating has not been assigned) or inapplicable (e. sid is a key). general constraints. rating INTEGER. CONSTRAINT noInterlakeRes CHECK (`Interlake’ <> 10 ) Database Management Systems 3ed. PRIMARY KEY (bid. Ramakrishnan and J. Special operators needed to check if value is/is not null. foreign key constraints.day). R. OR and NOT connectives? We need a 3-valued logic (true. Gehrke ( SELECT B. age must be < 200) Types of IC’s: Domain constraints.bid=bid))) 27 .g.

R. Number of boats sname CHAR(10). R. the CHECK number of Boats ( (SELECT COUNT (S.bid) FROM Boats B) < 100 ) ASSERTION 28 Database Management Systems 3ed. name. rating FROM NewSailors N WHERE N. Ramakrishnan and J. empty.bid) FROM Boats B) < anything! ¡ ¡   100 ) is the CREATE ASSERTION smallClub right solution. Gehrke 30 . sailors is < 100 wrong! age REAL. ( (SELECT COUNT (S. CHECK not associated with either table. plus number of Awkward and rating INTEGER. Ramakrishnan and J.sid) FROM Sailors S) + (SELECT COUNT (B. rating) SELECT sid. age. age.age <= 18 Database Management Systems 3ed. If Sailors is PRIMARY KEY (sid). name. R.Constraints Over Multiple Relations CREATE TABLE Sailors ( sid INTEGER. Gehrke 29 Triggers: Example (SQL:1999) CREATE TRIGGER youngSailorUpdate AFTER INSERT ON SAILORS REFERENCING NEW TABLE NewSailors FOR EACH STATEMENT INSERT INTO YoungSailors(sid. Ramakrishnan and J. Gehrke Triggers Trigger: procedure that starts automatically if specified changes occur to the DBMS Three parts:     ¡ ¡ Event (activates the trigger) Condition (tests whether the triggers should run) Action (what happens if the trigger runs)   Database Management Systems 3ed.sid) FROM Sailors S) tuples can be + (SELECT COUNT (B.

procedural query languages. Gehrke Summary (Contd.Summary SQL was an important factor in the early acceptance of the relational model. users need to be aware of how queries are optimized and evaluated for best results. Ramakrishnan and J.) NULL for unknown field values brings many complications SQL allows specification of rich integrity constraints Triggers respond to changes in the database Database Management Systems 3ed. optimizer should look for most efficient evaluation plan. significantly more expressive power than relational algebra. Even queries that can be expressed in RA can often be expressed more naturally in SQL.   ¡ ¡ ¡ ¡ ¡ ¡ ¡ In practice. Many alternative ways to write a query. in fact. more natural than earlier. Gehrke 32 . Ramakrishnan and J. 31 Database Management Systems 3ed. R. Relationally complete. R.