I am having a tough time to understand why does LEFT JOIN
/ IS NULL
eliminate records which are there in one table and not in the other.
Here is an example
SELECT l.id, l.value
FROM t_left l
LEFT JOIN t_right r
ON r.value = l.value
WHERE r.value IS NULL
Why should r.value = NULL
eliminate records ? I am not understanding . I know I am missing something very basic but at present I cant figure out even that basic one. I would appreciate if someone explains it to me in detail .
I want a very basic explanation.
Let's assume r-table is employees, and r_table is computers. Some employees don't have computers. Some computers are not assigned to anyone yet.
Inner join:
gives you the list of all employees who HAVE a computer, and the info about computer assigned for each of them. The employees without a computer will NOT appear on this list.
Left join:
gives you the list of ALL employees. The employees with computer will show the computer info. The employees without computers will appear with NULLs instead of computer info.
Finally
Left join with the WHERE clause will start with the same list as the left join (2), but then it will keep only those employees that do not have corresponding information in the computer table, that is, employees without the computers.
I this case, selecting anything from the
r
table will be just nulls, so you can leave those fields out and select only stuff from thel
table:Try this sequence of selects and observe the output. Each next step builds on the previous one.
Please let me know if this explanation is understandable, or you'd like me to elaborate some more.
EDITED TO ADD: Here's sample code to create the two tables used above:
There are 4 employees. Becky and John have computers. Anne and Bob do not have a computer. (Anne, has a comp_id 7, which doesn't correspond to any row in computers table - so, she doesn't really have a computer.)
It's Simple Left join funda..... following Query get all the record from left table and matched record From Right table if it doesn't match than it will return NUll value. at The end you filter data by using where condition which return values that are in Left table but not in right table
the left returns all the row from the left table
ON r.value = l.value
if there is no r.value for a l.value then the r is empty
r.value is null will be true
This is based on the difference between
INNER JOIN
andLEFT JOIN
. When you use anINNER JOIN
, the result only contains rows that match between the two tables (based on theON
condition).But when you use a
LEFT JOIN
, you also get rows in the result for all rows in the first table that don't have a match in the second table. In these cases, all the result columns from the second table are filled withNULL
as a placeholder.The
WHERE r.value IS NULL
test then matches these rows. So the final result only contains the rows with no match.This sentence:
will have all the elements from
t_left
table joined to all elements fromt_right
table. As you probably will now, in anINNER JOIN
only rows which matches both tables will be shown, sot_left
could have less rows than all the possible rows. In aLEFT JOIN
, you have ALL THE ROWS fromt_left
table plus the matched rows fromt_right
(if they can be joined). So, what happens with rows in t_left that not matches any t_right row? All the fields from t_right table that would be supposed to contain data will be filled with NULL values. So, if you only selectNULL
values from right table, you'll find all the values from thet_left
table that has no match on the right one, XD