Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - calculate MODE without sorting

Tags:

sql

mysql

I would like to calculate the MODE of a single column in SQL. This is done easily enough with:

SELECT v AS Mode  
FROM Data  
GROUP BY v HAVING COUNT(*) >= ALL (SELECT COUNT(*) FROM Data GROUP BY v);

However, I would like to do this without sorting, i.e. without using GROUP BY or any similar construct. Is there a quick and easy way to do this?

like image 697
user6269144 Avatar asked Apr 24 '26 13:04

user6269144


1 Answers

group by doesn't do sorting. It does partitioning. So instead of 1 aggregate result, you get 1 result per group in which all values that you group by are the same.

like image 174
grovkin Avatar answered Apr 27 '26 03:04

grovkin