+ All Categories
Transcript
Page 1: Advanced SQL Queries

1

Advanced SQL QueriesAdvanced SQL Queries

Page 2: Advanced SQL Queries

2

Example Tables UsedExample Tables Used

Reserves

sid bid day

22

58

101

103

10/10/04

11/12/04

Sailors

sid sname rating age

22

31

58

Dustin

Lubber

Rusty

7

8

10

45.0

55.5

35.0

Boats

bid bname color

101

103

Nancy

Gloria

red

green

Page 3: Advanced SQL Queries

3

Rewriting Rewriting MinusMinus Queries Queries

SELECT S.sname, S.sid

FROM Sailors S, Boats B, Reserves R

WHERE S.sid = R.sid and R.bid = B.bid

and B.color = ‘red’

MINUS

SELECT S.sname, S.sid

FROM Sailors S, Boats B, Reserves R

WHERE S.sid = R.sid and R.bid = B.bid

and B.color = ‘green’;

Name and id of sailors that has reserved at least one red boat and has never reserved green boat

Page 4: Advanced SQL Queries

4

Rewriting Rewriting MinusMinus Queries Queries Using Using Not InNot In

SELECT S.sname, 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 NOT IN (

SELECT R.sid

FROM Boats B, Reserves R

WHERE R.bid = B.bid and B.color = ‘green’);

Page 5: Advanced SQL Queries

5

Rewriting Rewriting MinusMinus Queries Queries Using Using Not ExistsNot Exists

SELECT S.sname, S.sid

FROM Sailors S, Boats B, Reserves R

WHERE S.sid = R.sid and R.bid = B.bid

and B.color = ‘red’

and NOT EXISTS (

SELECT *

FROM Boats B, Reserves R

WHERE R.sid = S.sid and R.bid = B.bid and B.color = ‘green’);

Page 6: Advanced SQL Queries

6

DivisionDivision

• Consider: A(X,Y) and B(Y).

Then AB =

• In general, we require that the set of fields in B be contained in those of A.

A}, B)y( |x{ yx

Page 7: Advanced SQL Queries

7

Suppliers from A who supply Suppliers from A who supply All Parts from B (1)All Parts from B (1)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

P2

pno

B1

sno

=

Page 8: Advanced SQL Queries

8

Suppliers from A who supply Suppliers from A who supply All Parts from B (1)All Parts from B (1)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

P2

pno

B1

sno

=S1

S2

S3

S4

Page 9: Advanced SQL Queries

9

Suppliers from A who supply Suppliers from A who supply All Parts from B (2)All Parts from B (2)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

sno

=P2

P4

pno

B2

Page 10: Advanced SQL Queries

10

Suppliers from A who supply Suppliers from A who supply All Parts from B (2)All Parts from B (2)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

sno

=P2

P4

pno

B2

S1

S4

Page 11: Advanced SQL Queries

11

Suppliers from A who supply Suppliers from A who supply All Parts from B (3)All Parts from B (3)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

sno

=P1

P2

P4

pno

B3

Page 12: Advanced SQL Queries

12

Suppliers from A who supply Suppliers from A who supply All Parts from B (3)All Parts from B (3)

sno pno

S1

S1

S1

S1

S2

S2

S3

S4

S4

P1

P2

P3

P4

P1

P2

P2

P2

P4A

sno

=P1

P2

P4

pno

B3

S1

Page 13: Advanced SQL Queries

13

Sailors who Reserved all Sailors who Reserved all BoatsBoats

(sid,bid

Reserves) (bid

Boats)

Sailor S whose "set of boats

reserved" contains the "set of

all boats"

Page 14: Advanced SQL Queries

14

Division in SQL (1)Division in SQL (1)

SELECT sid

FROM Sailors S

WHERE NOT EXISTS

(SELECT B.bid

FROM Boats B

WHERE B.bid NOT IN

(SELECT R.bid

FROM Reserves R

WHERE R.sid = S.sid));

Sailor S for which there does not exist a boat B in Boats that he did not reserve

Page 15: Advanced SQL Queries

15

Division in SQL (2)Division in SQL (2)

SELECT S.sid

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))

Sailor S for which there does not exist a boat B in Boats that he did not reserve

Page 16: Advanced SQL Queries

16

Division in SQL (3)Division in SQL (3)

SELECT S.sid

FROM Sailors S

WHERE NOT EXISTS((SELECT B.bid

FROM Boats B)

MINUS

(SELECT R.bid

FROM Reserves R

WHERE R.sid = S.sid));

Sailor S for which there does not exist

a boat B in Boats for which there is no

