Fixed bug in sql-query