fixing sql query