fixing sql queries