Fixed incorrect sql query