SQLite Max of Count problem - sql

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.

Related

Which actor has the highest difference in ratings?

I need further help with my SQL problem.
In this database on movies, ratings and actors: https://i.stack.imgur.com/qFIbC.jpg
I am required to find the actor who has the largest difference between their best and their worst rated movie.
The condition is that the ratings cannot be lower than 3! (>3)
My current SQL looks as follows:
SELECT * FROM stars
JOIN ratings ON stars.movie_id = ratings.movie_id
WHERE ratings.movie_id = (
SELECT MAX(rating) - MIN(rating) FROM ratings
WHERE rating > 3);
My expectations were that I would get somewhat of a result in my Github terminal that I can work with to adjust my SQL query.
But I seem to have reached a dead-end and I'm not sure how to solve this solution
You need to GROUP BY actor to calculate everyone's rating range. Then, take the actor with the largest range. Something like this:
SELECT
person_id,
MAX(rating) - MIN(rating) AS rating_range
FROM
stars
JOIN ratings ON stars.movie_id = ratings.movie_id
WHERE
rating > 3
GROUP BY
person_id
ORDER BY
2 DESC
LIMIT
1
;

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.

Why does the code 'ORDER BY rating DESC' cause an error in my code?

In the cs50 class I was assigned to list all of the movies in 2012 and their ratings, in descending order by rating in sql. When I made this code the error
'line 12: 2111 File size limit exceeded/usr/bin/sqlite3 -header
-separator ' | ' "$#"'
and I would like to know why.
SELECT title, rating FROM movies, ratings
WHERE year = 2012 AND movie_id IN (SELECT id FROM movies
WHERE year = 2012)
ORDER BY rating DESC
The error does go away after I remove 'ORDER BY rating DESC', but I cannot do that because check50 wouldn't accept it.
This is an accidental cross-join, or Cartesian Product, which will cause the result set to be large: one row for each (movie,rating) combination instead of one row for each rating with the title pulled from the related movie.
The large result size is apparently causing a resource exhaustion error when it's sorted. When you don't have an ORDER BY the results don't have to be buffered and sorted, so it requires less resources.
Should be something like
SELECT m.title, r.rating
FROM movies m
JOIN ratings r
ON m.id = r.movie_id
WHERE r.year = 2012
AND m.year = 2012
ORDER BY r.rating DESC
You should rewrite your query as follows:
SELECT title, rating FROM movies INNER JOIN ratings
ON movies.movie_id = ratings.movie_id
WHERE movies.year = 2012
ORDER BY rating DESC

SQL displaying only max value

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

Nesting Aggregate Functions - SQL

I want to make a SQL query which finds the catagory of awards for movies which has the highest average rating, so for a group of movies which have won a particular award, if they have a higher average rating than any other awards group of movies then it will be returned.
I tried something like this:
SELECT MAX(AVG(m."Rating"))
FROM awards a, movies m
WHERE a."Title" = m."Title"
GROUP BY a."Award"
but it seems that aggregate functions cannot be nested. How can I call the max function on the average ratings for each catagory?
If you are only interested in the value itself, the following should do it:
SELECT MAX(avg_rating)
FROM (
SELECT AVG(m."Rating") as avg_rating
FROM awards a, movies m
WHERE a."Title" = m."Title"
GROUP BY a."Award"
) t
Otherwise Adrian's solution is better.
This will bring your desired result:
SELECT a."Award", AVG(m."Rating")
FROM awards a, movies m
WHERE a."Title" = m."Title"
GROUP BY a."Award"
ORDER by AVG(m."Rating") desc
LIMIT 1
This will allow you not only get the MAX value, but its corresponding Award info
Did you try this?
SELECT MAX(
SELECT AVG(m."Rating")
FROM awards a, movies m
WHERE a."Title" = m."Title"
GROUP BY a."Award"
)
Another way is to use windowed MAX:
SELECT MAX(AVG(m."Rating")) OVER()
FROM awards a -- proper JOIN syntax
JOIN movies m ON a."Title" = m."Title"
GROUP BY a."Award"
LIMIT 1;
db<>fiddle demo