Find most frequent value in SQL column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Find most frequent value in SQL column



How can I find the most frequent value in a given column in an SQL table?



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


two


one
two
two
three





GROUP BY version: stackoverflow.com/questions/344665/get-most-common-value-in-sql
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Sep 7 '15 at 19:33




7 Answers
7


SELECT `column`,
COUNT(`column`) AS `value_occurrence`
FROM `my_table`
GROUP BY `column`
ORDER BY `value_occurrence` DESC
LIMIT 1;



Replace column and my_table. Increase 1 if you want to see the N most common values of the column.


column


my_table


1


N





what what should I do if I want to select another fields too from 'my_table'? In the other words, another value;
– grep
Jul 10 '15 at 11:59





what if more than one value appear same no of times (which is max)? In this case, if three also appeared twice? LIMIT 1 will show only one record
– mustafa1993
Jan 17 '16 at 18:44







@mustafa1993 SELECT * FROM my_table GROUP BY value ORDER BY count(*) DESC;
– Mujahed AKAS
Nov 4 '16 at 5:27


SELECT * FROM my_table GROUP BY value ORDER BY count(*) DESC;





why doesn't it work when i add WHERE 'value_occurrence' = 1 ?
– swisswiss
Jan 22 at 22:18


WHERE 'value_occurrence' = 1





@swisswiss you must use HAVING instead of WHERE in this case.
– HellBaby
Feb 21 at 15:18


HAVING


WHERE



Try something like:


SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;





I didn't know that you could use COUNT(*) directly in the ORDER BY. I knew there were a few restrictions regarding GROUP BY/HAVING and aggregate columns and I always assumed that wouldn't work.
– Mihai Stancu
Sep 2 '12 at 11:40


COUNT(*)


ORDER BY


GROUP BY


HAVING



Let us consider table name as tblperson and column name as city. I want to retrieve the most repeated city from the city column:


tblperson


city


select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)



Here nor is an alias name.


nor





+1 for using standard SQL that will work in any database (whereas LIMIT is MySQL specific, TOP is SQL Server specific).
– Dylan Smith
Jul 20 at 14:46



Below query seems to work good for me in SQL Server database:


select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC



Result:


column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..



For use with SQL Server.



As there is no limit command support in that.



Yo can use the top 1 command to find the maximum occurring value in the particular column in this case (value)


SELECT top1
`value`,
COUNT(`value`) AS `value_occurrence`
FROM
`my_table`
GROUP BY
`value`
ORDER BY
`value_occurrence` DESC;





You also need to move COUNT function to ORDER BY section to avoid getting the following error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
– Saba Jamalian
Jan 22 '16 at 21:12



Assuming Table is 'SalesLT.Customer' and the Column you are trying to figure out is 'CompanyName' and AggCompanyName is an Alias.


SalesLT.Customer


CompanyName


AggCompanyName


Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;



If you can't use LIMIT or LIMIT is not an option for your query tool. You can use "ROWNUM" instead, but you will need a sub query:


SELECT FIELD_1, ALIAS1
FROM(SELECT FIELD_1, COUNT(FIELD_1) ALIAS1
FROM TABLENAME
GROUP BY FIELD_1
ORDER BY COUNT(FIELD_1) DESC)
WHERE ROWNUM = 1





MySQL doesn't have ROWNUM
– Barmar
Dec 28 '15 at 15:53


ROWNUM





This is valid for oracle but not mysql
– Prabhu
Sep 11 '16 at 5:29





@Prabhu in MySQL, you use LIMIT 1 instead; syntax is shown in the accepted answer.
– ToolmakerSteve
Jan 12 '17 at 0:41




LIMIT 1






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS