Add more sql clauses
Fixed things up.