Fixed bug in sql