Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select statement to find ID's that occur the most

Tags:

sql

Basic SQL statement question -

I have a table (myUsers) that contains the column "UserID." The same UserID can appear one to many times in these rows. I'm looking for a query that will give me back the specific userIDs that appear the most in this table, and also with their count. Any thoughts?

Thanks in advance!

like image 786
Alex Avatar asked Dec 01 '09 16:12

Alex


2 Answers

select UserID, count(UserID)
from myUsers
group by UserID
order by count(UserID) desc
like image 144
kͩeͣmͮpͥ ͩ Avatar answered Oct 03 '22 18:10

kͩeͣmͮpͥ ͩ


DECLARE @THRESHOLD INT
SET @THRESHOLD = 20
SELECT UserID, COUNT(*)
FROM MYUSERS
GROUP BY UserID
HAVING COUNT(*) > @THRESHOLD
ORDER BY COUNT(*) DESC

EDIT: I changed from where to having, duh totally forgot about that. :)

like image 23
Wil P Avatar answered Oct 03 '22 18:10

Wil P