Sunteți pe pagina 1din 12

Possible Solutions of Tutorial 3 and 4

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;

15202 rows in set


Q2.4 Find the actor who has acted in more movies in 2005 than any other year
in his career.

ANS select P1.PID, P1.Name


from Person as P1 natural join M Cast natural join Movie
where year = 2005
group by P1.PID
having count(MID) >all (select count(MID)
from Person as P2 natural join M Cast natural join Movie
where P1.PID = P2.PID and year <> 2005
group by year);

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.

select P1.PID, Name, M1.MID, year, rating


from Person as P1 natural join M Cast natural join Movie as M1
where (select count(distinct rating), count(rating)
from (select rating
from Person as P2 natural join M Cast
natural join Movie as M2
where P1.PID = P2.PID and (M1.year < M2.year
or (M1.year = M2.year and M1.MID <= M2.MID))
order by year, M2.MID limit 3) as T) = (1, 3);

ERROR in MySQL: Unknown column ‘P1.PID’ in ‘where clause’


Q2.6 Find the actor(s) whose movies have received the same rating maximum
number of times.

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.

select distinct(PID), Name, rating, count(MID)


from Person natural join M Cast natural join Movie
group by PID, rating
order by count(MID) desc limit 1;

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;

158 rows in set


Q2.8 List the names of all the actors who played in the movie ‘Anand’ (1971).
ANS select PID, Name
from Person natural join M Cast natural join Movie
where title = ‘Anand’ and year = ‘1971’;

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.

ANS select year, count(distinct MID) as total movie count,


((select count(distinct MID)
from M Cast natural join Movie as M3
where M1.year = M3.year)
− (select count(distinct MID)
from Person natural join M Cast natural join Movie as M2
where M1.year = M2.year and gender = ‘male’))
* 100 / count(distinct MID) as female movie percentage
from Movie as M1
group by M1.year
order by year desc;

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;

Q2.14 A decade is a sequence of 10 consecutive years. For example say in your


database you have movie information starting from 1965. Then the first
decade is 1965, 1966, ..., 1974; the second one is 1975, 1968, ..., 1984 and
so on. Find the decade D with the largest number of films and the total
number of films in D.
ANS select (floor((year - (select min(year) from Movie)) / 10) * 10
+ (select min(year) from Movie)) as start year,
(floor((year - (select min(year) from Movie)) / 10) * 10
+ (select min(year) from Movie) + 9) as end year, count(MID)
from Movie

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

15345 rows in set


Q2.16 Find all the actors who made more movies with Yash Chopra than any
other director.

ANS select P1.PID, P1.Name, count(Movie.MID) as movies with yc


from Person as P1 natural join M Cast natural join Movie
join M Director on (Movie.MID = M Director.MID)
join Person as P2 on (M Director.PID = P2.PID)
where P2.Name = ‘Yash Chopra’
group by P1.PID
having count(Movie.MID) >all (select count(Movie.MID)
from Person as P3 natural join M Cast natural join Movie
join M Director on (Movie.MID = M Director.MID)
join Person as P4 on (M Director.PID = P4.PID)
where P1.PID = P3.PID and P4.Name != ‘Yash Chopra’
group by P4.PID)
order by movies with yc desc;

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

13063 rows in set


Q2.18 List the title of 1970 films by order of cast list size.
ANS select title, count(PID) as cast size
from M Cast natural join Movie
where year = 1970
group by MID
order by cast size desc;

8
Q2.19 List the name of actors featured in at least 13 films released after 1990.

ANS select PID, Name, count(MID) as no of movies


from Person natural join M Cast natural join Movie
where year > 1990
group by PID
having no of movies >= 13;

429 rows in set

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.

select A.MID, A.title, B.MID, B.title, A.Name as lead actor


from (select C1.MID, title, PID, Name
from Movie natural join M Cast as C1 natural join Person
where (title like ‘A%’ or title like ‘S%’)
and ID = (select min(ID)
from M Cast as C2
where C1.MID = C2.MID)) as A,
(select C1.MID, title, PID, Name
from Movie natural join M Cast as C1 natural join Person
where (title like ‘A%’ or title like ‘S%’)
and ID = (select min(ID)
from M Cast as C2
where C1.MID = C2.MID)) as B
where A.MID < B.MID and A.PID = B.PID;

944 rows in set


Q2.22 Which were the busiest years for ‘Amitabh Bachhan’ ? Busiest means that
the actor performed in the maximum number of movies.
ANS select year, count(MID)
from Person natural join M Cast natural join Movie
where Name = ‘Amitabh Bachchan’
group by year

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

1807 rows in set


Q2.24 List the actor who has done the maximum number of movies with a (a)
single director and (b) a single production house.
ANS select Person.PID, Name, count(M Cast.MID) as no of movies
from Person natural join M Cast join M Director using (MID)
group by M Director.PID, Person.PID
order by no of movies desc limit 1;

select Person.PID, Name, count(M Cast.MID) as no of movies


from Person natural join M Cast join M Producer using (MID)
group by M Producer.PID, Person.PID
order by no of movies desc limit 1;

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)

select distinct PID, Name


from Person natural join M Cast natural join Movie
where title like ‘Dhoom%’
group by PID
having count(MID) = (select count(MID)
from Movie where title like ‘Dhoom%’) − 1;

12

S-ar putea să vă placă și