C Amr Moneim C Amr Moneim - 2 months ago 6
SQL Question

How to select most Name frequent from table

I have table have a lot of Names like this

MrMiroBear
MrMiroBear
BigBear
MrMiroBear
BigBear
MrMiroBear
MrMiroBear
BigBear
MrMiroBear
MrMiroBear
BigBear
MrMiroBear
BigBear
BigBear
MrMiroBear
BigBear
BigBear
MrMiroBear
BigBear


I want select most Name frequent in this table how I can do this?
And I want give me the name only like (BigBear) not number of count

And I try this Query but it's give me only count of name not name

select Top 1 Count(*) as TopName From _Client group by ClientName

Answer

If your query works, you are not using MySQL.

The answer to your question would then be:

select Top 1 ClientName
From _Client
group by ClientName
order by count(*) desc;

In other words, you have to select the field that you want.

Note: There could be ties. If you want all top client names:

select Top (1) With Ties ClientName
From _Client
group by ClientName
order by count(*) desc;