Strange Behavior of NULL Values in SQL Queries
I found a strange behavior of NULL Values when I have a table values as in the following:
Declare @val table
(
Id int,
Username varchar(100),
Desg varchar(100),
Level varchar(100)
)
Insert into @val
select 1,'AAA',NULL,1 union all
select 1,'BBB',NULL,1 union all
select NULL,'CCC','SE',1 union all
select 2,NULL,'PL',4 union all
select 2,'EEE',NULL,5
-- Query 1
select * from @val
-- Query 2
select Id,Username,desg,COUNT(*)
[No. of Employees] from @val
where desg <> 'SE'
group by Id,Username,Desg
-- Query 3
select Id,Username,desg,COUNT(*)
[No. of Employees] from @val
where desg is null or desg <> 'SE'
group by Id,Username,Desg
When we execute the above queries in the Sql Server Management Studio, we would get the following outputs:
Query 1 Result:
This is the output of the entire table we just created.
Query 2 Result:
This is the output when I use this condition – desg <> ‘SE’. But it should show the remaining rows that are with NULL values.
Query 3 Result:
when I use this condition – desg IS NULL or desg <> ‘SE’, I am able to get the required output.
Somebody who knows can explain the trick ;-)
Labels: .NET, .NET Interview Questions, job seekers, NULL Values, SQL Server, WHERE Clause