Reformat sql queries