Cleaning up SQL queries