I have recently written a stored procedure which has this part
select T.Id from Teams T
inner join
(
select SG.Team1Id, SG.Team2Id from SG
union all
select SP.Team1Id, SP.Team2Id from SP
) G
on T.Id in (G.Team1Id, G.Team2Id)
I am doubtful about this query efficiency
Will this query fetch all the records from SG
and SP
in the subquery and then will apply join condition? If yes, then I think this is not efficient
or
Sql server is smart enough to only fetch the rows from both the tables that matches the criteria of join?