# Finding 'Top X' users with the most badges

**URL:** https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042
**Category:** Data & reporting
**Tags:** badges, sql-query
**Created:** [December 19, 2018, 4:00pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042 "2018-12-19T16:00:08Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![SidV](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sidv/32/119460_2.png) [@SidV](https://meta.discourse.org/u/SidV)
#### Post date: [December 19, 2018, 5:14pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/2 "2018-12-19T17:14:30Z")

</div>

### Users ordered by badge count

> [@Richie](#):
>
> Has anyone done something like this already?

Test this:

```
-- [params]
-- int :posts = 100
-- int :top = 10
SELECT u.username, count(ub.id) as "Badges"
FROM user_badges ub, users u, user_stats us
WHERE u.id = ub.user_id
AND u.id = us.user_id
AND us.post_count > :posts
AND (u.admin = 'f' AND u.moderator = 'f')
GROUP BY u.username
ORDER BY count(ub.id) desc
LIMIT :top

```

---

_[View the full topic](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042)._
