Используйте GROUP BY
, ORDER BY
и LIMIT
, если хотите получить один результат (даже при наличии связей):
select name -- , count(*) you don't need the count(*) here, but I would keep it
from t
where country = 'USA'
group by name
order by count(*) desc
limit 1;
Если вам нужны все строки при наличии связей, используйте окно функции:
select name
from (select name, count(*) as cnt,
rank() over (partition by country order by count(*) desc) as seqnum
from t
where country = 'USA'
group by name
) t
where seqnum = 1;