I am just starting to learn SQL
. Can someone explain what ON
means in the example?
SELECT title, imdb_score
FROM films
JOIN reviews
ON films.id = reviews.film_id
WHERE title = 'To Kill a Mockingbird';
I am just starting to learn SQL
. Can someone explain what ON
means in the example?
SELECT title, imdb_score
FROM films
JOIN reviews
ON films.id = reviews.film_id
WHERE title = 'To Kill a Mockingbird';
ON films.id = reviews.film_id
To specify JOIN
condition. With ON
clause you specify exactly on which column you want to match both table records. You can as well specify extra conditions like
ON films.id = reviews.film_id
AND <more condition>
It's a join condition.
In your example you will get title
and imdb_score
from those rows from films
and reviews
, which have the same id
and film_id
.
'ON' shows the connection between tables. For your example, the main table is 'films' and you want to bring data from table 'reviews'. If you dont tell computer how to connect data it cant know which movie has which reviews. You are saying that id's are the same for both of this table when you use 'ON'
I would add for better readability, the ON
right next to the table name you want to join
SELECT title, imdb_score
FROM films
JOIN reviews ON films.id = reviews.film_id
WHERE title = 'To Kill a Mockingbird';