Medium SQL Challenge: Using LIMIT()

365 Days of Daily Coding: Day 50

I did another sqlpad challenge. For one reason or the other I just wasn’t able to get the pass the challenge. I think I have gotten the right answer but it says that is not the correct answer. I will find another way to solve this in my Day 51.

Medium Challenge:

WITH temp AS(
SELECT r.rental_id, i.film_id
  FROM rental r JOIN inventory i
  ON r.inventory_id = i.inventory_id
  WHERE EXTRACT(YEAR FROM DATE(r.rental_ts)) = 2020
  AND EXTRACT(MONTH FROM DATE(r.rental_ts)) = 06
)
SELECT film_id, title FROM(
SELECT f.film_id, f.title, COUNT(t.rental_id) AS counts
FROM temp t JOIN film f
ON t.film_id = f.film_id
GROUP BY f.film_id
ORDER BY counts DESC, title, film_id DESC
LIMIT 5) AS k;

Shorter version:

SELECT COUNT(DISTINCT customer_id)
FROM rental
WHERE EXTRACT(YEAR FROM rental_ts) = 2020 
AND EXTRACT(MONTH FROM rental_ts) = 05 
AND customer_id IN (SELECT customer_id FROM rental
					  WHERE EXTRACT(YEAR FROM rental_ts) = 2020 
  AND EXTRACT(MONTH FROM rental_ts) = 06 );

Leave a comment