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


source to share





All Articles