w3resource

SQL Exercises, Practice, Solution - SUBQUERIES exercises on movie Database

SQL [16 exercises with solution]

You may read our SQL Subqueries tutorial before solving the following exercises.

[An editor is available at the bottom of the page to write and execute the scripts.]

Sample Database:

Movie database model

1. From the following table, write a SQL query to find the actors who played a role in the movie 'Annie Hall'. Return all the fields of actor table.  Go to the editor

Sample table: actor


Sample table: movie_cast


Sample table: movie


Sample Output:

 act_id |      act_fname       |      act_lname       | act_gender
--------+----------------------+----------------------+------------
    111 | Woody                | Allen                | M
(1 row)

Click me to see the solution

2. From the following tables, write a SQL query to find the director of a film that cast a role in 'Eyes Wide Shut'. Return director first name, last name.  Go to the editor

Sample table: director


Sample table: movie_direction


Sample table: movie_cast


Sample table: movie


Sample Output:

      dir_fname       |      dir_lname
----------------------+----------------------
 Stanley              | Kubrick
(1 row)

Click me to see the solution

3. From the following table, write a SQL query to find those movies that have been released in countries other than the United Kingdom. Return movie title, movie year, movie time, and date of release, releasing country.  Go to the editor

Sample table: movie


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)

Click me to see the solution

4. From the following tables, write a SQL query to find for movies whose reviewer is unknown. Return movie title, year, release date, director first name, last name, actor first name, last name.  Go to the editor

Sample table: movie


Sample table: actor


Sample table: director


Sample table: movie_direction


Sample table: movie_cast


Sample table: reviewer


Sample table: rating


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)

Click me to see the solution

5. From the following tables, write a SQL query to find those movies directed by the director whose first name is Woddy and last name is Allen. Return movie title.  Go to the editor

Sample table: movie


Sample table: director


Sample table: movie_direction


Sample Output:

                     mov_title
----------------------------------------------------
 Annie Hall
(1 row)

Click me to see the solution

6. From the following tables, write a SQL query to determine those years in which there was at least one movie that received a rating of at least three stars. Sort the result-set in ascending order by movie year. Return movie year.  Go to the editor

Sample table: movie


Sample table: rating


Sample Output:

 mov_year
----------
     1958
     1961
     1962
     1977
.....

Click me to see the solution

7. From the following table, write a SQL query to search for movies that do not have any ratings. Return movie title.  Go to the editor

Sample table: movie


Sample table: rating


Sample Output:

                     mov_title
----------------------------------------------------
 Deliverance
 Amadeus
 Spirited Away
 The Prestige
 .....
 

Click me to see the solution

8. From the following table, write a SQL query to find those reviewers who have not given a rating to certain films. Return reviewer name.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample Output:

            rev_name
--------------------------------
 Neal Wruck
 Scott LeBrun
(2 rows)

Click me to see the solution

9. From the following tables, write a SQL query to find movies that have been reviewed by a reviewer and received a rating. Sort the result-set in ascending order by reviewer name, movie title, review Stars. Return reviewer name, movie title, review Stars.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample table: movie


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
 .....
 

Click me to see the solution

10. From the following table, write a SQL query to find movies that have been reviewed by a reviewer and received a rating. Group the result set on reviewer’s name, movie title. Return reviewer’s name, movie title.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample table : movie


Sample Output:

            rev_name            |                     mov_title
--------------------------------+----------------------------------------------------
 Righty Sock                    | Titanic
 Righty Sock                    | Vertigo
(2 rows)

Click me to see the solution

11. From the following tables, write a SQL query to find those movies, which have received highest number of stars. Group the result set on movie title and sorts the result-set in ascending order by movie title. Return movie title and maximum number of review stars.  Go to the editor

Sample table: rating


Sample table: movie


Sample Output:

                     mov_title                      | max
----------------------------------------------------+------
 Aliens                                             | 8.40
 American Beauty                                    | 7.00
 Annie Hall                                         | 8.10
 Avatar                                             | 7.30
 .....
 

Click me to see the solution

12. From the following tables, write a SQL query to find all reviewers who rated the movie 'American Beauty'. Return reviewer name.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample table: movie


Sample Output:

            rev_name
--------------------------------
 Sasha Goldshtein
(1 row)

Click me to see the solution

13. From the following table, write a SQL query to find the movies that have not been reviewed by any reviewer body other than 'Paul Monks'. Return movie title.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample table: movie


Sample Output:

                     mov_title
----------------------------------------------------
 Avatar
 Lawrence of Arabia
 Donnie Darko
 Aliens
 .....
 

Click me to see the solution

14. From the following table, write a SQL query to find the movies with the lowest ratings. Return reviewer name, movie title, and number of stars for those movies.  Go to the editor

Sample table: reviewer


Sample table: rating


Sample table: movie


Sample Output:

            rev_name            |                     mov_title                      | rev_stars
--------------------------------+----------------------------------------------------+-----------
 Paul Monks                     | Boogie Nights                                      |      3.00
(1 row)

Click me to see the solution

15. From the following tables, write a SQL query to find the movies directed by 'James Cameron'. Return movie title.  Go to the editor

Sample table: director


Sample table: movie_direction


Sample table: movie


Sample Output:

                     mov_title
----------------------------------------------------
 Titanic
 Aliens
(2 rows)

Click me to see the solution

16. Write a query in SQL to find the movies in which one or more actors appeared in more than one film.  Go to the editor

Sample table: movie


Sample table: movie_cast


Sample table: actor


Click me to see the solution

Keep Learning: SQL Subqueries, SQL Single Row Subqueries, SQL Multiple Row and Column Subqueries, SQL Correlated Subqueries, SQL Nested subqueries.

Practice Online


More to Come !

Query visualizations are generated using Postgres Explain Visualizer (pev)

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Share this Tutorial / Exercise on : Facebook and Twitter

SQL: Tips of the Day

Count the occurrences of DISTINCT values?

example db
id         name
-----      ------
1          Mark
2          Mike
3          Paul
4          Mike
5          Mike
6          John
7          Mark
SELECT name,COUNT(*) as count 
FROM tablename 
GROUP BY name 
ORDER BY count DESC;

expected result

name       count
-----      -----
Mike       3
Mark       2
Paul       1
John       1

Ref : https://bit.ly/3EXu62o