Update the SQL query construction