151

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:

one
two
two
three
sashoalm
  • 69,127
  • 105
  • 396
  • 720
Jake
  • 3,172
  • 6
  • 36
  • 55

9 Answers9

234
SELECT
  <column_name>,
  COUNT(<column_name>) AS `value_occurrence` 

FROM
  <my_table>

GROUP BY 
  <column_name>

ORDER BY 
  `value_occurrence` DESC

LIMIT 1;

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

Insung Park
  • 389
  • 2
  • 15
Mihai Stancu
  • 15,260
  • 2
  • 32
  • 49
61

Try something like:

SELECT       `column`
    FROM     `your_table`
    GROUP BY `column`
    ORDER BY COUNT(*) DESC
    LIMIT    1;
Mihai Stancu
  • 15,260
  • 2
  • 32
  • 49
Mat
  • 195,986
  • 40
  • 382
  • 396
  • 6
    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 02 '12 at 11:40
30

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

 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.

Simon M
  • 2,863
  • 2
  • 21
  • 37
naveen
  • 301
  • 3
  • 2
  • 1
    +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 '18 at 14:46
15

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
..
..
Swadhikar
  • 1,953
  • 1
  • 18
  • 31
3

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;
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Muneeb Hassan
  • 125
  • 12
  • 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
2

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

Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;
ρяσѕρєя K
  • 130,641
  • 51
  • 193
  • 212
0

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
Roadkill
  • 9
  • 2
0

If you have an ID column and you want to find most repetitive category from another column for each ID then you can use below query,

Table:

Table content

Query:

SELECT ID, CATEGORY, COUNT(*) AS FREQ
FROM TABLE
GROUP BY 1,2
QUALIFY ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FREQ DESC) = 1;

Result:

Query result

Chris Schaller
  • 10,266
  • 3
  • 41
  • 71
Mayur Mane
  • 13
  • 1
  • 7
-2

One way I like to use is:

select *<given_column>*,COUNT(*<given_column>*)as VAR1 from Table_Name

group by *<given_column>*

order by VAR1 desc

limit 1
RF1991
  • 1,408
  • 1
  • 6
  • 12
Omar Lari
  • 19
  • 1