Fixed bug in SQL statement