LEFT JOIN - Part II lecture
Hi, can you please explain the first 4 rows in the output from this LEFT JOIN query? Why are the emp_no NULL? I thought they would be 999004 to 007 as they are in the dept_manager_dup table (the m table). As well why are the 3rd nd 4th row dept_name fields Public Relations and Marketing? Thank you!
Hi Quang!
Thanks for reaching out.
Actually, you should rewrite your query like this:
SELECT
d.dept_no, m.emp_no, d.dept_name
FROM
departments_dup d
LEFT JOIN
dept_manager_dup m ON m.dept_no = d.dept_no
ORDER BY d.dept_no;
The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match. So, following this you will obtain NULL values for the first two records. The order of the tables matters when we use LEFT/RIGHT JOIN.
Hope this helps.
Best,
Tsvetelin