Vatsal Sharma
Vatsal Sharma

Reputation: 11

Find the most frequent value and order by it

+-----------------------+------------------------+
| being_followed        | follower               |
+-----------------------+------------------------+
| Bob Dylan             |                      B |
| Bob Dylan             |                      A |
| Sam Cooke             |                      X |
| The Beatles           |                      Y |
| Bob Dylan             |                      M |
| Sam Cooke             |                      N |
+-----------------------+------------------------+

Now, I want to find which is the most occurring value in being_followed and then order by it. It should look somewhat like -

Bob Dylan - 3
Sam Cooke - 2
The Beatles - 1

Please don't mark this as a duplicate.

Upvotes: 0

Views: 92

Answers (6)

Perhaps you can try this:

select being_followed, count(*) follower
from TableName
group by being_followed
order by follower desc

It's working fine.

Upvotes: 0

Vipin Jain
Vipin Jain

Reputation: 3756

Try this

SELECT being_followed,COUNT(1) count_followers
FROM table
GROUP BY being_followed
ORDER BY COUNT(1) DESC;

Get count of being_followed and also order by high to low bases(descending order)

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

Try this:

SELECT being_followed,COUNT(*) AS follower 
FROM tablename GROUP BY being_followed ORDER BY follower DESC;

Output:

+-----------------------+------------------------+
| being_followed        | follower               |
+-----------------------+------------------------+
| Bob Dylan             |                      3 |
| Sam Cooke             |                      2 |
| The Beatles           |                      1 |
+-----------------------+------------------------+

Upvotes: 0

jitendra joshi
jitendra joshi

Reputation: 687

Try This:-

select being_followed,count(*) total_followers
from table
group by being_followed
order by total_followers desc

Upvotes: 0

jilesh
jilesh

Reputation: 436

SELECT being_followed , count(being_followed )as counter FROM `table_Name` GROUP BY being_followed ORDER BY counter DESC

You will get result what you want.Here using group by you will get unique value and using count you will get counter of same being_followed

Upvotes: 0

minatverma
minatverma

Reputation: 1099

Try below :

select being_followed , count(1) as count
from table
group by being_followed 
order by count desc ;

Upvotes: 1

Related Questions