What is the significance of using GROUP BY in the SQL query when counting data based on postal codes?

When counting data based on postal codes in SQL, using GROUP BY is significant because it allows us to group the results by postal code, ensuring that the count is calculated for each unique postal code. Without GROUP BY, the query would return a total count of all records without distinguishing between different postal codes.

$query = "SELECT postal_code, COUNT(*) as count FROM table_name GROUP BY postal_code";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo "Postal Code: " . $row['postal_code'] . " - Count: " . $row['count'] . "<br>";
    }
} else {
    echo "No results found.";
}