SELECT @columns, COUNT(*) FROM $table GROUP BY @columns HAVING COUNT(*) > 1 #### @key_cols1 = qw(account_id group_id); @key_cols2 = qw(account_id manager_id asset_id); # Later... $set1_sql = 'SELECT ' . join(',', @key_cols1) . 'FROM ' . $table1_name . 'WHERE ' . $table1_cond # You get the idea