Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of NOT IN and NOT EQUALS different behaviour in SQL query

Tags:

sql

oracle

I thought NOT IN behaves the same as != in a query. But a query using != returns more rows than a query using NOT IN:

SELECT  count(A.NO)
FROM A
WHERE
A.CODE != 'a' 
AND 
A.CODE  != 'b'
AND
A.CODE  != 'c'
AND 
A.NAME  != 'd' 
AND
A.NAME  != 'e'

returns 1566 rows, whereas

SELECT  count(A.NO)
FROM A
WHERE
A.CODE NOT IN ('a','b','c')
AND
A.NAME NOT IN ('d','e') 

returns only 1200 rows.

I suppose NOT IN excludes NULL values - would that be the only difference?

like image 247
Harshana Avatar asked Feb 14 '13 02:02

Harshana


1 Answers

I have tried to replecate the problem using this simplified SQL fiddle, however, it returns the same number for both versions.

What is differant about your data?

like image 181
Dale M Avatar answered Oct 17 '22 01:10

Dale M