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)