Вы находитесь на странице: 1из 15

1.

 Write a query in SQL to list all the information of the actors who played a role
in the movie 'Annie Hall'.

Sample table: actor
Sample table: movie_cast
Sample table: movie
Sample Solution:
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'
));
Copy
Sample Output:
act_id | act_fname | act_lname |
act_gender
--------+----------------------+----------------------
+------------
111 | Woody | Allen | M
(1 row)
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'.

Sample table: director
Sample table: movie_direction
Sample table: movie_cast
Sample table: movie

Sample Solution:
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'))));
Sample Output:
dir_fname | dir_lname
----------------------+----------------------
Stanley | Kubrick
(1 row)

3. Write a query in SQL to list all the movies which released in the country other
than UK.

Sample table: movie
Sample Solution:
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';
Copy
Sample Output:
mov_title | mov_year
| mov_time | date_of_release | releasing_country
----------------------------------------------------
+----------+----------+-----------------+-------------------
The Innocents | 1961
| 100 | 1962-02-19 | SW
Annie Hall | 1977
| 93 | 1977-04-20 | USA
Seven Samurai | 1954
| 207 | 1954-04-26 | JP
(3 rows)

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.

Sample table: movie

Sample table: actor

Sample table: director

Sample table: movie_direction
Sample table: movie_cast

Sample table: reviewer

Sample table: rating

Sample Solution:
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;
Copy
Sample Output:
mov_title |
mov_year | mov_dt_rel | dir_fname |
dir_lname | act_fname | act_lname
----------------------------------------------------
+----------+------------+----------------------
+----------------------+----------------------
+----------------------
Blade Runner |
1982 | 1982-09-09 | Ridley | Scott
| Harrison | Ford
Princess Mononoke |
1997 | 2001-10-19 | Hayao | Miyazaki
| Claire | Danes
(2 rows)
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.

Sample table: movie
Sample table: director
Sample table: movie_direction
Sample Solution:
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'
));

Sample Output:
mov_title
----------------------------------------------------
Annie Hall
(1 row)

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.

Sample table: movie
Sample table: rating
Sample Solution:
SELECT DISTINCT mov_year
FROM movie
WHERE mov_id IN (
SELECT mov_id
FROM rating
WHERE rev_stars>3)
ORDER BY mov_year;
Copy
Sample Output:
mov_year
----------
1958
1961
1962
1977
1982
1986
1995
1997
1999
2001
2004
2008
2009
(13 rows)

7. Write a query in SQL to find the titles of all movies that have no
ratings.

Sample table: movie
Sample table: rating

Sample Solution:
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));
Sample Output:
mov_title
----------------------------------------------------
Deliverance
Amadeus
Spirited Away
The Prestige
The Deer Hunter
Eyes Wide Shut
Back to the Future
The Shawshank Redemption
Seven Samurai

8. Write a query in SQL to find the names of all reviewers who have
ratings with a NULL value.

Sample table: reviewer
Sample table: rating
Sample Solution:
SELECT DISTINCT rev_name
FROM reviewer
WHERE rev_id IN (
SELECT rev_id
FROM rating
WHERE rev_stars IS NULL);

Sample Output:
rev_name
--------------------------------
Neal Wruck
Scott LeBrun
(2 rows)
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.

Sample table : reviewer


Sample table : rating
Sample table : movie
Sample Solution:
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;

Sample Output:
rev_name |
mov_title | rev_stars
--------------------------------
+----------------------------------------------------
+-----------
Brandt Sponseller | Aliens
| 8.40
Flagrant Baronessa | Lawrence of Arabia
| 8.30
Hannah Steele | Donnie Darko
| 8.10
Jack Malvern | The Innocents
| 7.90
Josh Cates | Good Will Hunting
| 4.00
Krug Stillo | Braveheart
| 7.70
Mike Salvati | Annie Hall
| 8.10
Paul Monks | Boogie Nights
| 3.00
Richard Adams | Beyond the Sea
| 6.70
Righty Sock | Titanic
| 7.70
Righty Sock | Vertigo
| 8.40
Sasha Goldshtein | American Beauty
| 7.00
Simon Wright | The Usual Suspects
| 8.60
Victor Woeltjen | Avatar
| 7.30
Vincent Cadena | Slumdog Millionaire
| 8.00
(15 rows)

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.

