How can the use of JOIN instead of subselects improve query performance in PHP?

Using JOIN instead of subselects can improve query performance in PHP by reducing the number of queries executed and minimizing the amount of data that needs to be processed. JOINs allow for combining data from multiple tables in a single query, which can be more efficient than using subselects that execute separate queries for each record.

<?php
// Using JOIN to improve query performance
$query = "SELECT orders.order_id, orders.order_date, customers.customer_name 
          FROM orders 
          JOIN customers ON orders.customer_id = customers.customer_id";
$result = mysqli_query($connection, $query);

// Process the result set
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Order ID: " . $row['order_id'] . " - Order Date: " . $row['order_date'] . " - Customer Name: " . $row['customer_name'] . "<br>";
    }
} else {
    echo "No orders found.";
}

// Free result set
mysqli_free_result($result);

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