Easy SQL Challenge: Using RANK() or LIMIT

365 Days of Daily Coding: Day 39

I would say that I had quite a productive day. I ended up doing all of the things that I had in my mind except cooking for lunch (hehe!). I have been trying to save some money that is I why I am quite strict with my expenses.

I solved 2 easy and I medium questions today. Thesedatys, I can solve these questions in few minutes. I have definitely made a lot of progress knowing that I would have had taken a long time to just solve an easy challenge few months ago.

Easy Challenge 1:

Solution 1:

SELECT title 
FROM film
WHERE length = (SELECT MIN(length) FROM film)
ORDER BY length ASC;

Solution 2:

SELECT title
FROM (
  SELECT title, RANK() OVER(ORDER BY length ASC) AS rnk
		FROM film) AS k
WHERE rnk = 1;

Easy Challenge 2:

SELECT title FROM
  (SELECT title, RANK() OVER (ORDER BY length) AS rnk
  FROM film) AS k
WHERE rnk = 2;

Medium Challenge:

SELECT title FROM
(SELECT f.title, COUNT(a.actor_id) AS counts
FROM film f LEFT JOIN film_actor a
ON f.film_id = a.film_id
GROUP BY title 
ORDER BY counts DESC) AS k
LIMIT 1;

Leave a comment