How can the SQL query in PHP be optimized to efficiently find duplicate IP addresses in a database?

To efficiently find duplicate IP addresses in a database using SQL query in PHP, you can use the GROUP BY clause along with the HAVING clause to filter out the duplicate IP addresses. By grouping the records based on the IP address and then using the HAVING clause to filter out the groups with more than one record, you can efficiently identify the duplicate IP addresses.

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

// SQL query to find duplicate IP addresses
$sql = "SELECT ip_address, COUNT(*) as count
        FROM your_table
        GROUP BY ip_address
        HAVING count > 1";

// Execute the query
$stmt = $pdo->query($sql);

// Fetch and display the results
while ($row = $stmt->fetch()) {
    echo "Duplicate IP Address: " . $row['ip_address'] . " Count: " . $row['count'] . "<br>";
}
?>