What are the advantages of using the MySQL query "SELECT sum(tore) FROM test" over retrieving data and calculating the sum in PHP?

Using the MySQL query "SELECT sum(tore) FROM test" is advantageous over retrieving data and calculating the sum in PHP because it offloads the computation to the database server, which is optimized for such operations. This can result in faster query execution and reduced network traffic between the PHP application and the database server.

<?php
// Establish connection to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query to retrieve sum of 'tore' column from 'test' table
$query = "SELECT sum(tore) FROM test";
$result = mysqli_query($connection, $query);

// Fetch the result
$row = mysqli_fetch_array($result);

// Output the sum
echo "Sum of 'tore' column: " . $row[0];

// Close connection
mysqli_close($connection);
?>