w3resource
SQL exercises

SQL exercises on movie Database: Find the first and last name of all the directors with number of genres movies they directed with genres name, and arranged the result alphabetically with the first and last name of the director

SQL movie Database: Join Exercise-6 with Solution

6. Write a query in SQL to list first and last name of all the directors with number of genres movies they directed with genres name, and arranged the result alphabetically with the first and last name of the director.

Sample table: director


Sample table: movie_direction


Sample table: genres


Sample table: movie_genres


Sample Solution:

SELECT dir_fname,dir_lname, gen_title,count(gen_title)
FROM director
NATURAL JOIN movie_direction
NATURAL JOIN movie_genres
NATURAL JOIN genres
GROUP BY dir_fname, dir_lname,gen_title
ORDER BY dir_fname,dir_lname;

Sample Output:

      dir_fname       |      dir_lname       |      gen_title       | count
----------------------+----------------------+----------------------+-------
 Alfred               | Hitchcock            | Mystery              |    1
 Bryan                | Singer               | Crime                |    1
 Danny                | Boyle                | Drama                |    2
 David                | Lean                 | Adventure            |    1
 Frank                | Darabont             | Crime                |    1
 Hayao                | Miyazaki             | Animation            |    1
 Jack                 | Clayton              | Horror               |    1
 James                | Cameron              | Action               |    1
 John                 | Boorman              | Adventure            |    1
 Kevin                | Spacey               | Music                |    1
 Michael              | Cimino               | War                  |    1
 Ridley               | Scott                | Thriller             |    1
 Sam                  | Mendes               | Romance              |    1
 Stanley              | Kubrick              | Mystery              |    1
 Woody                | Allen                | Comedy               |    1
(15 rows)

Practice Online


Movie database model

Query Visualization:

Duration:

Query visualization of Find the first and last name of all the directors with number of genres movies they directed with genres name, and arranged the result alphabetically with the first and last name of the director - Duration

Rows:

Query visualization of Find the first and last name of all the directors with number of genres movies they directed with genres name, and arranged the result alphabetically with the first and last name of the director - Rows

Cost:

Query visualization of Find the first and last name of all the directors with number of genres movies they directed with genres name, and arranged the result alphabetically with the first and last name of the director - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query in SQL to list all the actors who acted in a movie before 1990 and also in a movie after 2000.
Next: Write a query in SQL to list all the movies with year and genres.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming