163

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
  • 75,001
  • 122
  • 434
  • 781
Jake
  • 3,326
  • 7
  • 39
  • 59

11 Answers11

259
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
  • 401
  • 3
  • 15
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
64

Try something like:

SELECT       `column`
    FROM     `your_table`
    GROUP BY `column`
    ORDER BY COUNT(*) DESC
    LIMIT    1;
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 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
32

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,931
  • 2
  • 22
  • 37
naveen
  • 321
  • 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
17

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
  • 2,152
  • 1
  • 19
  • 32
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
  • 732,580
  • 175
  • 1,330
  • 1,459
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
  • 132,198
  • 53
  • 198
  • 213
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
  • 13,704
  • 3
  • 43
  • 81
Mayur Mane
  • 13
  • 1
  • 1
  • 7
0

Return all most frequent rows in case of tie

Find the most frequent value in mysql,display all in case of a tie gives two possible approaches:

  1. Scalar subquery:

    SELECT
      "country",
      COUNT(country) AS "cnt"
    FROM "Sales"
    GROUP BY "country"
    HAVING
      COUNT("country") = (
        SELECT COUNT("country") AS "cnt"
        FROM "Sales"
        GROUP BY "country"
        ORDER BY "cnt" DESC,
        LIMIT 1
      )
    ORDER BY "country" ASC
    
  2. With the RANK window function, available since MySQL 8+:

    SELECT "country", "cnt"
      FROM (
        SELECT
          "country",
          COUNT("country") AS "cnt",
          RANK() OVER (ORDER BY COUNT(*) DESC) "rnk"
        FROM "Sales"
        GROUP BY "country"
      ) AS "sub"
    WHERE "rnk" = 1
    ORDER BY "country" ASC
    

    This method might save a second recount compared to the first one.

    RANK works by ranking all rows, such that if two rows are at the top, both get rank 1. So it basically directly solves this type of use case.

    RANK is also available on SQLite and PostgreSQL, I think it might be SQL standard, not sure.

In the above queries I also sorted by country to have more deterministic results.

Tested on SQLite 3.34.0, PostgreSQL 14.3, GitHub upstream.

Most frequent for each GROUP BY group

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0
SELECT     TOP 20 WITH TIES COUNT(Counted_Column) AS Count, OtherColumn1, 
OtherColumn2, OtherColumn3, OtherColumn4
FROM         Table_or_View_Name
WHERE
(Date_Column >= '01/01/2023') AND 
(Date_Column <= '03/01/2023') AND 
(Counted_Column = 'Desired_Text')
GROUP BY OtherColumn1, OtherColumn2, OtherColumn3, OtherColumn4
ORDER BY COUNT(Counted_Column) DESC
  • 20 can be changed to any desired number
  • WITH TIES allows all ties in the count to be displayed
  • Date range used if date/time column exists and can be modified to search a date range as desired
  • Counted_Column 'Desired_Text' can be modified to only count certain entries in that column
  • Works in INSQL for my instance
DAGWOOD
  • 1
  • 1
-1

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
  • 2,037
  • 4
  • 8
  • 17
Omar Lari
  • 29
  • 1