This really sucks. Why does contacts depend on dotlrn at all. Someone thought it should.
You can remove that line:
"and groups.group_id not in (select community_id from dotlrn_communities_all)"
Make sure you have activated "watch all files" in the Package Manager for the package where this query belongs to to make your change visible.