How can INNER JOIN be utilized in PHP queries to improve query efficiency?

Using INNER JOIN in PHP queries can improve query efficiency by reducing the number of rows that need to be processed. By joining tables based on a common key, INNER JOIN only returns rows that have matching values in both tables, eliminating unnecessary data retrieval. This can result in faster query execution and better performance when working with large datasets.

<?php
// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare and execute a query with INNER JOIN
$stmt = $pdo->prepare('SELECT users.id, users.name, orders.order_date FROM users INNER JOIN orders ON users.id = orders.user_id');
$stmt->execute();

// Fetch and display results
while ($row = $stmt->fetch()) {
    echo $row['id'] . ' - ' . $row['name'] . ' - ' . $row['order_date'] . '<br>';
}
?>