Fix synchronous SQL query