0

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.

2
  • the fact that id doesn't exist in @y, if you were to run SELECT id FROM @y then it would give an error. May 19, 2014 at 14:42
  • The id is not selected from the @y table, but from the @c table!
    – martennis
    May 19, 2014 at 14:43

3 Answers 3

3

I'm not sure why, but when the sub-query is calling on incorrect columns, and is essentially invalid, the outer select statement still runs. The in / not in predicate with the sub query is essentially disregarded. I've seen this before but never find out why. However I just has a look around and found this link:

https://connect.microsoft.com/SQLServer/feedback/details/542289/subquery-with-error-does-not-cause-outer-select-to-fail

Where someone mentions the following:

I agree that the behavior is confusing but it is ANSI standard behavior for column name resolution at different scopes.

See this KB article for more info: http://support.microsoft.com/kb/298674

The reason this confused me even more is that I used the column name that does not exist in either table and hence got “expected” (invalid column name) error. So, if you are using a column name that cannot be resolved in the inner scope (SELECT Table1Id FROM Table2) but can be resolved in outer scope (SELECT * FROM Table1 WHERE ...), it will be resolved and bound at that scope.

This appears confusing in the example you gave but the same logic is applies if you use such construct in the WHERE clause of the inner query. E.g. consider inner query looking like this:

... (SELECT Table2Id FROM Table2 WHERE Table2Id = Table1Id)

The query by itself will fail but it will work since Table1Id will be bound Table1 in outer query.

2
  • Excellent, so it's a bug, but not "really" a bug as it's part of the ANSI standard. Brilliant :D May 19, 2014 at 14:54
  • It's certainly caught me out a couple of times in the past :) May 19, 2014 at 15:08
2

In a subquery, if the column name you mention doesn't exist in the tables that form the subquery, SQL will next look for the columns in the enclosing query (unless you've given an alias).

So, the id mentioned in your subquery is actually the id column from the @c table.

So, that id value will be returned once for every row that the subquery returns - but given that the subquery returns an empty set, this means that the NOT IN considers no values and so is successful.


This is why it's a really good habit to get into to use table aliases in subqueries - that way, if you accidentally name a column that only exists in an outer table, you get an error rather than an unexpected result:

SELECT * FROM @c WHERE id NOT IN (SELECT y.id FROM @y y);

produces an error.

0

In the SELECT id FROM @y there is no single value in @y. Because there is no value in @y, the id from @c will not be returned by the subquery. As far as i can tell SQL server returning 1 is correct. Only once one or more records are inserted into @y SQL server will return nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.