1

I'm trying to find the film that has been rented the most without using limit. I'm trying to use the following query:

SELECT f.title, f.film_id
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN rental r ON r.inventory_id = i.inventory_id
GROUP BY f.film_id
HAVING COUNT(r.rental_id) = MAX(
    SELECT COUNT(r2.rental_id)
    FROM rental r2, inventory i2
    WHERE i2.inventory_id = r2.inventory_id
    GROUP BY i2.film_id);

but mySQL tells me that I have a syntax error somewhere in here SELECT COUNT(r2.rental_id) FROM rental r2, inventory however, when I run the subquery independently it returns the expected table. Am I doing something massively wrong?

relevant database schema:

film(film  id, title, description, release year, language id, original language id, rental duration, rental rate, length, replacement cost, rating, special features, last update)
inventory(inventory id, film id, store id, last update)
rental(rental id, rental date, inventory id, customer id, return date, staff id, last update)
1
  • 1
    I don't think you can have a query inside MAX()... Commented Oct 15, 2013 at 2:28

2 Answers 2

1

You can't use MAX() over a result set, but you can use

someValue >= ALL (subquery)

to achieve what you're attempting, because ALL requires that the preceding operator be true for all values in the set.

Try this:

SELECT f.title, f.film_id
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN rental r ON r.inventory_id = i.inventory_id
GROUP BY f.film_id
HAVING COUNT(r.rental_id) >= ALL (
    SELECT COUNT(r2.rental_id)
    FROM rental r2, inventory i2
    WHERE i2.inventory_id = r2.inventory_id
    GROUP BY i2.film_id);
Sign up to request clarification or add additional context in comments.

2 Comments

How should I use max in this context? my subquery definitely returns more than one line, as there is more than one film_id in inventory and at least one rental corresponding to each inventory entry.
FACEPALM, how did I not think of that, so obvious in hindsight. thanks.
0

I don't have a database to test in, but this should work:

Edited to LIMIT 1 instead of SELECT TOP 1 for MySQL)

SELECT f.title, f.film_id
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN rental r ON r.inventory_id = i.inventory_id
GROUP BY f.film_id
HAVING COUNT(r.rental_id) = (SELECT COUNT(r2.rental_id)
                              FROM rental r2, inventory i2
                              WHERE i2.inventory_id = r2.inventory_id
                              GROUP BY i2.film_id
                              ORDER BY COUNT(r2.rental_id) desc
                              LIMIT 1) s                             

3 Comments

I don't think mySQL supports TOP
Looks like you're correct, serves me right... I've modified to the MySQL equivalent (LIMIT 1)
and I cannot use limit (mentioned in the question).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.