How can SQL queries be optimized for sorting data in PHP applications?

To optimize SQL queries for sorting data in PHP applications, you can add an ORDER BY clause in your SQL query to sort the data based on a specific column. Additionally, you can index the columns that are frequently used for sorting to improve query performance.

// Example SQL query with ORDER BY clause for sorting data
$query = "SELECT * FROM table_name ORDER BY column_name ASC";
$result = mysqli_query($connection, $query);

// Example of indexing a column for sorting
$query = "ALTER TABLE table_name ADD INDEX index_name (column_name)";
mysqli_query($connection, $query);