$sql = "SELECT col_2 FROM theTable WHERE col_1 = $value_1"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute 1!" . $dbh->errstr); while ($data = $sth->fetchrow_array()) { $union{$data} = undef; } $sql = "SELECT col_2 FROM theTable WHERE col_1 = $value_2"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute 2!" . $dbh->errstr); while ($data = $sth->fetchrow_array()) { $isect{$data} = undef if exists $union{$data}; } @isect = keys %isect;