SQL Inner Join for Movie Ratings: Find Movies with 4 or 5 Stars
SQL Inner Join for Movie Ratings: Find Movies with 4 or 5 Stars
This SQL query uses an INNER JOIN to find movies that received a rating of 4 or 5 stars, sorted in increasing order of rating.
SQL Query:
SELECT year, ratingStars
FROM Movie
INNER JOIN Rating ON Movie.mID = Rating.mID
WHERE ratingStars >= 4
ORDER BY ratingStars ASC;
Explanation:
- Inner Join: The
INNER JOINcombines theMovieandRatingtables based on the sharedmID(movie ID) column. This ensures that only movie records with matching ratings are included in the result set. - Filtering: The
WHEREclause filters for movies that received a rating of 4 or 5 stars (ratingStars >= 4). - Sorting: The
ORDER BYclause sorts the result in ascending order ofratingStarsto present the highest rated movies first.
Why Use Inner Join?
The INNER JOIN is crucial for combining data from different tables based on a common field. In this case, it allows us to retrieve both movie information (from the Movie table) and its rating (from the Rating table) in a single query. Without it, we would have to perform separate queries for each table and then manually combine the results. The INNER JOIN simplifies this process and ensures data consistency by only including records that exist in both tables.
原文地址: https://www.cveoy.top/t/topic/pmN1 著作权归作者所有。请勿转载和采集!