reservation in Reserves

Page 17: Advanced SQL Queries

17

AggregationAggregation

Page 18: Advanced SQL Queries

18

Aggregate OperatorsAggregate Operators

• The aggregate operators available in SQL are:

– COUNT(*)

– COUNT([DISTINCT] A)

– SUM([DISTINCT] A)

– AVG([DISTINCT] A)

– MAX(A)

– MIN(A)

• NULL values are ignored

Page 19: Advanced SQL Queries

19

Some ExamplesSome Examples

SELECT COUNT(*)

FROM Sailors S

SELECT AVG(S.age)

FROM Sailors S

WHERE S.rating=10

SELECT COUNT(distinct color)

FROM Boats

SELECT COUNT(sid)

FROM Sailors S

Page 20: Advanced SQL Queries

20

Find Average Age for each Find Average Age for each RatingRating

• So far, aggregation has been

applied to all tuples that

passed the WHERE clause test.

• How can we apply aggregation

to groups of tuples?

Page 21: Advanced SQL Queries

21

Basic SQL QueryBasic 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 and aggregation operators

Page 22: Advanced SQL Queries

22

EvaluationEvaluation

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

Page 23: Advanced SQL Queries

23

Find Average Age for each Find Average Age for each RatingRating

SELECT AVG(age)

FROM Sailors

GROUP BY rating;

Page 24: Advanced SQL Queries

24

Sailors

sid snameratin

gage

22

31

58

63

78

84

Dustin

Lubber

Rusty

Fluffy

Morley

Popey

e

7

8

10

7

7

10

45.0

55.5

35.0

44.0

31.0

33.0

Sailors

sid snameratin

gage

22

63

78

31

58

84

Dustin

Fluffy

Morley

Lubber

Rusty

Popey

e

7

7

7

8

10

10

45.0

44.0

31.0

55.5

35.0

33.0

40 55.5 34

Page 25: Advanced SQL Queries

25

Find name and age of Find name and age of oldest Sailoroldest Sailor

SELECT S.sname, MAX(S.age)

FROM Sailors S

Wrong!

SELECT S.sname, MAX(S.age)

FROM Sailors S

GROUP BY S.sname

Wrong: we don’t obtain what we want

Page 26: Advanced SQL Queries

26

Find name and age of Find name and age of oldest Sailoroldest Sailor

SELECT S.sname, S.age

FROM Sailors S

WHERE S.age >= ALL

(SELECT S2.age

FROM Sailors S2)

SELECT S.sname, S.age

FROM Sailors S

WHERE S.age =

(SELECT MAX(S2.age)

FROM Sailors S2)

Right!!

How else can this be done?

Hint: >= ALL

Page 27: Advanced SQL Queries

27

What does this return?What does this return?

SELECT B.bid, COUNT(*)

FROM Boats B, Reserves R

WHERE R.bid=B.bid and B.color=‘red’

GROUP BY B.bid

Tuples: (id of reserved red boat,

number of reservations of the red boat)

What would happen if we put the condition about the color in the HAVING clause?

Page 28: Advanced SQL Queries

28

What would happen if we What would happen if we put the condition about put the condition about the color in the HAVING the color in the HAVING

clause?clause?

SELECT B.bid, COUNT(*)

FROM Boats B, Reserves R

WHERE R.bid=B.bid

GROUP BY B.bid, B.color

HAVING B.color=‘red’

We have also to put the color in the grouping list!

Page 29: Advanced SQL Queries

29

Names of Boats that were Names of Boats that were not Reserved on more than not Reserved on more than

5 days5 days

Can we move the condition in the HAVING to the WHERE?

SELECT bname

FROM Boats B, Reserves R

WHERE R.bid=B.bid

GROUP BY bid, bname

HAVING count(DISTINCT day) <= 5

Aggregate functions are not allowed in WHERE

Page 30: Advanced SQL Queries

30

The Color for which there The Color for which there are the most boatsare the most boats

SELECT color

FROM Boats B

GROUP BY color

HAVING max(count(bid))

What is wrong with this?

How would you fix it?

Page 31: Advanced SQL Queries

31

The Color for which there The Color for which there are the most boatsare the most boats

SELECT color

FROM Boats B

GROUP BY color

HAVING count(bid) >= ALL

(SELECT count(bid)

FROM Boats

GROUP BY Color)

Page 32: Advanced SQL Queries

32

Aggregation Instead of Aggregation Instead of ExistsExists

• Aggregation can take the place of

exists.

• Example:SELECT color

FROM Boats B1

