my $max; while (my @row=$tmp_id->fetchrow_array){ $max = $row[2] if( not defined $max or $row[2] > $max ); }