I assume that either you wanted to add the DISTINCT keyword to the SELECT in the second query (or, less likely, the Department has only one Contact).
First, always start with "logical" considerations. The EXISTS construct is perhaps more intuitive, so all the physical ones are "equal", I would go with that.
Secondly, there will be one day when you need the ports of this code, not necessarily to another SQL product, but, say, the same product, but with a different optimizer. A decent optimizer must recognize that both are equivalent and come up with the same perfect plan. Suppose that, theoretically, the EXISTS design has slightly more potential for short circuiting.
Third, test it using a sufficiently large dataset. If performance is unacceptable, start looking at “physical” considerations (but I suggest you always keep your “logically clean” code in the comments for the upcoming day when the perfect optimizer comes :)
onedaywhen Jul 22 '10 at 13:33 2010-07-22 13:33
source share