WHERE NOT EXISTS(

SELECT *

FROM Boats B2

WHERE B1.bid <> B2.bid

AND B1.color=B2.color)

The color of the boat that there is no other boat of this color

Page 33: Advanced SQL Queries

33

Aggregation Instead of Aggregation Instead of ExistsExists

SELECT color

FROM Boats B1

GROUP BY color

HAVING count(bid) = 1

Page 34: Advanced SQL Queries

34

Sub-queries and ViewsSub-queries and Views

Page 35: Advanced SQL Queries

35

A Complex QueryA Complex Query

• We would like to create a table

containing 3 columns:

– Sailor id

– Sailor age

– Age of the oldest Sailor

How can this be done?

Page 36: Advanced SQL Queries

36

Attempt 1Attempt 1

SELECT S.sid, S.age, MAX(S.age)

FROM Sailors S;

Why is this wrong?

Page 37: Advanced SQL Queries

37

Attempt 2Attempt 2

SELECT S.sid, S.age, MAX(S.age)

FROM Sailors S

GROUP BY S.sid, S.age;

Why is this wrong?

Each group contains only one tuple

Page 38: Advanced SQL Queries

38

Solution 1:Solution 1:Sub-query in FROMSub-query in FROM

SELECT S.sid, S.age, M.mxage

FROM Sailors S,(SELECT MAX(S2.age) as mxage

FROM Sailors S2) M;

•We can put a query in the FROM clause instead of a table

•The sub-query in the FROM clause must be renamed with a range variable (M in this case).

Page 39: Advanced SQL Queries

39

Solution 2:Solution 2:Sub-query in SELECTSub-query in SELECT

SELECT S.sid, S.age, (SELECT MAX(S2.age)

FROM Sailors S2)

FROM Sailors S;

•A sub-query in the SELECT clause must return at most one value for each row returned by the outer query.

Page 40: Advanced SQL Queries

40

Another Example of a Another Example of a Sub-query in SELECTSub-query in SELECT

SELECT S.sid, S.age, (SELECT MAX(S2.age)

FROM Sailors S2

WHERE S2.age<S.age)

FROM Sailors S;

•What does this query return? For each sailor S, the age of the oldest sailor among the sailors younger than S

•Note the use of S (defined in the outer query) in the sub-query.

Page 41: Advanced SQL Queries

41

Another Example of a Another Example of a Sub-query in FROM??Sub-query in FROM??

SELECT S.sid, S.age, M.mxage

FROM Sailors S, (SELECT MAX(S2.age) as mxage

FROM Sailors S2

WHERE S2.age<S.age) M;

Why is this wrong?

We cannot refer to S.age in a sub-query in FROM

Page 42: Advanced SQL Queries

42

Solution 3: Create a TableSolution 3: Create a Table

CREATE TABLE MaxAge as

SELECT MAX(S.age) as mxage

FROM Sailors S;

SELECT S.sid, S.age, M.mxage

FROM Sailors S, MaxAge M;

MUST Rename

!

Problem: how to update MaxAge table?

Page 43: Advanced SQL Queries

43

ViewsViews

• A view is a "virtual table" defined using

a query

• You can use a view as if it were a table,

even though it doesn't contain data

• The view is computed every time that

it is referenced

Page 44: Advanced SQL Queries

44

Advantages and Advantages and DisadvantagesDisadvantages

• Advantages:

– no memory used for views

– update of table does not require updating views

– gives query processor more choices for

optimizing

• Disadvantages:

– must be recomputed every time used

– if tables that view uses are dropped, view data is

lost

Page 45: Advanced SQL Queries

45

Solution 4: ViewsSolution 4: Views

• A View is a query that looks like a table and can be used as a table.

CREATE OR REPLACE VIEW MaxAge as

SELECT MAX(S.age) as mxage

FROM Sailors S;

SELECT S.sid, S.age, M.mxage

FROM Sailors S, MaxAge M;

MUST Rename

!

Page 46: Advanced SQL Queries

46

Another Example of ViewsAnother Example of Views

CREATE OR REPLACE VIEW MaxAges AS

SELECT S1.sid, S2.age AS mxage

FROM Sailors S1, Sailors S2

WHERE S2.age = (SELECT MAX(S3.age)

FROM Sailors S3

WHERE S3.age < S1.age);

SELECT S.sid, S.age, M.mxage

FROM Sailors S, MaxAges M

WHERE S.sid = M.sid;

Page 47: Advanced SQL Queries

47

Views For Restricting Views For Restricting AccessAccess

• 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 as

SELECT *

FROM Grades

WHERE Login = User;

The system defines the user name.


Top Related