Reason for Joining to a Subquery

Screen Link:
https://app.dataquest.io/m/464/intermediate-joins-in-sql/4/combining-multiple-joins-with-subqueries

My Code:

select
	a.title as album,
	s.name as artist,
	sum(i.quantity) as tracks_purchased
from
	invoice_line i
join track t on
	i.track_id = t.track_id
join album a on
	t.album_id = a.album_id
join artist s on
	a.artist_id = s.artist_id
group by
	a.title,
	s.name
order by
	sum(i.quantity) DESC
limit 5

What I expected to happen:
The code to return the correct results.

What actually happened:
The code returned the correct results.

Replace this line with the output/error

The “solution” code specifically joins the invoice_line table to a subquery that joins the track table, artist table, and album table. What is the reason behind using the additional subquery? Why not use the approach outlined in my code and simply inner join the four tables together? Thanks!

1 Like

As far as I know, INNER JOIN and subqueries are interchangable.

@ammacdonald3, it seems like you can solve this problem using either joins or subqueries. From my standpoint, I used the same technique as you did because the code was simpler to write and I prefer doing multiple joins because they are easier to follow (just my opinion). StackOverflow provides a debate about these two methods found here.

Hope this helps!

I believe using the subquery is just for the sake of practicing what you learned in the lesson.