Fix SQL queries

There are some errors in binding order and sql queries.