# Users who don’t have a particular badge

**URL:** https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031
**Category:** Data & reporting
**Tags:** badges, sql-query
**Created:** [17 juli 2018 om 17:35 UTC](https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031 "2018-07-17T17:35:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Waltrip1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/john_waltrip1/32/98972_2.png) [@John\_Waltrip1](https://meta.discourse.org/u/John_Waltrip1)
#### Post date: [17 juli 2018 om 17:35 UTC](https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031/1 "2018-07-17T17:35:36Z")

</div>

I’m trying to put together a query that will show users of a group that don’t have a particular badge, like Certified.

Something along the lines of "give me all the users from group VIP where badge Certified does not exist. Thank you.

---

<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: [17 juli 2018 om 19:19 UTC](https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031/2 "2018-07-17T19:19:28Z")

</div>

### Users who don’t have a particular badge

Well, let’s try this:

```sql
WITH exclude_badge AS (
SELECT gu.user_id
FROM badges b, user_badges ub, users u, group_users gu
WHERE u.id = ub.user_id
AND ub.badge_id = b.id
AND u.id = gu.user_id
AND b.name = 'Certified'
AND gu.id = 10 
)

SELECT
u.id AS user_id
FROM users u
WHERE u.id NOT IN (SELECT * FROM exclude_badge)
ORDER BY user_id
LIMIT 10

```

---

<div class="post-metadata">

### Author: ![John\_Waltrip1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/john_waltrip1/32/98972_2.png) [@John\_Waltrip1](https://meta.discourse.org/u/John_Waltrip1)
#### Post date: [17 juli 2018 om 21:35 UTC](https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031/3 "2018-07-17T21:35:13Z")

</div>

Thanks for your help Sid, I made a couple of tweaks and got it to work with the following:

```WITH
SELECT gu.user_id
FROM badges b, user_badges ub, users u, group_users gu
WHERE u.id = ub.user_id
AND ub.badge_id = b.id
AND u.id = gu.user_id
AND b.name = 'Certified'
AND gu.group_id = 42
)

SELECT
u.id AS user_id
FROM users u, group_users gu
WHERE u.id = gu.user_id
and gu.group_id = 42
and u.id NOT IN (SELECT * FROM exclude_badge)
ORDER BY user_id
LIMIT 100

```

---

<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: [17 juli 2018 om 22:36 UTC](https://meta.discourse.org/t/users-who-don-t-have-a-particular-badge/275031/4 "2018-07-17T22:36:43Z")

</div>

### Members of group who do not have a particular badge

Great! Let’s add params and update my query list 😉

Final version:

> <https://github.com/SidVal/discourse-data-explorer/blob/queries/queries/users-from-group-without-badge.sql>
