You are on page 1of 9

3.

Consider the schema for Movie Database:

ACTOR (Act_id, Act_Name, Act_Gender)

DIRECTOR (Dir_id, Dir_Name, Dir_Phone)

MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)

MOVIE_CAST (Act_id, Mov_id, Role)

RATING (Mov_id, Rev_Stars)

Write SQL queries to

1. List the titles of all movies directed by‘Hitchcock’.

2. Find the movie names where one or more actors acted in two or moremovies.

3. List all actors who acted in a movie before 2000 and also in a movie after 2015(use JOINoperation).

4. Find the title of movies and number of stars for each movie that has at least one rating and find the
highest number of stars that movie received. Sort the result by movie title.

5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

Solution: Table Creation :

create table ACTOR (

Act_id varchar(3),

Act_name varchar(10),

Act_gender varchar(1),

primary key(Act_id)

);

create table DIRECTOR (

Dir_id varchar(3),

Dir_name varchar(20),

Dir_phone varchar(10),
primary key(Dir_id)

);

create table MOVIES (

Mov_id varchar(3),

Mov_title varchar(20),

Mov_year int,

Mov_lang varchar(10),

Dir_id1 varchar(3),

primary key(Mov_id),

foreign key(Dir_id) references DIRECTOR(Dir_id) on delete CASCADE

);

create table MOVIE_CAST (

Act_id varchar(3),

Mov_id varchar(3),

Role varchar(10),

primary key (Act_id, Mov_id),

foreign key (Act_id) references actor (Act_id) on delete CASCADE,

foreign key (Mov_id) references movies (Mov_id) on delete CASCADE

);

create table RATING (

Mov_id varchar(3),

Rev_stars int,

primary key(Mov_id, Rev_stars),

foreign key(Mov_id) references MOVIES (Mov_id) on delete CASCADE

);
Insertion Of Values To the Tables:

insert into ACTOR values (’a1’,’robert d’,’m’);

insert into ACTOR values (’a2’,’scarlett’,’f’);

insert into ACTOR values (’a3’,’puneeth’,’m’);

insert into ACTOR values (’a4’,’meera’,’f’);

insert into ACTOR values (’a5’,’prabhas’,’m’);

insert into ACTOR values (’a6’,’anushka’,’f’);

insert into DIRECTOR values (’d1’,’hitchcock’, ’7690870681’);

insert into DIRECTOR values (’d2’,’steven spielberg’, ’7986554437’);

insert into DIRECTOR values (’d3’,’mahesh babu’, ’8765675304’);

insert into DIRECTOR values (’d4’,’rajamouli’, ’9651232245’);

insert into MOVIES values (’m1’,’iron Man-1’, 1990, ’english’,’d1’);

insert into MOVIES values (’m2’,’munna’, 1998, ’telugu’,’d3’);

insert into MOVIES values (’m3’,’iron Man-2’, 2001, ’english’,’d2’);

insert into MOVIES values (’m4’,’arasu’, 2007, ’kannada’,’d3’);

insert into MOVIES values (’m5’,’iron Man-3’, 2016, ’english’,’d2’);

insert into MOVIES values (’m6’,’bahubali-2’, 2017, ’telugu’,’d4’);

insert into MOVIE_CAST values (’a1’, ’m1’,’hero’);

insert into MOVIE_CAST values (’a5’, ’m2’, ’hero’);

insert into MOVIE_CAST values (’a1’, ’m3’, ’hero’);

insert into MOVIE_CAST values (’a2’, ’m3’, ’heroine’);

insert into MOVIE_CAST values (’a2’, ’m5’, ’guest’);


insert into MOVIE_CAST values (’a3’, ’m4’, ’hero’);

insert into MOVIE_CAST values (’a4’, ’m4’, ’heroine’);

insert into MOVIE_CAST values (’a1’, ’m5’, ’hero’);

insert into MOVIE_CAST values (’a5’, ’m6’, ’hero’);

insert into MOVIE_CAST values (’a6’, ’m6’, ’heroine’);

insert into RATING values (’m1’,8);

insert into RATING values (’m2’,4);

insert into RATING values (’m3’,6);

insert into RATING values (’m4’,8);

insert into RATING values (’m5’,7);

insert into RATING values (’m6’,9);

insert into RATING values (’m2’,9);

insert into RATING values (’m1’,4);

1. List the titles of all movies directed by ‘Hitchcock’.

select Mov_title

from MOVIES

where Dir_id IN (select Dir_id from DIRECTOR where Dir_name = ’hitchcock’);

2. Find the movie names where one or more actors acted in two or more movies.

Select Mov_title, a.Act_name


from MOVIES m, MOVIE_CAST mc, ACTOR a

where m.Mov_id=mc.Mov_id and mc.Act_id=a.Act_id

and mc.Act_id IN ( select Act_id from MOVIE_CAST group by Act_id

having COUNT (*)>1);

3. List all actors who acted in a movie before 2000 and also in a movie after 2015(use
JOINoperation).

Select Act_name, Mov_title, Mov_year from ACTOR a

JOIN MOVIE_CAST c

ON a.Act_id=c.Act_id

JOIN MOVIES m

ON c.Mov_id=m.Mov_id

Where m.Mov_year NOT BETWEEN 2000 and 2015;


4. Find the title of movies and number of stars for each movie that has at least one rating and find
the highest number of stars that movie received. Sort the result by movie title.

Select Mov_title, MAX(Rev_stars) as Best_Rating

from MOVIES m, RATING r

where m.Mov_id= r.Mov_id and r.Rev_stars IS NOT NULL

group by Mov_title

order by Mov_title;

5.Update rating of all movies directed by ‘Steven Spielberg’ to5.

update RATING set Rev_stars=5

where Mov_id IN (select Mov_id

from MOVIES

where Dir_id IN(select Dir_id

from DIRECTOR

where Dir_name = ’steven spielberg’));

(2 row(s) affected)

Slelect * from rating


Select * from actor Select * from director

Select * from movies select * from movie_cast

Select * from rating

You might also like