Fixed bug in an sql query