









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Union, Intersection, Di erence
\Relation UNION relation" pro duces the union of the two relations.
Similarly for INTERSECT, EXCEPT = intersection and set di erence. F But: in Oracle set di erence is MINUS, not EXCEPT.
Example
Find the drinkers and b eers such that the drinker likes the b eer and frequents a bar that serves it.
Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker , bar)
Likes INTERSECT (SELECT drinker, beer FROM Sells, Frequents WHERE Frequents.bar = Sells.bar );
Bag Semantics of SQL
An SQL relation is really a bag or multiset.
It may contain the same tuple more than once, although there is no sp eci ed order (unlike a list).
Example: f 1 ; 2 ; 1 ; 3 g is a bag and not a set.
Bag Union
Sum the times an element app ears in the two bags.
Example: f 1 ; 2 ; 1 g [ f 1 ; 2 ; 3 g = f 1 ; 1 ; 1 ; 2 ; 2 ; 3 g.
Bag Intersection
Take the minimum of the numb er of o ccurrences in each bag.
Example: f 1 ; 2 ; 1 g \ f 1 ; 2 ; 3 ; 3 g = f 1 ; 2 g.
Bag Di erence
Prop er-subtract the numb er of o ccurrences in the two bags.
Example: f 1 ; 2 ; 1 g f 1 ; 2 ; 3 ; 3 g = f 1 g.
Forcing Set/Bag Semantics
Default for select-from-where is bag; default for union, intersection, and di erence is set. F Why? Saves time of not comparing tuples as we generate them. F But we need to sort anyway when we take intersection or di erence. (Union seems to b e thrown in for go o d measure!)
Force set semantics with DISTINCT after SELECT. F But make sure the extra time is worth it.
Example
Find the di erent prices charged for b eers.
Sells(bar , beer , price)
SELECT DISTINCT price FROM Sells;
Force bag semantics with ALL after UNION, etc.
Aggregations
Sum, avg, min, max, and count apply to attributes/columns. Also, count(*) applies to tuples.
Use these in lists following SELECT.
Example
Find the average price of Bud.
Sells(bar , beer , price)
SELECT AVG(price) FROM Sells WHERE beer = 'Bud';
Counts each tuple (presumably each bar that sells Bud) once.
Class Problem
What would we do if Sells were a bag?
Grouping
Follow select-from-where by GROUP BY and a list of attributes.
The relation that is the result of the FROM and WHERE clauses is group ed according to the values of these attributes, and aggregations take place only within a group.
Example
Find the average sales price for each b eer.
Sells(bar , beer , price)
SELECT beer, AVG(price) FROM Sells GROUP BY beer;
Example
Find, for each drinker, the average price of Bud at the bars they frequent.
Sells(bar , beer , price) Frequents(drinker , bar)
SELECT drinker, AVG(price) FROM Frequents, Sells WHERE beer = 'Bud' AND Frequents.bar = Sells.bar GROUP BY drinker;
Note: grouping o ccurs after the and op erations.
HAVING Clauses
HAVING clauses are selections on groups, just as WHERE clauses are selections on tuples.
Condition can use the tuple variables or relations in the FROM and their attributes, just like the WHERE can. F But the t.v.'s range only over the group. F And the attribute b etter make sense within a group; i.e., b e one of the grouping attributes.
Example
Find the average price of those b eers that are either served in at least 3 bars or manufactured by Anheuser-Busch.
Beers(name , manf) Sells(bar , beer , price)
SELECT beer, AVG(price) FROM Sells GROUP BY beer HAVING COUNT(*) >= 3 OR beer IN ( SELECT name FROM Beers WHERE manf = 'Anheuser-Busch' );
Insertion of the Result of a Query
INSERT INTO relation (sub query).
Example
Create a (unary) table of all Sally's p otential buddies, i.e., the p eople who frequent bars that Sally also frequents.
Frequents(drinker , bar)
CREATE TABLE PotBuddies( name char(30) ); INSERT INTO PotBuddies (SELECT DISTINCT d2.drinker FROM Frequents d1, Frequents d WHERE d1.drinker = 'Sally' AND d2.drinker <> 'Sally' AND d1.bar = d2.bar );
Deletion
DELETE FROM relation WHERE condition.
Deletes all tuples satisfying the condition from the named relation.
Example
Sally no longer likes Bud.
Likes(drinker , beer )
DELETE FROM Likes WHERE drinker = 'Sally' AND beer = 'Bud';
Example
Make the Likes relation empty.
DELETE FROM Likes;
Semantics is tricky. If A.B. makes Bud and BudLite (only), do es deletion of Bud make BudLite not satisfy the condition?
SQL2 semantics: all conditions in mo di cations must b e evaluated by the system b efore any mo ds due to that mo d command o ccur. F In Bud/Budlite example, we would rst identify b oth b eers as targets, and then delete b oth.
Up dates
UPDATE relation SET list of assignments WHERE condition.
Example
Drinker Fred's phone numb er is 555-1212.
Drinkers(name , addr, phone)
UPDATE Drinkers SET phone = '555-1212' WHERE name = 'Fred';
Example
Make $4 the maximum price for b eer.
Up dates many tuples at once.
Sells(bar , beer , price)
UPDATE Sells SET price = 4. WHERE price > 4.00;