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 Tree:
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 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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework