Charles Yeung
Charles Yeung

Reputation: 38805

SQL - select the most frequency item

Suppose I have the table as below:

id | name
----------
01 | Tony
02 | Peter
03 | Tony
04 | Tony
05 | John
.. | ..
99 | David

How can I use SQL statement to get the most frequency of the field name(Tony)?

Upvotes: 2

Views: 7196

Answers (3)

Kevin Kreamer
Kevin Kreamer

Reputation: 184

Something along the lines of:

SELECT name
FROM table_name
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 1;

Upvotes: 6

M.R.
M.R.

Reputation: 4827

SELECT Name, COUNT(*)  
FROM YourNames 
GROUP BY Name 
ORDER BY COUNT(*) DESC 
LIMIT 1

Upvotes: 0

Aater Suleman
Aater Suleman

Reputation: 2328

What you are looking for is the Mode. This article explains how to get it (look at the last code example):

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/calculating-mean-median-and-mode-with-sq

Upvotes: 0

Related Questions