Feb 2, 2009 0
Rewriting Sub-selects as Joins
Your RDBMS will usually rewrite your sub-selects behind the scenes as joins. However, there are times where you’ll want to do this yourself. For example, past versions of MySQL did not play well with sub-selects. Here are a couple examples of how to rewrite sub-selects as joins.
select * from table_a where id not in (select a_id from table_b); -- can be rewritten as... select * from table_a left outer join table_b on table_a.id = table_b.a_id where table_b.a_id is null; select * from table_a where id in (select a_id from table_b); -- can be rewritten as... select * from table_a left outer join table_b on table_a.id = table_b.a_id where table_b.a_id is not null;
