fixed bug in sql