What is the purpose of using GROUP BY in a MySQL query when selecting multiple columns?

When selecting multiple columns in a MySQL query, using GROUP BY allows you to group the results based on one or more columns. This can be useful when you want to aggregate data or perform calculations on grouped data. Without using GROUP BY, you may get duplicate rows in the result set.

$query = "SELECT column1, column2, SUM(column3) 
          FROM table_name 
          GROUP BY column1, column2";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        // Process the grouped data here
    }
}