How can the GROUP BY clause be used to aggregate data in PHP queries?

The GROUP BY clause in PHP queries can be used to aggregate data by grouping rows that have the same values in specified columns. This allows for the application of aggregate functions such as SUM, COUNT, AVG, etc. to the grouped data, providing a way to analyze and summarize information from a database table.

// Example of using GROUP BY clause in a PHP query
$sql = "SELECT column1, SUM(column2) AS total FROM table_name GROUP BY column1";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Column 1: " . $row['column1'] . " Total: " . $row['total'] . "<br>";
    }
} else {
    echo "No results found.";
}