SQL queries recoded