SQL Multiple joins

Hello,

I’m presently working with the Chinook Database on the “Intermediate Joins in SQL” course.
I’d like to know if there’s a specific order we should respect when writing the query. If so, how can we determine it?
I wrote almost the exact same query than Dataquest with a different order and i keep having errors.

Enclosed here is my code and Dataquest’s.

Thanks in advance

https://app.dataquest.io/c/46/m/189/intermediate-joins-in-sql/3/joining-more-than-three-tables

My Code:
SELECT
il.track_id,
t.name track_name,
art.name artist_name,
mt.name track_type,
il.unit_price,
il.quantity
FROM album al
INNER JOIN track t ON t.track_id = al.track_id
INNER JOIN invoice_line il ON il.track_id=t.track_id
INNER JOIN media_type mt ON mt.media_type_id = t.media_type_id
INNER JOIN artist art ON art.artist_id=al.artist_id
WHERE il.invoice_id = 4;

Dataquest Code:
SELECT
il.track_id,
t.name track_name,
ar.name artist_name,
mt.name track_type,
il.unit_price,
il.quantity
FROM invoice_line il
INNER JOIN track t ON t.track_id = il.track_id
INNER JOIN media_type mt ON mt.media_type_id = t.media_type_id
INNER JOIN album al ON al.album_id = t.album_id
INNER JOIN artist ar on ar.artist_id = al.artist_id
WHERE il.invoice_id = 4;

You are trying to JOIN on al table’s track_id in the line above, but al, that is the table album doesn’t have a track_id column. The query can’t access something that doesn’t exist, so you do need to be more careful about that to begin with.

1 Like

Ah ouais. Thank you!