Trimming sql queries