cos nik
cos nik

Reputation: 386

Find the 5 most popular values mysql

i have a database column called 'country_name' where it takes the country names of my shop buyers. How can i get the 5 most popular country names order by the frequency of appearing in my db? MOST POPULAR for me is how often that country names appears in my database. for example if i have the countries below:

Brazil
Nigeria
Nigeria
Chile
Chile
Chile
Norway
Italy  

i want to get 1.Chile (cause it appears 3 times), 2.Nigeria (2 times), 3.Norway (1 time), 4.Italy, 5.Brazil

Upvotes: 2

Views: 91

Answers (1)

DZDomi
DZDomi

Reputation: 1679

Use following code:

SELECT country_name, count(*) as entries
FROM table
GROUP BY country_name
ORDER BY entries DESC
LIMIT 5

Upvotes: 4

Related Questions