Left join question

Question: How do you know what to information to pull FROM and what table to JOIN to??

My Code :
select user_id
from premium_users
left join users
on users_id = premium_users.user_id
where premium_users.user_id is null ;

Correct Code:
SELECT users.id
FROM users
LEFT JOIN premium_users
ON users.id = premium_users.user_id
WHERE premium_users.user_id IS NULL;

Hello @board2501067444 ,

Are you asking about which table to pull from first?

A LEFT JOIN will return all to records from the FIRST table and any matching records from the SECOND table.

In YOUR Code: All the records from PREMIUM_USERS table will be listed and any matching records from USERS table.

In CORRECT code: All the records from USERS table will be listed and any matching records from PREMIUM_USERS table.

It’s important to understand the data in the tables and what information you are trying to retrieve. Depending on your data, you may get different results.

Left Join Explained

If this is a question from a lesson, please include the link. It will give us some context to help you better.