Please note the code block below:
There are two tables called outer_table and inner_table.
The table inner_table has two columns aband bc; but in sub query (first and second subquery) i have used it wrongly as selecta from inner_table.
I thought I would get a parse error. What I found is surprising. For first query no result set and for second query, 4 records of value 2 were returned. I ran it in SQL server 2005 and SQL server 2008.
3rd and 4th query, though behaved
Is it not a bug? Or can some one explain how SQL behaves here?
CREATE TABLE Outer_table (a INT , b INT) CREATE TABLE inner_table(ab INT ,bc INT) go INSERT outer_table SELECT 1,2 INSERT outer_table SELECT 1,2 INSERT outer_table SELECT 1,2 INSERT outer_table SELECT 1,2 INSERT inner_table SELECT 1,2 INSERT inner_table SELECT 1,2 INSERT inner_table SELECT 1,2 INSERT inner_table SELECT 1,2 GO SELECT a FROM outer_table WHERE a NOT IN (SELECT a FROM inner_table) SELECT b FROM outer_table WHERE b NOT IN (SELECT a FROM inner_table)
SELECT a FROM outer_table WHERE a NOT IN (SELECT c FROM inner_table)
SELECT b FROM outer_table WHERE b NOT IN (SELECT c FROM inner_table)