Это должно делать то, что вам нужно, в оракуле; очевидно, что для другой базы данных вам нужно использовать источник случайных чисел. Там, вероятно, лучший способ; будем надеяться, что кто-то еще укажет нам на это: p
select question, answer, team
from
(
select question, answer, team, r
from
(
select
question,
answer,
team,
rank() over (partition by team order by dbms_random.value) r
from questions
)
where r = 1
order by dbms_random.value
) where rownum<=5;
Тестовый код:
create table questions(question varchar2(16), answer varchar2(16), team varchar2(16));
insert into questions(question, answer, team)
values ('question 1', 'answer 1', 'team A');
insert into questions(question, answer, team)
values ('question 2', 'answer 2', 'team B');
insert into questions(question, answer, team)
values ('question 3', 'answer 3', 'team B');
insert into questions(question, answer, team)
values ('question 4', 'answer 3', 'team D');
insert into questions(question, answer, team)
values ('question 5', 'answer 3', 'team A');
insert into questions(question, answer, team)
values ('question 6', 'answer 3', 'team C');
insert into questions(question, answer, team)
values ('question 7', 'answer 3', 'team F');
insert into questions(question, answer, team)
values ('question 8', 'answer 3', 'team C');
insert into questions(question, answer, team)
values ('question 9', 'answer 3', 'team G');
insert into questions(question, answer, team)
values ('question 10', 'answer 3', 'team D');
commit;