SQL displaying only max value - sql

I am trying to return a result from sql query where sort by movie title and the highest rating of the movie - and get rid of the lower rating of the same movie. and theres only 1 select statement allowed.
i tried this;
Select distinct m.title, r.stars
from Movie as m inner join Rating as r on m.mid = r.mid
order by m.title
but can't figure out how to only choose the higher rating. If anyone has a good resource for the nuances it would help.

use MAX() which is an aggregate function that gets the greatest value in a certain field on each group.
Select m.title, MAX(r.stars) stars
from Movie as m inner join Rating as r on m.mid = r.mid
GROUP BY m.title
order by m.title

Related

Get Average in SQL Through Join

I'm just playing around with SQL and I'm trying to do the following.
I have 2 tables and here is their structure:
Movies_metadata Movies
ratings table:
Ratings
As there are many ratings for one movie, what I'd like to do is get the avg rating per movie and have it display next to the title which is only available in the Metadata table.
This is as far as I got but obviously the issue with my SELECT statement is that it'll return the average of all movies and display it for each record:
SELECT
(SELECT
AVG(rating)
FROM
`movies-dataset.movies_data.ratings`) AS rating_avg,
metadata.title,
metadata.budget,
metadata.revenue,
metadata.genres,
metadata.original_language,
metadata.release_date
FROM
`movies-dataset.movies_data.Movies_metadata` AS metadata
INNER JOIN `movies-dataset.movies_data.ratings` AS ratings
ON metadata.id = ratings.movieId
LIMIT 10
Here is an example of the result:
Result
I'm thinking I can potentially use a GROUP BY but when I try, I get an error
Appreciate the help!
The following should work:
SELECT movies_metadata.title, AVG(ratings.rating)
FROM movies_metadata
LEFT JOIN ratings ON movies_metadata.id = ratings.movieID
GROUP BY movies_metadata.title
You can swap movies_metadata.title by movies_metadata.id if not unique.
The LIMIT function and GROUP function might conflict with each other. Try getting the average rating as part of the inner join like this:
SELECT
ratings.averagerating,
metadata.title,
metadata.budget,
metadata.revenue,
metadata.genres,
metadata.original_language,
metadata.release_date
FROM `movies-dataset.movies_data.Movies_metadata` AS metadata
INNER JOIN (SELECT movieId, AVG(rating) averagerating FROM `movies-dataset.movies_data.ratings` GROUP by movieId) AS ratings
ON metadata.id = ratings.movieId
ORDER BY ratings.averagerating
LIMIT 5
Maybe try something like:
Select m.movieID, (r.rate_sum / r.num_rate) as avg_rating
From your_movies_table m
Left Join (select movie_id, sum(rating) as ‘rate_sum’, count(rating) as ‘num_rate’
From your_ratings_table
Group by movie_id) r
On m.movie_id = r.movie_id
I'm using a left join because I'm not sure if all movies have been rated at least once.

How can I generate SQL query with average score for movie from different tables?

I would very much appreciate your help with this query. My database (Oracle) has a table called MOVIES with fields like id and title. Then there is another one, SCORES which has fields like movie_id and score. It is a one-to-many relation.
Now, I'm looking for a way to get a SELECT that will give me a result like one below:
movie-title, avg(score where movie.id=scores.movie_id)
It looks simple at first glance, but I cannot pass through it. Any suggestions?
First off, you need to join the tables on the pk/fk
Then, in order to get the avg for each movie, you need to use group and specify the column (score) that you want.
In this case we are grouping by the id because it's unique and the movie title since it is in the select clause
select m.title, avg(s.score)
from movie m
inner join scores s
on m.id = s.movie_id
group by m.id, m.title
You just need join and group by as follows:
Select m.title, avg(s.score)
From movies m join scores s
On m.id = s.movie_id
Group by m.id, m.title

What makes this difference when using group by or join?

Can anybody tell the difference between them? The 1st and 3rd query can be successfully executed, and they have the same output. While the 2nd and 4th query cannot be executed, and they both raised an ERROR:
column "movie.title" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT title, (MAX(stars)-MIN(stars)) AS ratingspread.
My questions are:
why using m.mid are difference with r.mid while using group by (query 1 vs query 2)
why A inner join B is not equal to B inner join A (query 3 vs query 4)
SELECT title, (MAX(stars)-MIN(stars)) AS ratingspread
FROM rating r JOIN movie m
ON r.mid = m.mid
GROUP BY m.mid
ORDER BY ratingspread DESC, title;
SELECT title, (MAX(stars)-MIN(stars)) AS ratingspread
FROM rating r JOIN movie m
ON r.mid = m.mid
GROUP BY r.mid
ORDER BY ratingspread DESC, title;
SELECT title, (MAX(stars) - MIN(stars)) AS ratingspread
FROM movie
INNER JOIN rating USING(mId)
GROUP BY mId
ORDER BY rating_spread DESC, title;
SELECT title, (MAX(stars)-MIN(stars)) AS ratingspread
FROM rating
INNER JOIN movie USING(mId)
GROUP BY mId
ORDER BY ratingspread DESC, title
FYI the schema goes like this:
Movie ( mID, title, year, director )There is a movie with ID number mID, a title, a release year, and a director.
Reviewer ( rID, name ) The reviewer with ID number rID has a certain name.
Rating ( rID, mID, stars, ratingDate ) The reviewer rID gave the movie mID a number of stars rating (1-5) on a certain ratingDate.
Your queries are not producing what you think they are producing for a number of reasons.
Here is what you are really looking for I think:
SELECT
movie.mid
movie.title,
(MAX(rating.stars)-MIN(rating.stars)) AS ratingspread
FROM
movie
INNER JOIN rating on movie.mid = rating.mid
GROUP BY
movie.mid,
title
ORDER BY
(MAX(stars)-MIN(stars)) DESC,
title
There are a few things to point out: Firstly, you need to join on the matching column - in some of your queries you are joining mid to rid - these are unrelated fields. The movie ID is what joins the rating to the movie. Secondly, you are not getting the GROUP BY concept. What you are trying to do is get the spread of ratings for a given movie and display its title, so to display its title (and any other non-summarised data), you have to include the field in the group by. For further illustration, imagine you wanted to get the spread of all reviews by each reviewer, to see if they had any bias towards going hard or going soft on the movies they were reviewing. Here is how you would get the spread of reviews for each reviewer:
SELECT
reviewer.rid,
reviewer.name,
(MAX(rating.stars)-MIN(rating.stars)) AS ratingspread
FROM
reviewer
INNER JOIN rating on reviewer.rid = rating.rid
GROUP BY
reviewer.rid,
reviewer.name
ORDER BY
(MAX(stars)-MIN(stars)) DESC,
reviewer.name
By the way, the reason you want to include the ID as well as the title or reviewer name is to ensure you eliminate problems where two movies share the same title, or two reviewers have the same name.

How to correct the error in this sql code?

select title
from Movie M , Rating R
where exists((select M.mID
from Movie)
except (select R.mID
from Rating));
Error: near "(": syntax error
Do the proper JOINs with LEFT OUTER JOIN
SELECT m.* FROM Movie m
LEFT OUTER JOIN Rating r
ON r.mID = m.mID
WHERE r.mID IS NULL
Parentheses are used for subqueries, but a compound query is not composed of subqueries, so you must write both SELECTs together:
SELECT title
FROM Movie M , Rating R
WHERE EXISTS (SELECT M.mID
FROM Movie
EXCEPT
SELECT R.mID
FROM Rating);
But while this query is syntactically valid, it still does not make sense.
EXISTS just checks whether the subquery on the rights side return any rows; this usually requires a correlated subquery to make the subquery depend on the current row in the outer query. And it does not make sense to have the Rating table in the outer query.
You should use IN instead of EXISTS:
SELECT title
FROM Movie
WHERE mID IN (SELECT mID
FROM Movie
EXCEPT
SELECT mID
FROM Rating);
And you already know that all IDs in the Movie table exist in the Movie table, so you do not have to repeat it in the subquery; simply reverse the comparison:
SELECT title
FROM Movie
WHERE mID NOT IN (SELECT mID
FROM Rating);

SQLite Max of Count problem

I have the following query:
SELECT M.movieId, COUNT (*) AS mcount
FROM Movies M, Rentals R
WHERE M.movieId = R.movieId
GROUP BY M.movieId
I have a Movies DB and a Rentals DB, the resulting table currently shows the movie ID and how many times it has been checked out but i just can't figure out how to incorporate a MAX call on the mcount. Every time I try to do it I get a syntax error.
I want to be able to find the movie(s) that have been checked out the most.
You could just sort by the count column and limit the result to the number you want
SELECT M.movieId, COUNT(*) AS mcount
FROM Movies M, Rentals R
WHERE M.movieId = R.movieId
GROUP BY M.movieId
ORDER BY 2 DESC
LIMIT 1
would give you the top one.