# 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:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Richie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richie/32/115110_2.png) [@Richie](https://meta.discourse.org/u/Richie)
#### Post date: [December 19, 2018, 4:00pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/1 "2018-12-19T16:00:08Z")

</div>

Has anyone come up with some SQL to display a list of users (top 10 maybe), ordered by the total number of Badges they have?

I’ve had a poke around in the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) and looked at the “user\_badges” table and can see there is a “user\_id” column and “badge\_id” column but I’m not skilled enough in SQL to be able to run some kind of count or join query to produce a top ten list.

Has anyone done something like this already?

---

<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

```

---

<div class="post-metadata">

### Author: ![Richie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richie/32/115110_2.png) [@Richie](https://meta.discourse.org/u/Richie)
#### Post date: [December 20, 2018, 8:33pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/3 "2018-12-20T20:33:43Z")

</div>

Hi @SidV 👋🏻

This works quite well, thanks!

However it’s not quite accurate…

Here’s what I get:

 ![37](https://global.discourse-cdn.com/meta/original/3X/1/9/19cc0ff58421cd9c38469083708f0665d7f1c09c.png)

Yet hovering over a few of those users shows different numbers.

Memento has 44 (not 48):

 ![40](https://global.discourse-cdn.com/meta/original/3X/b/4/b43ca3a983fe9ed4342a2bb602e7dc7fbd5383af.png)

Brian has 33 (not 35):

 ![55](https://global.discourse-cdn.com/meta/original/3X/4/0/40369264e7d893d678f5a5fbd22f539b082e871b.png)

Yet Paul does actually have 34!

 ![21](https://global.discourse-cdn.com/meta/original/3X/1/7/17761e0d09130f1e062f63e233ddc9d94cb4ed8b.png)

If I remove the line that’s excluding admins, it goes a little haywire:

 ![42](https://global.discourse-cdn.com/meta/original/3X/9/b/9b690117ae86257e95232e2986b6d7c014083070.png)

Ping has 52 (not 179):

 ![53](https://global.discourse-cdn.com/meta/original/3X/1/4/14dd938d20385260b84e73dca38f00a2af8919b3.png)

But Ozone does indeed have 47:

 ![30](https://global.discourse-cdn.com/meta/original/3X/c/4/c48c30497a3e1e0f9c7c9ed91e9f5448bfb50c61.png)

Any idea what might be causing these inaccuracies @SidV ?

Thanks once again for your help on this one 😃

---

<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 21, 2018, 12:02pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/4 "2018-12-21T12:02:26Z")

</div>

Yes. A lot of badges have the option to win more that one time. 🤔

Check this if it more accurate:

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

```

---

<div class="post-metadata">

### Author: ![Richie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richie/32/115110_2.png) [@Richie](https://meta.discourse.org/u/Richie)
#### Post date: [December 23, 2018, 2:45pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/5 "2018-12-23T14:45:15Z")

</div>

> [@SidV](#):
>
> A lot of badges have the option to win more that one time

D’oh! 🤦🏻‍♂️

Yes, of course 😊

However, your updated reply is actually _exactly_ what I need 👏🏻

Thanks ever so much @SidV 🙇‍♂️

---

<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 23, 2018, 6:06pm UTC](https://meta.discourse.org/t/finding-top-x-users-with-the-most-badges/275042/6 "2018-12-23T18:06:46Z")

</div>

I’m glad to help !

**[Query’s list updated](https://github.com/SidVal/discourse-data-explorer/blob/queries/querys.md)** 🚀

Have a great xmas! 🎄
