w3resource
SQL exercises

SQL exercises on movie Database: Find all the years which produced a movie that received a rating of 3 or 4

SQL movie Database: Join Exercise-12 with Solution

12. Write a query in SQL to find all the years which produced a movie that received a rating of 3 or 4, and sort the result in increasing order.

Sample table: movie


Sample table: rating


Sample Solution:

SELECT DISTINCT mov_year
FROM movie, rating
WHERE movie.mov_id = rating.mov_id 
AND rev_stars IN (3, 4)
ORDER BY mov_year;

OR

SELECT DISTINCT mov_year
FROM movie
INNER JOIN rating 
ON movie.mov_id = rating.mov_id
WHERE rev_stars IN (3, 4)
ORDER BY mov_year;

OR

SELECT DISTINCT mov_year
FROM movie
INNER JOIN rating USING(mov_id)
WHERE rev_stars IN (3,4)
ORDER BY mov_year;

OR

SELECT DISTINCT mov_year
FROM movie NATURAL JOIN rating
WHERE rev_stars IN (3, 4)
ORDER BY mov_year;

Sample Output:

 mov_year
----------
     1997
(1 row)

Practice Online


Movie database model

Query Visualization for Sample Solution:

Duration:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Duration

Rows:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Rows

Cost:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Cost

Query Visualization for alternate Sample Solution:

Duration:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Duration

Rows:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Rows

Cost:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Cost

Query Visualization for second alternate Sample Solution:

Duration:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Duration

Rows:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Rows

Cost:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Cost

Query Visualization for third alternate Sample Solution:

Duration:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Duration

Rows:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Rows

Cost:

Query visualization of Find all the years which produced a movie that received a rating of 3 or 4 - Cost

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

Previous: Write a query in SQL to find those lowest duration movies along with the year, director's name, actor's name and his/her role in that production.
Next: Write a query in SQL to return 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.

What is the difficulty level of this exercise?



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