How can the ORDER BY clause be optimized for sorting numerical values in a PHP database query?

When sorting numerical values in a PHP database query using the ORDER BY clause, it is important to specify the data type of the column being sorted. This can help optimize the sorting process and ensure that the results are returned in the correct order. To do this, you can use the CAST function in SQL to explicitly convert the column data to a numerical type before sorting.

$query = "SELECT * FROM table_name ORDER BY CAST(column_name AS SIGNED) ASC";
$result = mysqli_query($connection, $query);

// Fetch and display results
while($row = mysqli_fetch_assoc($result)) {
    // Display the data
}