# Linking badge sql to a topic id?

**URL:** https://meta.discourse.org/t/linking-badge-sql-to-a-topic-id/404943
**Category:** Support
**Tags:** sql-triggered-badge
**Created:** [June 10, 2026, 12:54pm UTC](https://meta.discourse.org/t/linking-badge-sql-to-a-topic-id/404943 "2026-06-10T12:54:20Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![chapoi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chapoi/32/537252_2.png) [@chapoi](https://meta.discourse.org/u/chapoi)
#### Post date: [June 10, 2026, 1:24pm UTC](https://meta.discourse.org/t/linking-badge-sql-to-a-topic-id/404943/2 "2026-06-10T13:24:52Z")

</div>

I think you can:

```plaintext
SELECT
  u.id AS user_id,
  CURRENT_TIMESTAMP AS granted_at,
  (SELECT id FROM posts WHERE topic_id = YOUR_TOPIC_ID AND post_number = 1) AS post_id
FROM users u
INNER JOIN group_users gu ON gu.user_id = u.id
INNER JOIN groups g ON g.id = gu.group_id
WHERE g.name = 'beta-testers'

```

Replace `YOUR_TOPIC_ID `

And it has to have `Query targets posts` ticked I think.

Lmk if that works.

---

_[View the full topic](https://meta.discourse.org/t/linking-badge-sql-to-a-topic-id/404943)._
