0

I have events table, now I want to display the most frequent element in a column using SQL

Here is the event table.

id  |  selectedcountries
0       | Tanzania
1       | Tanzania
2       | Tanzania
3       | Kenya
4       | Uganda
5       | Uganda
6       | Kenya
7       | Uganda
8       | Tanzania
8       | Poland
9       | Poland
10      | Tanzania

UPDATE

For example, for this table it should return Tanzania since it is the most frequent value:

Here is my solution

SELECT  selectedcountries, COUNT(  'selectedcountries' ) AS  'country'
FROM EVENTS
GROUP BY  'selectedcountries'
ORDER BY  'country' DESC 

Unfortunately, I am getting the following

selectedcountries country
                      73

enter image description here

What do I need to do to get what I want?

3
  • I think the only problem is the quotation marks in your GROUP BY clause. Commented Aug 6, 2019 at 11:59
  • What is the version of MySql? Commented Aug 6, 2019 at 12:01
  • @forpas Apache/2.4.27 (Amazon) PHP/5.6.32 Commented Aug 6, 2019 at 12:05

2 Answers 2

1

This is called the mode in statistics.

You can use group by and limit if you want a single value:

select selectedcountries
from events
group by selectedcountries
order by count(*) desc
limit 1;

Here is a db<>fiddle.

This does not return multiple values when there are ties. One way to get all values for ties is two levels of aggregation:

select group_concat(selectedcountries)
from (select selectedcountries, count(*) as cnt
      from events
      group by selectedcountries
     ) t
group by cnt
order by cnt desc
limit 1;
Sign up to request clarification or add additional context in comments.

6 Comments

@Strawberry your right, this does not work at all, return empty
@user9964622 . . . The code does work. That is not what Strawberry is referring to. Given your sample queries, you are probably confused by how to use quotes in SQL queries.
@GordonLinoff i tested just returns empty column
@user9964622 . . . Then the empty value has the highest count. I added a db<>fiddle as a demonstration.
Your right, its working , I have one more question assume I have other multiple null values inside column selectedcountry I dont want these null or empty values to be counted, what do I need to add in that sql statement your provided in a fiddle?
|
1

Try this-

SELECT  selectedcountries, 
COUNT(selectedcountries) AS  'country'
FROM EVENTS 
WHERE selectedcountries <> ''
AND selectedcountries IS NOT NULL
GROUP BY  selectedcountries
ORDER BY  COUNT(selectedcountries) DESC 

5 Comments

check fiddle - dbfiddle.uk/…
You can apply limit 1 if you need the first row only.
aah got you, for example if I have empty column and I dont want them to be counted what do I need to add in my statement?
Add WHERE selectedcountries <> '' before GROUP BY. Check updated script.
Added both Empty and NULL checking. This should work for all case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.