Updated SQL query for rankings