Bugfix for sql query