It is not an error because “The query that follows IN is correlated. That is, it can refer to tables outside the subquery”:
The subquery takes the value of ProductId from tbl_Product, because it was not found in tbl_Order.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Consider below Query where we have two tables tbl_Product and tbl_Order, tbl_Product has column ProductId but tbl_Order doesn't have column ProductId. Still below query runs and it doesn't even give any compilation error.
Select ProductId, ProductName, * from dbo.tbl_Product where ProductId IN (
Select ProductId from dbo.tbl_Order where OrderId = XXXX
)
It is not an error because “The query that follows IN is correlated. That is, it can refer to tables outside the subquery”:
The subquery takes the value of ProductId from tbl_Product, because it was not found in tbl_Order.
Similar questions have been raised several times before.
The answer in this thread is very detailed, you can refer to it.
Best regards,
Percy Tang
it doesn't even give any compilation error.
Why shoul it, it's a valid SQL statement and the column ProductId exists in the out, referencable table tbl_Product.
So no bug, only a logical failure on your side and that's the reason, why we alway should use full-qualified object names.