word distinct

advertisement
Answer to the question Number a
Q1.
SELECT DISTINCT title, studioName FROM Movie, StarsIn
WHERE starname=’Meryl Streep’ AND
title=movietitle AND
year=movieyear;
Explanation :
The execution plan of the above query is as following :
First, Cross product of Movie and StartsIn will be resulted.
Second, The resultset will be filtered by ‘year=movieyear’ condition after where clause. It means all
the rows will be deleted that fail the condition.
Third, the resultset from second step will be filtered by ‘title=movietitle’ condition. It means all the
rows will be deleted that fail the condition.
Fourth, the resultset from third step will be filtered starname=’Meryl Streep’ condition. It means all
the rows will be deleted that fail the condition.
Fifth, Then dublicate rows with same tilte will be eliminated from the final resulted set because of
using key word DISTINCT and only attributes in select list will be kept.
So it will finally result in the title and studioname of those movie in which ‘Meryl Streep’ was
actor in same year of the movie.
Q2:
Explanation :
SELECT DISTINCT title, studioName FROM Movie, StarsIn,
(SELECT starname
FROM StarsIn HAVING count(*)>10
GROUP BY starname) Productive
WHERE title=movietitle AND
year=movieyear AND
Productive.starname=StarsIn.starname;
The execution plan of the above query is as following :
First : The nested query will return all the starname who was actor in more than ten movies and will
make a virtual table named ‘Productive.
Second, Cross product of Movie, StartsIn, Productive will be resulted.
Third, The resultset will be filtered by ‘year=movieyear’ condition after where clause. It means all
the rows will be deleted that fail the condition.
Fourth, the resultset from second step will be filtered by ‘title=movietitle’ condition. It means all
the rows will be deleted that fail the condition.
Fifth, the resultset from third step will be filtered starname=’Meryl Streep’ condition. It means all
the rows will be deleted that fail the condition.
Sixth, Then dublicate rows with same tilte will be eliminated from the final resulted set because of
using key word DISTINCT and only attributes in select list will be kept.
So it will finally result in the title and studioname of those movie with the actors who participated
more than ten different movies.
Answer to the question Number b
 title, studioname( starname 'Merylstreep ' AND,titlemovietitleAND, year movieyear ( Movie  StarsIn))
Rewriting the nested query
SELECT starname, Count(*) as total_participation from StarsIn
GROUP BY starname
WHERE total_participation>10;
 title,studioname( titlemovietitleAND, year movieyear (movie  ( movietitle,movieyear ( Pr oductive.starname StarsIn.starname( StarsIn  Pr oductive))))
Answer to the question Number c
The sequence of delete statement should be follwing:
1. First deleteing all the actors who participated in all color movies from before 1939.
DELETE FROM StarsIn
WHERE movietitle in (
SELECT title
FROM Movie
WHERE year<1939 AND inColor=1
);
2. Now Deleting all color movies from before 1939
DELETE FROM Movie WHERE year<1939 AND inColor=1;
Download