SQL exercises on movie Database: Find the name of the director who directed a movie that casted a role for 'Eyes Wide Shut'
SQL movie Database: Subquery Exercise-2 with 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.
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)
Code Explanation:
The said query in SQL that retrieves the first and last name of the
director(s) who directed a movie in which any actor played
a role that was also played in the movie 'Eyes Wide Shut'.
1. In the innermost subquery, it selects the mov_id of the movie 'Eyes Wide Shut' from the movie table.
Then, it uses that mov_id to select all roles played in
that movie from the movie_cast table.
2. Then that list of roles are use to select all mov_id values from the movie_cast table where the same
roles were played. This effectively finds all movies that have at
least one actor playing the same roles as in 'Eyes Wide Shut'.
3. Then, it selects the dir_id values from the movie_direction table for each of those movies found in the step 2.
4. Then the outermost query selects the first and last names of
the director(s) whose dir_id values were found in the step 3.
Practice Online
Query Visualization:
Duration:

Rows:

Cost:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: 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.
Next: From the following table, write a SQL query to find those movies, which released in the country besides UK. Return movie title, movie year, movie time, date of release, releasing country.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
What is the best way to paginate results in SQL Server?
SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, * FROM Orders WHERE OrderDate >= '1980-01-01' ) AS RowConstrainedResult WHERE RowNum >= 1 AND RowNum < 20 ORDER BY RowNum
Database: SQL Server
Ref: https://bit.ly/3MGrNlk
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook