Fix incorrect sql qries