# What is SQL Badge Query for invited \>25 People?

**URL:** https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451
**Category:** Support
**Created:** [March 21, 2018, 3:22am UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451 "2018-03-21T03:22:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![iidbae](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@iidbae](https://meta.discourse.org/u/iidbae)
#### Post date: [March 21, 2018, 3:22am UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451/1 "2018-03-21T03:22:44Z")

</div>

Hi, this is follow up my previous question from [Locked query field for system badges - #5 by iidbae](https://meta.discourse.org/t/copy-paste-locked-badge-query-sql-for-more-than-champion-or-invitation/83415/5)

So, I want to give a badge to a member who has successful invited 25 people.  
Just like the existing badge, the Campaigner (3 people) or Champion (5 people).  
The reward is only a badge without raising the Trust Level.

The concept is similar with the SQL Badge Query in “A badge for 10 accepted answers” [Discourse Solved](https://meta.discourse.org/t/discourse-solved-accepted-answer-plugin/30155) Only need changes the number.

I have see this ["Promoter" Badge broken?](https://meta.discourse.org/t/promoter-badge-broken/34413) and this [discourse/lib/badge\_queries.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/master/lib/badge_queries.rb) , but still afraid and confuse to found the correct sql.  
Maybe someone can help me.  
Thank you very much.

---

<div class="post-metadata">

### Author: ![schleifer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schleifer/32/86416_2.png) [@schleifer](https://meta.discourse.org/u/schleifer)
#### Post date: [March 21, 2018, 4:13pm UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451/2 "2018-03-21T16:13:43Z")

</div>

The specific query for each invitation badge is generated with two variables:

- How many people do they have to invite?
- What TL do those people have to reach?

The answers for the existing badges are:

| Badge | People | TL |
| --- | --- | --- |
| Promoter | 1 | 0 |
| Campaigner | 3 | 1 |
| Champion | 5 | 2 |

So for your new badge, take the SQL in the invite\_badge method at [discourse/lib/badge\_queries.rb at 43f63c435d0098ffb0bc66c5544e026099818310 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/43f63c435d0098ffb0bc66c5544e026099818310/lib/badge_queries.rb#L136) and replace `#{count.to_i}` with the number of people and `#{trust_level.to_i}` with the TL.

---

<div class="post-metadata">

### Author: ![iidbae](https://avatars.discourse-cdn.com/v4/letter/i/a183cd/32.png) [@iidbae](https://meta.discourse.org/u/iidbae)
#### Post date: [March 21, 2018, 11:58pm UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451/3 "2018-03-21T23:58:56Z")

</div>

Hi Andrew, thank you for your simple and easy explanation.  
I am really appreciate that.

I just want to confirm if the below query is right:

```plaintext
      SELECT u.id user_id, current_timestamp granted_at
      FROM users u
      WHERE u.id IN (
        SELECT invited_by_id
        FROM invites i
        JOIN users u2 ON u2.id = i.user_id
        WHERE i.deleted_at IS NULL AND u2.active AND u2.trust_level >= 2 AND u2.silenced_till IS NULL
        GROUP BY invited_by_id
        HAVING COUNT(*) >= 25
      ) AND u.active AND u.silenced_till IS NULL AND u.id > 0 AND
        (:backfill OR u.id IN (:user_ids) )

```

So the query will automatically give a badge to a member who has successful invite 25 people when the TL is 2. Am I right?

Thank you very much. 🙏

---

<div class="post-metadata">

### Author: ![schleifer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schleifer/32/86416_2.png) [@schleifer](https://meta.discourse.org/u/schleifer)
#### Post date: [March 22, 2018, 6:58pm UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451/4 "2018-03-22T18:58:56Z")

</div>

> [@iidbae](#):
>
> So the query will automatically give a badge to a member who has successful invite 25 people when the TL is 2. Am I right?

Yes.

&nbsp;&nbsp;&nbsp;

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/what-is-sql-badge-query-for-invited-25-people/83451/5 "2024-06-08T12:37:42Z")

</div>

This topic was automatically closed after 2271 days. New replies are no longer allowed.
