SQL - PDF Solution
SQL - PDF Solution
DBMS: CSN-351
September 5, 2016
The relation schemas to be used for the following queries are as follows.
Movie(MID,title,year,rating,num votes),
Person(PID,Name,DOB,Gender),
Genre(GID,Name),
M Cast(ID,MID,PID),
M Director(ID,MID,PID),
M Producer(ID,MID,PID),
M Genre(ID,MID,GID)
1 Tutorial 3
Q2.1 Return the total number of active actors (who has done a movie after
2005) in the dataset.
ANS select count(distinct PID)
from Person natural join M Cast natural join Movie
where year > 2005;
Q2.2 Return all the actors who have done at least one movie with Rani Mukher-
jee (in alphabetic order of their names).
ANS select distinct(PID), Name
from Person natural join M Cast
where MID in (select MID
from Person natural join M Cast
where Name = ‘Rani Mukerji’)
order by Name;
1
931 rows in set
Q2.3 Rate the actors according to the gross ratings their movies received. A
tie is resolved alphabetically. For example, if Amir Khan worked in five
movies with rating 7, 4, 10, 16 and 9, then his gross rating is 46. Output
the actors based on the decreasing order of ratings.
ANS select PID, Name, sum(rating) as gross rating
from Person natural join M Cast natural join Movie
group by PID
order by gross rating desc, Name;
2
612 rows in set
Q2.5 Find the actors whose three consecutive movies have got the same rating.
ANS Idea of the query is given below. Improve it to get the correct solution.
ANS The following query will work fine for the given instance, but may not
work for other instances where more than one actors are qualified to be
in the output set.
3
2 Tutorial 4
Q2.7 List all the directors who directed a ‘Comedy’ movie in a leap year. Your
query should return director name, the movie name, and the year.
ANS select Person.Name, title, year
from Person natural join M Director natural join Movie
join M Genre using (MID) join Genre using (GID)
where Genre.Name = ‘Comedy’ and year % 4 = 0;
Q2.9 List all the actors who acted in a film before 1970 and also in a film after
1990.
ANS select distinct(PID), Name
from Person natural join M Cast natural join Movie
where year < 1970 and PID in (select distinct(PID)
from Person natural join M Cast natural join Movie
where year > 1990);
4
222 rows in set
Q2.10 List all directors who directed 10 movies or more, in descending order of
the number of movies they directed. Return the directors’ names and the
number of movies each of them directed.
ANS select Name, count(MID)
from Person natural join M Director
group by PID
having count(MID) >= 10
order by count(MID) desc;
36 rows in set
Q2.11 For each year, count the number of movies in that year that had only
female actors.
ANS select year, count(distinct MID) − (select count(distinct MID)
from Person natural join M Cast natural join Movie as M2
where M1.year = M2.year and gender = ’male’) as female movie count
from Movie as M1 natural join M Cast
group by M1.year
order by year desc;
68 rows in set
5
Q2.12 Report for each year the percentage of movies in that year with only female
actors, and also the total number of movies made that year.
68 rows in set
Q2.13 Find the film(s) with the largest cast. Return the movie title and the
size of the cast. By “cast size” we mean the number of distinct actors
that played in that movie: if an actor played multiple roles, or if it simply
occurs multiple times in cast we still count her/him only once.
ANS select title, count(distinct PID) as cast size
from Movie natural join M Cast
group by MID
order by cast size desc limit 1;
6
group by start year
order by count(MID) desc limit 1;
Q2.15 Find the actors who were never unemployed for more than 3 years at a
stretch. (Assume that the actors remain unemployed between two consec-
utive movie releases).
ANS select PID, Name
from Person where PID not in(select distinct(PID)
from M Cast as C1 natural join Movie as M1
where exists(select MID
from M Cast as C2 natural join Movie as M2
where C1.PID = C2.PID and (M2.year - 3) > M1.year
and not exists(select MID
from M Cast as C3 natural join Movie as M3
where C1.PID = C3.PID and M1.year < M3.year
and M3.year < M2.year)));
7
116 rows in set
Q2.17 The Shahrukh number of an actor is the length of the shortest path be-
tween the actor and Shahrukh Khan in the “co-acting” graph. That is,
Shahrukh Khan has Shahrukh number 0; all actors who acted in the same
film as Shahrukh have Shahrukh number 1; all actors who acted in the
same film as some actor with Shahrukh number 1 have Shahrukh number
2, etc. Return all actors whose Shahrukh number is 2.
ANS select distinct PID, Name
from Person natural join M Cast
where Name <> ‘Shah Rukh Khan’ and MID in (select MID
from M Cast where PID in (select PID
from Person natural join M Cast
where Name <> ‘Shah Rukh Khan’ and MID in (select MID
from Person natural join M Cast
where Name = ‘Shah Rukh Khan’)))
and PID not in (select PID
from Person natural join M Cast
where Name <> ‘Shah Rukh Khan’ and MID in (select MID
from Person natural join M Cast
where Name = ‘Shah Rukh Khan’));
8
Q2.19 List the name of actors featured in at least 13 films released after 1990.
Q2.20 List the name pair of actors who have worked together in more than 10
films.
ANS select P1.PID, P1.Name, P2.PID, P2.Name, count(C1.MID) as movies together
from (Person as P1 natural join M Cast as C1) join (Person as P2
natural join M Cast as C2) using (MID)
where P1.PID < P2.PID
group by P1.PID, P2.PID
having movies together > 10;
9
662 rows in set
Q2.21 List the title pair of movies which have the same lead star actor. Restrict
result to movies with names starting with ‘A’ or ‘S’.
ANS For each movie, consider the cast record with lowest ID in M Cast table
as the lead star actor of that movie.
10
order by count(MID) desc limit 1;
Q2.23 List the name of all actors who have worked with ‘Om Puri’.
ANS select distinct PID, Name
from Person natural join M Cast
where Name <> ‘Om Puri’ and MID in (select MID
from Person natural join M Cast
where Name = ‘Om Puri’);
Q2.25 List names of actors who acted in every movie with titles starting with
‘Dhoom’.
ANS select distinct PID, Name
from Person natural join M Cast natural join Movie
11
where title like ‘Dhoom%’
group by PID
having count(MID) = (select count(MID)
from Movie where title like ‘Dhoom%’);
Empty set (0.00 sec)
12