Inside an SP I need to find out the Id's of some clients of the first account whose Code matches any of the second account's clients. I wrote the following query that works -
SELECT DISTINCT cil.Id FROM ClientIdList AS cil
INNER JOIN Client AS c1
ON cil.Id = c1.Id
INNER JOIN Client AS c2
ON c1.Code = c2.Code
WHERE c2.AccountId = 2
ORDER BY cil.Id
Here ClientIdList is a single-column table-type variable which holds the Ids of the selected clients from the first account (and I need to use this variable for other requirements prior to this point). I can get the same correct output if I put the condition in the WHERE clause as the JOIN condition as follows -
SELECT DISTINCT cil.Id FROM ClientIdList AS cil
INNER JOIN Client AS c1
ON cil.Id = c1.Id
INNER JOIN Client AS c2
ON c1.Code = c2.Code AND c2.AccountId = 2
ORDER BY cil.Id
Considering the 2000 accounts and 10000 clients per account (that is, 2000 x 10000 rows in Client table) which one would be an appropriate choice?
Can the query be optimized further to improve performance?
Edit : Actually the condition is c2.AccountId = @accountId where the @accountId is a parameter to the SP
Edit 2 : As much as I understand, with the WHERE clause version the JOIN will be performed with the rest of the Client table, and then the result will be filtered based on the WHERE condition. But with the later version the JOIN should be performed with a smaller set of rows for which the condition satisfies. Am i right? If so, shouldn't the later version give better performance?