Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LEFT JOIN gives different data set depending on the position of WHERE condition

See the below 3 Counts, just to give you a brief idea about the table data. All skCitizen in [dbo].[LUEducation] is present in [dbo].[LUCitizen]

SELECT  COUNT(*) FROM   [dbo].[LUCitizen] --115000 ROWS
SELECT  COUNT(*) FROM   [dbo].[LUEducation]  --201846 ROWS

SELECT  COUNT(*) --212695 ROWS
FROM    [dbo].[LUCitizen] C
LEFT JOIN   [dbo].[LUEducation] E
ON      C.skCitizen = E.skCitizen

SELECT  COUNT(*) FROM   [dbo].[LUEducation] WHERE skSchool = 24417 --4 ROWS

See the below 2 queries,

SELECT  C.skCitizen,E.skCitizen
FROM    [dbo].[LUCitizen] C
LEFT JOIN   [dbo].[LUEducation] E
ON      C.skCitizen = E.skCitizen
WHERE   E.skSchool = 24417
--4 ROWS

SELECT  C.skCitizen,E.skCitizen
FROM    [dbo].[LUCitizen] C
LEFT JOIN   (SELECT * FROM [dbo].[LUEducation] WHERE skSchool = 24417) E
ON      C.skCitizen = E.skCitizen
--115000 ROWS

In the last 2 queries, the confusing query for me is the 1st one. There i expected 115000 rows, but only 4 rows displayed. According to my understanding, Full rows from [dbo].[LUCitizen] will be displayed, then 4 rows from [dbo].[LUEducation] will be LEFT Joined.

Why is the 2 Queries different?

Pardon me , if this is a duplicate question.

like image 999
HHH Avatar asked Aug 25 '14 12:08

HHH


People also ask

How does left join with WHERE clause works?

Left join returns all values from the right table, and only matching values from the left table. ID and NAME columns are from the right side table, so are returned. Score is from the left table, and 30 is returned, as this value relates to Name "Flow". The other Names are NULL as they do not relate to Name "Flow".

Can we use condition on left join?

The LEFT JOIN condition is used to decide how to retrieve rows from table 2nd_table. If there is a row in 1st_table that matches the WHERE clause, but there is no row in 2nd_table that matches the ON condition, an extra 2nd_table row is generated with all columns set to NULL.

What is the difference between left join with WHERE clause and left join without WHERE clause?

When you use a Left Outer join without an On or Where clause, there is no difference between the On and Where clause. Both produce the same result as in the following. First we see the result of the left join using neither an On nor a Where clause.

Does LEFT join change number of rows?

condition.


2 Answers

When you do this:

SELECT  C.skCitizen,E.skCitizen
FROM    [dbo].[LUCitizen] C
LEFT JOIN   [dbo].[LUEducation] E
ON      C.skCitizen = E.skCitizen
WHERE   E.skSchool = 24417;

You are turning the left join into an inner join, because E.skSchool is NULL for non-matching rows. The correct way to put a condition on the second table in a left join is to use the on clause:

SELECT  C.skCitizen,E.skCitizen
FROM    [dbo].[LUCitizen] C
LEFT JOIN   [dbo].[LUEducation] E
ON      C.skCitizen = E.skCitizen AND E.skSchool = 24417;
like image 200
Gordon Linoff Avatar answered Sep 28 '22 01:09

Gordon Linoff


If the left join fails to find a match in E, columns from E receive a null value. Then the where clause:

E.skSchool = 24417

Becomes:

null = 24417

Which is not true. So it will filter out all rows.

like image 21
Andomar Avatar answered Sep 28 '22 01:09

Andomar