What is the purpose of using GROUP BY in PHP when querying a database?

When querying a database in PHP, the GROUP BY clause is used to group rows that have the same values in specified columns. This is useful when you want to perform aggregate functions (like COUNT, SUM, AVG) on a group of rows rather than on individual rows. It helps organize and summarize data in a meaningful way.

// Example query using GROUP BY in PHP
$query = "SELECT column1, column2, COUNT(*) 
          FROM table_name 
          GROUP BY column1, column2";
$result = mysqli_query($connection, $query);

// Fetch and display results
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column1'] . ' - ' . $row['column2'] . ' : ' . $row['COUNT(*)'] . '<br>';
}