# make sure our random value is not higher than # any value in the primary key my ($max) = @{$dbh->selectcol_arrayref(qq{select max(id) from table})}; my $cnt = rand($max); my $query = qq{SELECT question, answer FROM table WHERE id = $cnt };