# How do I make my own badge query?

**URL:** https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [01.Октябрь.2016 09:11:49 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946 "2016-10-01T09:11:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![keith1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/keith1/32/178982_2.png) [@keith1](https://meta.discourse.org/u/keith1)
#### Post date: [01.Октябрь.2016 09:11:49 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/1 "2016-10-01T09:11:49Z")

</div>

I’m feeling quite silly for having to ask, but I’m finding tons of great resources for making my own badges, but I can’t figure out where exactly to put all of these badge queries. Can someone just point me in the right direction?

Thanks!  
Keith

---

<div class="post-metadata">

### Author: ![Pad\_Pors](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pad_pors/32/52016_2.png) [@Pad\_Pors](https://meta.discourse.org/u/Pad_Pors)
#### Post date: [01.Октябрь.2016 09:35:28 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/2 "2016-10-01T09:35:28Z")

</div>

there is a topic describing other badges:

> [@\[Superseded\] What cool badge queries have you come up with?](https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978):
>
> warning Enabling Badge SQL entails security and performance risks so it is not available by default. For more information, see: [Enable Badge SQL](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894) This topic became too long to be easily searched and has been broken down into separate topics. If you have any questions about how to write a badge query, or have one to share, please start a new topic in the #data & reporting category and tag it with the #sql-triggered-badge tag. +1 More ideas: [Some common badge queries](https://meta.discourse.org/t/some-common-badge-queries-idea/31859)

the faq topic for badge (which may not be what you want):

> [@Understanding and using badges](https://meta.discourse.org/t/what-are-badges/32540):
>
> bookmark This guide explains what badges are in Discourse, how they work, and how to use them effectively. person_raising_hand Required user level: All users Badges in Discourse are a key feature that reinforces positive user behavior and facilitates “learning by doing”. They are an integral part of Discourse’s gamification features, designed to encourage and reward user engagement. Summary This guide covers: What badges are and how they work How to receive badges Types and groups of…

creating badge:

> [@Grant a badge to individual users manually](https://meta.discourse.org/t/grant-a-badge-to-individual-users-manually/29426):
>
> bookmark This is a guide for manually granting a custom badge to individual users on Discourse. person_raising_hand Required user level: Administrator In this guide, you’ll learn how to create and manually grant a custom badge to individual users. A new “Scholar” badge will be created as an example. warning This guide is only applicable for custom badges. To manually grant one of Discourse’s built in badges, you will need to follow: [Bulk Award a badge to a set of users](https://meta.discourse.org/t/award-a-badge-to-a-set-of-users/138694)Create a …

---

<div class="post-metadata">

### Author: ![keith1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/keith1/32/178982_2.png) [@keith1](https://meta.discourse.org/u/keith1)
#### Post date: [01.Октябрь.2016 15:36:20 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/3 "2016-10-01T15:36:20Z")

</div>

Yes, like I said, I’ve found loads of great resources on here for customizing my own badges, etc (I guess I should have linked them to prevent this answer), but on a very basic level, I can’t find any information of where to put the queries. Let me be more specific:

```
SELECT user_id, 0 post_id, current_timestamp granted_at 
FROM badge_posts  
WHERE (:backfill OR user_id IN (:user_ids) OR 0 NOT IN (:post_ids) )
GROUP BY user_id 
HAVING count(*) > 1000

```

Where does this code belong? Is there an admin section for badge query modifications that I’m not finding, or is this offline/external file editing, in which case, can you just point me to where in the discourse directories these live? None of the topics on here about badge creation actually mention this bit, so I’m assuming it’s something entirely obvious that I’m missing.

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [01.Октябрь.2016 16:17:44 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/4 "2016-10-01T16:17:44Z")

</div>

Are you on latest?

If you are, read this before

> [@Enable Badge SQL](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894):
>
> bookmark This guide explains how to enable Badge SQL in Discourse and discusses the associated security and performance risks. person_raising_hand Required user level: Administrator warning Enabling Badge SQL can pose security and performance risks. To maintain security and performance integrity, as of Discourse 1.6, the ability for administrators to edit badge SQL directly is disabled by default. The two primary concerns around enabling Badge SQL are: Security: Allowing direct SQL …

After you enabled it, you will find a new SQL field on `/admin/badges/new`

---

<div class="post-metadata">

### Author: ![keith1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/keith1/32/178982_2.png) [@keith1](https://meta.discourse.org/u/keith1)
#### Post date: [01.Октябрь.2016 17:18:20 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/5 "2016-10-01T17:18:20Z")

</div>

Aha! That was the part I was missing. Thank you for connecting those dots!

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [10.Октябрь.2016 13:42:52 UTC](https://meta.discourse.org/t/how-do-i-make-my-own-badge-query/50946/6 "2016-10-10T13:42:52Z")

</div>


