my $duplicates= $dbh->selectall_arrayref("SELECT ID, Tag1, Tag2, COUNT(*) c FROM table GROUP BY Tag1, Tag2 HAVING c > 1"); #### 1:science:math 2:science:algebra 3:history:math 4:science:math