16.3 Parser to Logical Query Plans
16.3.0 Review Query Algebra Expression Tree Query Algebra in SQL Form (force DBMS follow you) 2
16.3.0 Review Expression Tree π starName ,studioName year=1996 Movie StarsIn Query Algebra π starName ,studioName ( year=1996 (Movie StarsIn ))
Review SELECT MSN.STARNAME, MSN.STUDIONAME From (Select * FROM Movies M, starsIn S) MS Where year =1996) MSN
Review SELECT STARNAME, STUDIONAME From Movies M, starsIn S Where year =1996
(Select * From Movies where year=2009)M, Quiz3/12 Convert the SQL into (1) Query Algebra (2) expression Tree Select MS.studioname, MS.starname From (Select M.title, M.year, M.length, M.inColor, M.studioname, M.producerC#, S.Starname (Select * From Movies where year=2009)M, (Select * From starsIn where Movieyear=2009)S where M.year= S.movieyear and S.movietitle=M.title) MS ;
16.2.3c Pushing Selections starName ,studioName StarsIn π Year=movieyear, Name=starname σ year = 1996 σ movieyear = 1996 Movie StarsIn π starName ,studioName (σ year = 1996(Movie StarsIn )= π starName ,studioName (σ year = 1996(Movie) σ year = 1996(StarsIn ))
16.1. SQL(not RAE) Figure 16.2 select distinct movietitle from starsIn where starname in (select name from moviestar where birthdate like '%1974%'); NOTRAE=Relational Algebra NOT Expressible SQL
16.3. SQL/RAE of Figure 16.19 select distinct movietitle from starsIn a, (select name from moviestar where birthdate like '%1974%') temp where a.starname = temp.name; RAE=Relational Algebra Expressible SQL
16.3Example16.19 Figure 16.24 starname=name σ movieYear lile ‘%1974’ πmovie title starname=name πname StarsIn σ movieYear lile ‘%1974’ MovieStar This is the RAE SQL
16.3. SQL/RAE of Figure 16.19 select movietitle from (select starname,movietitle From starsIn) a, (select name from moviestar where birthdate like '%1974%') b where a.starname = b.name;
16.3Example16.19 Figure 16.24 starname=name σ movieYear lile ‘%1974%’ πmovie title starname=name starame, movie title πname σ movieYear lile ‘%1974%’ StarsIn MovieStar This is the RAE SQL
SQL in Figure 16.20 Select distinct m1.movieTitle, m1.movieYear From StarsIn m1 Where m1.movieYear – 40 <= ( Select AVG(birthdate) From StarsIn m2, Moviestar s Where m2.starName=s.name AND m1.movieTitle = m2.movieTitle AND m1.movieyear = m2.movieyear )P;
SQL in Figure 16.22 Select distinct m.movieTitle, m.movieYear From StarsIn m1, ( Select m2.movieTitle, m2.movieyear, AVG(birthdate) as ave From StarsIn m2, Moviestar s Where m2.starName=s.name Group by m2.movieTitle, m2.movieyear ) m Where m1.movieTitle = m.movieTitle and m1.movieYear – 40 <=ave;
SQL in Figure 16.22 Select distinct movieTitle, movieYear From ( Select m1.movieTitle, m1.movieYear,m1.Starname, m.Ave From StarsIn m1, ( Select m2.movieTitle, m2.movieyear, AVG(birthdate) as ave From StarsIn m2, Moviestar s Where m2.starName=s.name Group by m2.movieTitle, m2.movieyear ) m Where m1.movieTitle = m.movieTitle and m1.movieYear-40 <=m.ave)P;
m2,starname=s.name m1.movieYear -40 abd π m1.movieYear m1.movieYear m1.movieYear -40 abd m2.movietitle=m1.movietitle and m2.movietitle=m1.movietitle γm2.movieTitle, m2.movieyear,AVG(birthDate)ave StarsIn m2,starname=s.name StaesIn MovieStar
m2,starname=s.name m1.movieYear -40 abd π m1.movieYear m1.movieYear m1.movieYear -40 abd m2.movietitle=m1.movietitle and m2.movietitle=m1.movietitle π m1.movieYear,m1.movieYear γm2.movieTitle, m2.movieyear,AVG(birthDate)ave StarsIn m2,starname=s.name StaesIn MovieStar
m2,starname=s.name m1.movieYear -40 abd π m1.movieYear m1.movieYear m1.movieYear -40 abd γm2.movieTitle, m2.movieyear,AVG(birthDate)ave m2,starname=s.name MovieStar StaesIn
m2,starname=s.name m1.movieYear -40 abd π m1.movietitle m1.movieYear m1.movieYear -40 abd γm2.movieTitle, m2.movieyear,AVG(birthDate)ave m2,starname=s.name m1.movietitle m1.movieYear m1.movietitle m1.movieYear StaesIn MovieStar
Lecture on Whiteboard Will be on EXAM2 Select PNAME, Sum (QTY) From Parts natural join Shipments Group by PNAME; Red part has to be written in Select **** from **** where *****
Lecture on Whiteboard pname ,SUM(qty) -->sum (Natural Join) Shipments Parts pname ,SUM(qty) -->sum (Shipment Parts)
General Groupby Select F.PNAME, Sum (F.QTY) as sum From ( Select PNAME, QTY FROM ( SELECT PNUM,PNAME From Parts) natural join ( Select PNUM, QTY From Shipments) incorrect )F Group by F.pname; When two reds are the same THIS SQL is Gamma
General Groupby Select F.PNAME, Sum (F.QTY) as sum From ( Select PNAME, QTY FROM ( Select LL From (SELECT PNUM,PNAME From Parts) A, ( Select PNUM, QTY From Shipments) B where A.PNUM= B.PNUM )F Group by F.pname;
pname ,SUM(qty) -->sum pname. qty (Natural join) π pnum,qty π pnum, pname Shipments Parts