Jeff Dean's blog



Jeff DeanJeff Dean
Removing subqueries from 'negative joins' in SQL
edit Posted by Jeff Dean on Thursday September 01, 2011 at 10:37PM

I recently worked on a project that had Users who were members of Groups, and we had to figure out how to get a list of all Groups that a User was not a part of. In this post I'll describe two ways of accomplishing this: one with a subquery, and one without.

Removing subqueries may, in some cases, increase the performance of the query. If you have a subquery in an SQL statement and the query optimizer is telling you that the subquery is expensive, you can probably accomplish the same thing with regular joins and improve things a bit. The only way to tell is to run them on realistic datasets.