What are the advantages and disadvantages of using aggregation functions in SQL queries for data retrieval in PHP?

When retrieving data from a database using SQL queries in PHP, aggregation functions like SUM, COUNT, AVG, etc., can be useful for performing calculations on the data. However, using aggregation functions can sometimes make the query more complex and slower, especially when dealing with large datasets. It is important to carefully consider the trade-offs between the advantages and disadvantages of using aggregation functions in SQL queries for data retrieval in PHP.

// Example of using SUM aggregation function in SQL query for data retrieval in PHP
$query = "SELECT SUM(amount) AS total_amount FROM transactions WHERE user_id = 1";
$result = mysqli_query($conn, $query);

if($result){
    $row = mysqli_fetch_assoc($result);
    $totalAmount = $row['total_amount'];
    
    echo "Total amount: $totalAmount";
} else {
    echo "Error: " . mysqli_error($conn);
}