I was debugging someone else's query and came across a very weird statement that looked like it shouldn't work at all. I distilled this down from the original query to this:
DECLARE @c TABLE (id INT);
DECLARE @y TABLE (name VARCHAR(50) PRIMARY KEY);
INSERT INTO @c VALUES (1);
SELECT
c.*
FROM
@c c
WHERE
id NOT IN (
SELECT
id
FROM
@y
WHERE
id IS NOT NULL);
But how can this possibly work?? I added the constraint that id IS NOT NULL, but removing this doesn't appear to change the behaviour.
You can also remove the PRIMARY KEY on the temporary table, this was just a play to show that the execution plan uses the index somehow!?
This is the "lite" version:
DECLARE @c TABLE (id INT);
DECLARE @y TABLE (name VARCHAR(50));
INSERT INTO @c VALUES (1);
SELECT * FROM @c WHERE id NOT IN (SELECT id FROM @y);
When executed in SQL Server 2008 R2 this will return an answer of 1.