I have the following SQL query which does not give me the correct results. Can you provide me with the correct SQL query for this? It is not returning the row containing ( NULL, 5 )
.
-- Create a table
CREATE TABLE [dbo].[MyTable]
(
[X] INT,
[Y] INT
)
GO
-- Insert some data
INSERT INTO MyTable ( [X], [Y] ) VALUES ( 1, 2 )
GO
INSERT INTO MyTable ( [X], [Y] ) VALUES ( 2, 3 )
GO
INSERT INTO MyTable ( [X], [Y] ) VALUES ( 3, 4 )
GO
INSERT INTO MyTable ( [X], [Y] ) VALUES ( NULL, 5 )
GO
/*
Find the rows whose [X] value is other than 1, 3
*/
-- Problem
SELECT [X], [Y]
FROM MyTable
WHERE [X] NOT IN ( 1, 3 ) -- We are missing NULL value row in the output result set
GO
We can use IS NULL
records first and then UNION them with the results from the above Query. That is one of the options. Are there any other ways that we can tackle this problem?