Bug fix in SQL