Fixed incorrect sql queries