Updated sql query