Easy SQL Challenge: Using JOINS again!

365 Days of Daily Challenge: Day 63

I like to sleep late cause I like working at night. However, working late has a disadvantage when you have a meeting in the morning next day. I was quite tired the whole day and was not able to be as much productive as I would like to have been.

I solved an easy SQL challenge today. It seemed more like a medium SQL challenge to me.

WITH temp AS(
SELECT p.customer_id, p.amount, c.address_id
  FROM payment p INNER JOIN customer c
  ON p.customer_id = c.customer_id
)

SELECT * FROM(
SELECT c.city, SUM(t.amount) AS amount
FROM temp t JOIN address a
ON t.address_id = a.address_id
JOIN city c
ON c.city_id = a.city_id
GROUP BY c.city) AS k
ORDER BY amount DESC
LIMIT 5;

Leave a comment