Sample table: reviewer
Sample table: rating
Sample table: movie
Sample Solution:
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;
Copy
Sample Output:
rev_name |
mov_title
--------------------------------
+----------------------------------------------------
Righty Sock | Titanic
Righty Sock | Vertigo
(2 rows)

11. Write a query in SQL to find the movie title, and the highest number
of stars that movie received and arranged the result according to the
group of a movie and the movie title appear alphabetically in ascending
order.

Sample table: rating

Sample table: movie

Sample Solution:
SELECT mov_title, MAX(rev_stars)

FROM movie, rating

WHERE movie.mov_id=rating.mov_id

AND rating.rev_stars IS NOT NULL

GROUP BY mov_title

ORDER BY mov_title;

Copy
Sample Output:
mov_title | max
----------------------------------------------------
+------
Aliens |
8.40
American Beauty |
7.00
Annie Hall |
8.10
Avatar |
7.30
Beyond the Sea |
6.70
Blade Runner |
8.20
Boogie Nights |
3.00
Braveheart |
7.70
Donnie Darko |
8.10
Good Will Hunting |
4.00
Lawrence of Arabia |
8.30
Princess Mononoke |
8.40
Slumdog Millionaire |
8.00
The Innocents |
7.90
The Usual Suspects |
8.60
Titanic |
7.70
Vertigo |
8.40
13. Write a query in SQL to find the titles of all movies which have been
reviewed by anybody except by Paul Monks.

Sample table: reviewer
Sample table: rating
Sample table: movie
Sample Solution:
SELECT movie.mov_title
FROM movie
WHERE movie.mov_id IN(
SELECT mov_id
FROM rating
WHERE rev_id NOT IN (
SELECT rev_id
FROM reviewer
WHERE rev_name='Paul Monks'));

14. Write a query in SQL to return the reviewer name, movie title, and
number of stars for those movies which rating is the lowest one.

Sample table : reviewer

Sample table: rating

Sample table: movie

Sample Solution:
SELECT reviewer.rev_name, movie.mov_title, rating.rev_stars

FROM reviewer, movie, rating

WHERE rating.rev_stars = (

SELECT MIN(rating.rev_stars)

FROM rating

AND rating.rev_id = reviewer.rev_id


AND rating.mov_id = movie.mov_id;

Copy
Sample Output:
rev_name |
mov_title | rev_stars
--------------------------------
+----------------------------------------------------
+-----------
Paul Monks | Boogie Nights
| 3.00
(1 row)

15. Write a query in SQL to find the titles of all movies directed by James
Cameron.

Sample table: director
Sample table: movie_direction
Sample table: movie
Sample Solution:
SELECT mov_title
FROM movie
WHERE mov_id IN (
SELECT mov_id
FROM movie_direction
WHERE dir_id IN (
SELECT dir_id
FROM director
WHERE dir_fname = 'James' AND dir_lname='Cameron'
));

OR
SELECT mov_title FROM movie
JOIN movie_direction
ON movie.mov_id=movie_direction.mov_id
JOIN director
ON movie_direction.dir_id=director.dir_id
WHERE dir_fname = 'James' AND dir_lname='Cameron';
Sample Output:
mov_title
----------------------------------------------------
Titanic
Aliens
(2 rows)

16. Write a query in SQL to find the name of those movies where one or
more actors acted in two or more movies.

Sample table: movie
Sample table: movie_cast
Sample table: actor
Sample Solution:
SELECT mov_title
FROM movie
WHERE mov_id IN (
SELECT mov_id
FROM movie_cast
WHERE act_id IN (
SELECT act_id
FROM actor
WHERE act_id IN (
SELECT act_id
FROM movie_cast GROUP BY act_id
HAVING COUNT(act_id)>1)));

Sample Output:
mov_title
----------------------------------------------------
Beyond the Sea
American Beauty
(2 rows)

Вам также может понравиться