move key WQL restrictions from WHERE/IN to JOIN

I tried to make the SQL a bit more readable here and there as well.