简体   繁体   中英

Write a query to find the name of the film which has been rented the fifth-most number of times

SELECT F.TITLE
FROM FILM AS F
LEFT OUTER JOIN INVENTORY AS I USING (FILM_ID)
LEFT OUTER JOIN RENTAL AS R USING (INVENTORY_ID)
GROUP BY F.TITLE,R.RENTAL_ID
ORDER BY COUNT(R.RENTAL_ID) DESC   
LIMIT 4,1;

I have used the query above to get the fifth-most rented movie in SAKILA DB. I am having no error with this but am not getting the expected result. Please help me out! Sakila ERD image below

Try the below code, it works.

select title from film where film_id in (

select film_id from inventory where inventory_id in (
select inventory_id from rental group by inventory_id 
                                order by count(inventory_id) desc ))

limit 4, 1 

you can do it using joins too

SELECT F.TITLE FROM FILM AS F 
INNER JOIN INVENTORY AS I USING (FILM_ID)
INNER JOIN RENTAL AS R USING (INVENTORY_ID)
GROUP BY F.TITLE,R.RENTAL_ID
ORDER BY COUNT(R.RENTAL_ID) DESC   
LIMIT 4,1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM