table: actor
act_id | act_fname | act_lname | act_gender
--------+----------------------+----------------------+------------
101 | James | Stewart |M
102 | Deborah | Kerr |F
103 | Peter | OToole |M
104 | Robert | De Niro |M
105 | F. Murray | Abraham |M
106 | Harrison | Ford |M
107 | Nicole | Kidman |F
108 | Stephen | Baldwin |M
109 | Jack | Nicholson |M
110 | Mark | Wahlberg |M
table: movie_cast
act_id | mov_id | role
--------+--------+--------------------------------
101 | 901 | John Scottie Ferguson
102 | 902 | Miss Giddens
103 | 903 | T.E. Lawrence
104 | 904 | Michael
105 | 905 | Antonio Salieri
106 | 906 | Rick Deckard
table: movie
mov_id mov_title mov_year mov_time mov_lang mov_dt_rel mov_rel_country
901 Vertigo 1958 128 English 8/24/1958 UK
902 The Innocents 1961 100 English 2/19/1962 SW
903 Lawrence of Arabia 1962 216 English 12/11/1962 UK
904 The Deer Hunter 1978 183 English 3/8/1979 UK
905 Amadeus 1984 160 English 1/7/1985 UK
906 Blade Runner 1982 117 English 9/9/1982 UK
907 Eyes Wide Shut 1999 159 English UK
908 The Usual Suspects 1995 106 English 8/25/1995 UK
table: director
dir_id | dir_fname | dir_lname
--------+----------------------+----------------------
201 | Alfred | Hitchcock
202 | Jack | Clayton
203 | David | Lean
204 | Michael | Cimino
205 | Milos | Forman
206 | Ridley | Scott
207 | Stanley | Kubrick
table: movie_direction
dir_id | mov_id
--------+--------
201 | 901
202 | 902
203 | 903
204 | 904
205 | 905
206 | 906
207 | 907
208 | 908
Sample table: movie_cast
act_id | mov_id | role
--------+--------+--------------------------------
101 | 901 | John Scottie Ferguson
102 | 902 | Miss Giddens
103 | 903 | T.E. Lawrence
104 | 904 | Michael
105 | 905 | Antonio Salieri
106 | 906 | Rick Deckard
107 | 907 | Alice Harford
108 | 908 | McManus
Sample table: reviewer
rev_id | rev_name
--------+--------------------------------
9001 | Righty Sock
9002 | Jack Malvern
9003 | Flagrant Baronessa
9004 | Alec Shaw
9005 |
9006 | Victor Woeltjen
9007 | Simon Wright
9008 | Neal Wruck
9009 | Paul Monks
9010 | Mike Salvati
Tasks:
1 .Write a query in SQL to list all the information of the actors who played a role in
the movie 'Annie Hall'.
Ans:
SELECT *
FROM actor
WHERE act_id IN(
SELECT act_id
FROM movie_cast
WHERE mov_id IN (
SELECT mov_id
FROM movie
WHERE mov_title='Annie Hall'
));
2. Write a query in SQL to find the name of the director (first and last names) who
directed a movie that casted a role for 'Eyes Wide Shut'.
SELECT dir_fname, dir_lname
FROM director
WHERE dir_id in (
SELECT dir_id
FROM movie_direction
WHERE mov_id in(
SELECT mov_id
FROM movie_cast WHERE role = ANY (
SELECT role
FROM movie_cast
WHERE mov_id IN (
SELECT mov_id
FROM movie
WHERE mov_title='Eyes Wide Shut'))));
3. Write a query in SQL to list all the movies which released in the country other
than UK.
SELECT mov_title, mov_year, mov_time,
mov_dt_rel AS Date_of_Release,
mov_rel_country AS Releasing_Country
FROM movie
WHERE mov_rel_country<>'UK';
4. Write a query in SQL to find the movie title, year, date of release, director and
actor for those movies which reviewer is unknown.
SELECT mov_title, mov_year, mov_dt_rel, dir_fname, dir_lname,
act_fname, act_lname
FROM movie a, movie_direction b, director c,
rating d, reviewer e, actor f, movie_cast g
WHERE a.mov_id=b.mov_id
AND b.dir_id=c.dir_id
AND a.mov_id=d.mov_id
AND d.rev_id=e.rev_id
AND a.mov_id=g.mov_id
AND g.act_id=f.act_id
AND e.rev_name IS NULL;
5. Write a query in SQL to find the titles of all movies directed by the director whose
first and last name are Woddy Allen.
SELECT mov_title
FROM movie
WHERE mov_id=(
SELECT mov_id
FROM movie_direction
WHERE dir_id=(
SELECT dir_id
FROM director
WHERE dir_fname='Woody' AND dir_lname='Allen'
));
6-Write a query in SQL to find all the years which produced at least one movie and
that received a rating of more than 3 stars. Show the results in increasing order.
SELECT DISTINCT mov_year
FROM movie
WHERE mov_id IN (
SELECT mov_id
FROM rating
WHERE rev_stars>3)
ORDER BY mov_year;
7-Write a query in SQL to find the titles of all movies that have no ratings.
SELECT DISTINCT mov_title
FROM movie
WHERE mov_id IN (
SELECT mov_id
FROM movie
WHERE mov_id NOT IN (
SELECT mov_id FROM Rating));
8. Write a query in SQL to find the names of all reviewers who have ratings with a
NULL value.
SELECT DISTINCT rev_name
FROM reviewer
WHERE rev_id IN (
SELECT rev_id
FROM rating
WHERE rev_stars IS NULL);
9. Write a query in SQL to return the reviewer name, movie title, and stars for those
movies which reviewed by a reviewer and must be rated. Sort the result by reviewer
name, movie title, and number of stars.
SELECT rev_name, mov_title, rev_stars
FROM reviewer, rating, movie
WHERE reviewer.rev_id=rating.rev_id
AND movie.mov_id=rating.mov_id
AND reviewer.rev_name IS NOT NULL
AND rating.rev_stars IS NOT NULL
ORDER BY rev_name, mov_title, rev_stars;
10. Write a query in SQL to find the reviewer's name and the title of the movie for
those reviewers who rated more than one movies.
SELECT rev_name, mov_title
FROM reviewer, movie, rating, rating r2
WHERE rating.mov_id=movie.mov_id
AND reviewer.rev_id=rating.rev_ID
AND rating.rev_id = r2.rev_id
GROUP BY rev_name, mov_title HAVING count(*) > 1;