my $sth = $dbh->prepare("SELECT DISTINCT country, COUNT(*) FROM contacts WHERE country != '' GROUP BY country ORDER BY 2 DESC"); $sth->execute() or die $sth->errstr; while (my @result = $sth->fetchrow_array()) { print qq($result[0], $result[1]); } $sth->finish();