my $sql = 'SELECT A.code FROM othertable as A LEFT JOIN mytable AS B ON B.state = A.state WHERE A.year=? AND B.city=?'; my $codedata = $dbh->selectall_arrayref( $sql, undef, 2014, $city );