tidy up sql a bit