# How do I write a query that removes one badge while awarding another?

**URL:** <https://meta.discourse.org/t/how-do-i-write-a-query-that-removes-one-badge-while-awarding-another/191850>\
**Category:** Data & reporting\
**Tags:** sql-triggered-badge\
**Created:** [2016年九月19日 23:32 UTC](https://meta.discourse.org/t/how-do-i-write-a-query-that-removes-one-badge-while-awarding-another/191850 "2016-09-19T23:32:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexTheRota](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@AlexTheRota](https://meta.discourse.org/u/AlexTheRota)\
**Post date:** [2016年九月19日 23:32 UTC](https://meta.discourse.org/t/how-do-i-write-a-query-that-removes-one-badge-while-awarding-another/191850/1 "2016-09-19T23:32:29Z")

</div>

Hello,

After some googles and on-page-searches I was unable to find anything about custom badge series. What I mean by this is a series of two or more badges that contain SQL that removes the previous badge in the series and adds the current badge.

So for example _I have unlocked “ **Custom Badge 1** ” by posting 100 posts, and on my 200th, I unlock “ **Custom Badge 2** ” which for business reasons should not share badge space with the previous badge, the “Custom Badge 1” would be programmatically removed from this user._

TL:DR; How to write a query that removes a badge in such a way to run concurrently with a badge awarding query?

---

<div class="post-metadata">

**Author:** ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)\
**Post date:** [2016年九月20日 00:13 UTC](https://meta.discourse.org/t/how-do-i-write-a-query-that-removes-one-badge-while-awarding-another/191850/2 "2016-09-20T00:13:47Z")

</div>

I think it should be possible if the query uses the `user_badges` table likely using these fields in some manner.

```plaintext
badge_id integer 
user_id integer fkey users 
granted_at timestamp 

```

As long as “run revocation query daily” is enabled you could have the “first level” badge get revoked if the “next level” badge had been granted.

There may be a brief time between the grant and the revoke where a member might have both, but it would resolve itself within a day at most.

I recommend that you become more familiar with the Discourse database tables before you attempt crafting badge queries. They can be a “foot gun” and there is good reason it is slightly more work to get the interface functional.
