С row_number()
оконной функцией:
select t.nation, t.surname
from (
select *,
row_number() over (partition by nation order by id) rn
from players
) t
where t.rn <= 4
order by t.nation, t.id
См. Демоверсию . Или, если ваша версия SQLite не поддерживает оконные функции:
select t.nation, t.surname
from (
select p.*,
(select count(*) + 1 from players where nation = p.nation and id < p.id) rn
from players p
) t
where t.rn <= 4
order by t.nation, t.id
См. demo . Результаты:
| nation | surname |
| ------ | --------- |
| fra | renoir |
| fra | dubois |
| fra | petit |
| gbr | smith |
| gbr | james |
| gbr | white |
| gbr | green |
| ger | muller |
| ger | weber |
| ger | wagner |
| ger | schmidt |
| ita | rossi |
| ita | conti |
| ita | bruni |
| rus | popov |
| rus | ivanov |
| rus | Smirnov |
| spa | Garcia |
| spa | villa |
| spa | lopez |
| spa | rodriguez |
| usa | johnson |
| usa | Armstrong |