Medium SQL Challenge: USING JOINS

365 Days of Daily Challenge: Day 61

I was unable to continue my daily challenge cause I lost my momentum as I was quite overloaded with work last week and was unable to work on daily challenges. Happy to announce that I am back and ever more determined to complete my 365 days of SQL challenge.

I solved 1 easy and 1 medium challenge. The latter challenge was one of those challenges that I was unable to solve few days/weeks back. As I progress, I am also looking at old challenges that I was unable to solve.

Easy Challenge:

SELECT a.actor_id, a.first_name, a.last_name
 FROM actor_movie a JOIN
 actor_tv b
 ON a.actor_id = b.actor_id;

Medium Challenge:

SELECT have_rented, COUNT(have_rented) AS count
FROM(
SELECT CASE 
WHEN counts = 0 THEN 'never-rented'
ELSE 'rented'
END have_rented
FROM (
	SELECT c.customer_id, COUNT(r.rental_id) AS COUNTS 
	FROM customer c LEFT JOIN (
           SELECT rental_id, customer_id 
	   FROM rental
	   WHERE EXTRACT(YEAR FROM rental_ts) = 2020
	   AND EXTRACT(MONTH FROM rental_ts) = 05) r
ON c.customer_id = r.customer_id
GROUP BY c.customer_id) k
)l GROUP BY have_rented;

Leave a comment