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: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars

SQL movie Database: Join Exercise-13 with Solution

13. From the following tables, write a SQL query to get the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars.

Sample table: movie


Sample table: rating


Sample table: reviewer


Sample Solution:

SELECT rev_name, mov_title, rev_stars
FROM movie, rating, reviewer
WHERE movie.mov_id = rating.mov_id 
AND reviewer.rev_id = rating.rev_id AND rev_name IS NOT NULL
ORDER BY rev_name, mov_title, rev_stars;

OR

SELECT rev_name, mov_title, rev_stars
FROM movie
INNER JOIN rating ON movie.mov_id = rating.mov_id
INNER JOIN reviewer ON reviewer.rev_id = rating.rev_id
WHERE rev_name IS NOT NULL
ORDER BY rev_name, mov_title, rev_stars;

OR

SELECT rev_name, mov_title, rev_stars
FROM movie
INNER JOIN rating USING(mov_id)
INNER JOIN Reviewer USING(rev_id)
WHERE rev_name IS NOT NULL
ORDER BY rev_name, mov_title, rev_stars;

OR

SELECT rev_name, mov_title, rev_stars
FROM movie 
NATURAL JOIN rating 
NATURAL JOIN reviewer
WHERE rev_name IS NOT NULL
ORDER BY rev_name, mov_title, rev_stars;

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
 Josh Cates                     | Good Will Hunting                                  |      4.00
 Krug Stillo                    | Braveheart                                         |      7.70
 Mike Salvati                   | Annie Hall                                         |      8.10
 Neal Wruck                     | Chinatown                                          |
 Paul Monks                     | Boogie Nights                                      |      3.00
 Richard Adams                  | Beyond the Sea                                     |      6.70
 Righty Sock                    | Titanic                                            |      7.70
 Righty Sock                    | Vertigo                                            |      8.40
 Sasha Goldshtein               | American Beauty                                    |      7.00
 Scott LeBrun                   | Trainspotting                                      |
 Simon Wright                   | The Usual Suspects                                 |      8.60
 Victor Woeltjen                | Avatar                                             |      7.30
 Vincent Cadena                 | Slumdog Millionaire                                |      8.00
(17 rows)

Relational Algebra Expression:

Relational Algebra Expression: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Tree:

Relational Algebra Tree: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Expression:

Relational Algebra Expression: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Tree:

Relational Algebra Tree: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Expression:

Relational Algebra Expression: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Tree:

Relational Algebra Tree: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Expression:

Relational Algebra Expression: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Relational Algebra Tree:

Relational Algebra Tree: Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars .

Practice Online


Movie database model

Query Visualization for Sample Solution:

Duration:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Duration

Rows:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Rows

Cost:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Cost

Query Visualization for alternate Sample Solution:

Duration:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Duration

Rows:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Rows

Cost:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Cost

Query Visualization for second alternate Sample Solution:

Duration:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Duration

Rows:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Rows

Cost:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Cost

Query Visualization for third alternate Sample Solution:

Duration:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Duration

Rows:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - Rows

Cost:

Query visualization of Find the reviewer name, movie title, and stars in an order that reviewer name will come first, then by movie title, and lastly by number of stars - 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 those years when a movie received a rating of 3 or 4. Sort the result in increasing order on movie year. Return move year.
Next: From the following tables, write a SQL query to find those movies that have at least one rating and received highest number of stars. Sort the result-set on movie title. Return movie title and maximum review stars.

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