1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid...
-
date post
21-Dec-2015 -
Category
Documents
-
view
239 -
download
0
Transcript of 1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid...
1
Rewriting Intersect Queries Using In
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
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’;
2
Rewriting Intersect Queries Using In
SELECT S.sid
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ and S.sid IN
(SELECT S2.sid
FROM Sailors S2, Boats B2, Reserves R2
WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘green’);
3
Division
• Consider: A(X,Y) and B(Y).
Then A/B =
• In general, we require that the set of fields in B be contained in those of A.
A}, B)y( |x{ yx
4
Suppliers from A who supply All Parts from B
sno pno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
A
P2
pno
B1
P2
P4
pno
B2
P1
P2
P4
pno
B3
S1
S4
sno
A/B2
S1
sno
A/B3
S1
S2
S3
S4
sno
A/B1
5
Sailors who Reserved all Boats
• To find the Sailors who reserved all boats:
(sid,bid
Reserves)/(bid
Boats)
• Division can be expressed using other relational algebra operators.
6
Division in SQL (1)
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
7
Division in SQL (2)
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));
9
Aggregate Operators
• The aggregate operators available in SQL are:– COUNT(*)– COUNT([DISTINCT] A)– SUM([DISTINCT] A)– AVG([DISTINCT] A)– MAX(A)– MIN(A)
11
Find name and age of oldest Sailor
SELECT S.sname, MAX(S.age)
FROM Sailors S
SELECT S.sname, S.age
FROM Sailors S
WHERE S.age =
(SELECT MAX(S2.age)
FROM Sailors S2)
Wrong!!
Right!!
12
Find Average Age for each Rating
• So far, aggregation has been applied to all tuples that passed the WHERE clause test.
• How can we apply aggregation to groups of tuples?
13
Basic SQL Query
SELECT [Distinct] target-list
FROM relation-list
WHERE condition
GROUP BY grouping-list
HAVING group-condition;
•target-list: Fields appearing in grouping-list and aggregation operators
•group-condition: Can only constrain attributes appearing in grouping-list
14
Evaluation
1. Compute cross product of relations in FROM
2. Tuples failing WHERE are thrown away
3. Tuples are partitioned into groups by values of grouping-list attributes
4. The group-condition is applied to eliminate groups
5. One answer in generated for each group
16
Find the number of Reservations of Each Red
Boat
SELECT B.bid, COUNT(*) as count
FROM Boats B, Reserves R
WHERE R.bid=B.bid and B.color=‘red’
GROUP BY B.bid
What would happen if we put the condition about the color in the HAVING clause?
17
Age of Youngest Sailor that is over 18, for each Rating with
at least 2 such Sailors
SELECT S.rating, MIN(S.age)
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT(*) > 1
18
Age of Youngest Sailor that is over 18, for each Rating with at least 2 Sailors (of any Age)
SELECT S.rating, MIN(S.age)
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING 1 < (SELECT COUNT(*)
FROM Sailors S2
WHERE S.rating =
S2.rating)
20
Sorting Results
• Results can be sorted using the ORDER BY clause
• Here are sailors who ordered boat 103, ordered by their names:
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid = R.sid and R.bid = 103
ORDER BY S.sname
21
Outer Join
• How can we find sailor names with their reservation numbers if we don’t want to lose information about sailors who did not reserve any boats??
SELECT S.sname, R.bid
FROM Sailors S, Reserves R
WHERE S.sid = R.sid(+)
• The (+) must follow column that we allow to be null.
22
Views
• A View is a query that looks like a table and can be used as a table.
CREATE OR REPLACE VIEW SailorsBoats
SELECT S.sname, B.bname, B.color
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid and R.bid = B.bid;
SELECT sname
FROM SailorBoats
WHERE color = ‘red’;
23
Views For Restricting Access
• Suppose that we have a table:Grades(Login, Exercise, Grade)
• We would like a user to only be able to see his own grades. We create the following view and grant privileges to query the view (not the underlying table)
CREATE OR REPLACE VIEW UserGrades
SELECT *
FROM Grades
WHERE Login = User;
Pseudo-column which is equal to the user name.
24
Views for Complex Queries
• Find those ratings for which the average age is the minimum over all ratings
CREATE OR REPLACE VIEW AvgAgeRatings
SELECT S.rating, AVG(S.age) as avgage
FROM Sailors S
WHERE S.rating;
SELECT rating, avgage
FROM AveAgeRatings
WHERE avgage = (SELECT MIN(avgage)
FROM AveAgeRatings)
25
Sub-queries in FROM, instead of Views
SELECT Temp.rating, Temp.avgage
FROM (SELECT S.rating, AVG(S.age) as avgage
FROM Sailors S
GROUP BY S.rating) as Temp
WHERE Temp.avgage = (SELECT MIN(Temp.avgage)
FROM Temp)
27
Deleting Tuples
• The basic form of a delete statement is:
DELETE FROM TableName
WHERE Condition;
28
Examples
DELETE FROM Sailors
WHERE rating < 3;
DELETE FROM Sailors S1
WHERE S1.rating = (SELECT MIN(S2.rating) FROM Sailors S2)
Delete Sailors with rating less than 3:
Delete Sailors with the minimum rating:
29
Updating Tuples
• The basic form of an update statement is:
UPDATE TableName
SET Column1 = Value1, …
ColumnN = ValueN
WHERE Condition;
30
Example
UPDATE Boats
SET color = ‘red’,
bname = ‘Voyager’
WHERE bid = 13;
Update boat 13: color to red and name to voyager