What are the potential pitfalls of using GROUP BY versus SELECT DISTINCT when querying data from a MySQL database in PHP?

Using GROUP BY can potentially lead to unexpected results when querying data from a MySQL database in PHP, as it aggregates rows based on a specified column. This can cause data to be combined or omitted unintentionally. To avoid this issue, it is recommended to use SELECT DISTINCT instead, as it simply removes duplicate rows from the result set without altering the data.

// Using SELECT DISTINCT to query data from a MySQL database in PHP
$query = "SELECT DISTINCT column_name FROM table_name";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo $row['column_name'] . "<br>";
    }
} else {
    echo "No results found.";
}