select distinct top_no from top WHERE rownum < 5 $sth = $oDbh->prepare($query); $sth->execute(); while ( my $rowUpper = $sth->fetchrow_hashref) { my $query = "select file.* where top_no = '". $rowUpper->{top_no}."'"; <> Here its not working since it is printing TOP_NO i.e. in caps } Printed with the Data::Dumper and it is printing this $VAR1 = [ { 'TOP_NO' => '072B03' } ];