Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL exercises on movie Database: Compute a report which contain the genres of those movies with their average time and number of movies for each genres

SQL movie Database: Join Exercise-10 with Solution

10. From the following table, write a SQL query to compute the average time and count number of movies for each genre. Return genre title, average time and number of movies for each genre.

Sample table: movie


Sample table: genres


Sample table: movie_genres


Sample Solution:

SELECT gen_title, AVG(mov_time), COUNT(gen_title) 
FROM movie
NATURAL JOIN  movie_genres
NATURAL JOIN  genres
GROUP BY gen_title;

Sample Output:

      gen_title       |         avg          | count
----------------------+----------------------+-------
 Adventure            | 162.5000000000000000 |     2
 Comedy               |  93.0000000000000000 |     1
 Drama                | 134.2500000000000000 |     4
 Horror               | 100.0000000000000000 |     1
 Thriller             | 117.0000000000000000 |     1
 Crime                | 124.0000000000000000 |     2
 Action               | 137.0000000000000000 |     1
 Music                | 118.0000000000000000 |     1
 War                  | 183.0000000000000000 |     1
 Romance              | 122.0000000000000000 |     1
 Animation            | 134.0000000000000000 |     1
 Mystery              | 137.3333333333333333 |     3
(12 rows)

Relational Algebra Expression:

Relational Algebra Expression: Compute a report which contain the genres of those movies with their average time and number of movies for each genres.

Relational Algebra Tree:

Relational Algebra Tree: Compute a report which contain the genres of those movies with their average time and number of movies for each genres.

Practice Online


Movie database model

Query Visualization:

Duration:

Query visualization of Compute a report which contain the genres of those movies with their average time and number of movies for each genres - Duration

Rows:

Query visualization of Compute a report which contain the genres of those movies with their average time and number of movies for each genres - Rows

Cost:

Query visualization of Compute a report which contain the genres of those movies with their average time and number of movies for each genres - Cost

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

Previous: From the following tables, write a SQL query to find the movies released before 1st January 1989. Sort the result-set in descending order by date of release. Return movie title, release year, date of release, duration, and first and last name of the director.
Next: From the following tables, write a SQL query to find movies with the lowest duration. Return movie title, movie year, director first name, last name, actor first name, last name and role.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

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