Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
aspnet100
Starting Member
1 Post |
Posted - 2005-06-11 : 09:48:53
|
| How to interpret the result from this query( the filter: AND E.deptno IS NULL in ON confuses me):Result:Deptno deptname empid empname deptno jobid salary ----------- --------------- ----------- ---------- ----------- ----------- --------- 100 Engineering NULL NULL NULL NULL NULL200 Production NULL NULL NULL NULL NULL300 Sanitation NULL NULL NULL NULL NULL400 Management NULL NULL NULL NULL NULLQuery:SELECT * FROM Departments AS D LEFT OUTER JOIN Employees AS E ON D.deptno = E.deptno AND E.deptno IS NULLTables:Deptno deptname ----------- --------------- 100 Engineering200 Production300 Sanitation400 Management==================empid empname deptno jobid salary ----------- ---------- ----------- ----------- --------- 1 Leo 400 30 10000.002 George 200 20 1000.003 Chris 100 10 2000.004 Rob 400 30 3000.005 Laura 400 30 3000.006 Jeffrey NULL 30 5000.00 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-11 : 10:34:16
|
By specifying that a column in the WHERE clause as NULL from the outer table (used in an OUTER JOIN, d.deptno = e.deptno), you're asking for records from your inner table that don't exist in the outer table.However in your query, specifying the join column as null in the JOIN criteria, makes no sense. Your basically joining only on non-existant values. So the results will be all rows from inner table and all nulls for returned columns from the outer table.this illustrates the point:set nocount ondeclare @tbI table (c int)declare @tbO table (c int)insert @tbI values (1)insert @tbI values (2)insert @tbO values (2)insert @tbO values (3)select o.c as o, i.c as ifrom @tbI ileft join @tbO o on i.c = o.c and o.c is nullselect o.c as o, i.c as ifrom @tbI ileft join @tbO o on i.c = o.c where o.c is null Be One with the OptimizerTG |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-06-11 : 17:16:50
|
The problem is that the join condition can never be true, unless you have ANSI_NULLS OFF. With ANSI_NULLS ON Two NULLS do not equal each other, so even if D.deptno and E.deptno are both null, the condition is not true. It is usually not a good ides to turn off ANSI_NULLS without understanding exactly what it means. You should read the topic "NULL Comparison Search Conditions" in SQL Server Books Online.This shows how it works:set ansi_nulls onif NULL = NULL begin print '= TRUE with ansi_nulls on' endelse begin print '= Not TRUE with ansi_nulls on' endif NULL <> NULL begin print '<> TRUE with ansi_nulls on' endelse begin print '<> Not TRUE with ansi_nulls on' endset ansi_nulls offif NULL = NULL begin print '= TRUE with ansi_nulls off' endelse begin print '= Not TRUE with ansi_nulls off' endif NULL <> NULL begin print '<> TRUE with ansi_nulls off' endelse begin print '<> Not TRUE with ansi_nulls off' endResults:= Not TRUE with ansi_nulls on<> Not TRUE with ansi_nulls on= TRUE with ansi_nulls off<> Not TRUE with ansi_nulls off quote: Originally posted by aspnet100SELECT * FROM Departments AS D LEFT OUTER JOIN Employees AS E ON D.deptno = E.deptno AND E.deptno IS NULL
CODO ERGO SUM |
 |
|
|
|
|
|