Fix bug in SQL