How can the use of COUNT() in MySQL help in calculating the ranking of teams in a league table?

To calculate the ranking of teams in a league table using COUNT() in MySQL, you can count the number of matches won by each team and order them in descending order. This will give you a list of teams ranked by the number of wins they have achieved in the league.

$query = "SELECT team_name, COUNT(*) AS total_wins 
          FROM matches 
          WHERE result = 'win' 
          GROUP BY team_name 
          ORDER BY total_wins DESC";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row['team_name'] . " - Total Wins: " . $row['total_wins'] . "<br>";
    }
} else {
    echo "No results found.";
}