Fixed bug in SQL