I recently stumbled upon this example. Why does query return all rows when using non-defined column on temp table "id". This only works for id and it only works when using IN subquery.
DECLARE @test TABLE
( Id INT)
INSERT INTO @test
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3
SELECT * FROM @test
SELECT 1 AS 'myid'
INTO #tmpId
--Returns one rows - OK
SELECT * FROM @test WHERE id in (SELECT myId FROM #tmpId)
--Returns all rowns - WHY
SELECT * FROM @test WHERE id in (SELECT id FROM #tmpId)
--ERROR - no column - OK
SELECT * FROM @test WHERE id in (SELECT asdf FROM #tmpId)
--ERROR - no column - OK
SELECT id FROM #tmpId
DROP TABLE #tmpId