In what ways can window functions be utilized in MySQL queries in PHP to improve the efficiency and accuracy of data manipulation and retrieval processes?

Window functions in MySQL queries can be utilized in PHP to perform calculations across a set of rows related to the current row. This can improve the efficiency and accuracy of data manipulation and retrieval processes by allowing for more complex queries to be executed with fewer round trips to the database. Window functions can be used to calculate running totals, ranks, averages, and more, making it easier to analyze and present data in a meaningful way.

$sql = "SELECT id, name, score, 
               ROW_NUMBER() OVER(ORDER BY score DESC) as rank,
               SUM(score) OVER() as total_score
        FROM players";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row['id'] . " | Name: " . $row['name'] . " | Score: " . $row['score'] . " | Rank: " . $row['rank'] . " | Total Score: " . $row['total_score'] . "<br>";
    }
} else {
    echo "No results found.";
}