Unexpected output from SQL Server IN statement

Consider the following tables. Note the different id column names.

CREATE TABLE A (id int)
CREATE TABLE B (bid int)

      

A contains values 1 to 10

. B contains values 1 to 5

.

Now I have written a query to find all the rows that are present in both tables by comparing their ids. I wrote the wrong query by mistake:

select id from A
 where id in (select id from B)

      

See how I forgot the id column B

has a namebid

I don't understand why this query is being executed and returning all rows from A

. Anyway, I expected it to return1 to 5

Looking at the execution plan doesn't help me. See how a table scan of table B actually returns 10 rows in the image below. enter image description here

So, is this really the expected behavior of the query I wrote? I would expect an error message like the Invalid column name 'id'

one I get when I run in select id from B

isolation.

+3
sql-server-2012 in-operator


source to share


No one has answered this question yet

See similar questions:

nine
Sql server 2008 management studio not validating the syntax of my query

or similar:

1770
How do I check if a column exists in a SQL Server table?
389
Enable Remote Connections for SQL Server Express 2012
1
Aggregation in a dynamic pivot table in SQL Server 2012
1
SQL Server Select Query Multiple Results
1
Invalid column name for SQL Server in subqueries
0
cumulative column in SQL Server 2012
0
SQL query works even if it doesn't work
0
How do I select the record with the minimum value from the table?
0
INFORMATION_SCHEMA.COLUMNS select from table sql server
0
Inconsistent SQL Server results over 2 columns using = and <>



All Articles
Loading...
X
Show
Funny
Dev
Pics