# SQL Badge Query - Quick Responder!

**URL:** <https://meta.discourse.org/t/sql-badge-query-quick-responder/36399>\
**Category:** Data & reporting\
**Tags:** sql-triggered-badge\
**Created:** [December 7, 2015, 8:14pm UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399 "2015-12-07T20:14:04Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![ivanrlio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivanrlio/32/48497_2.png) [@ivanrlio](https://meta.discourse.org/u/ivanrlio)\
**Post date:** [December 7, 2015, 8:14pm UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399/1 "2015-12-07T20:14:04Z")

</div>

Hello again,

So I have this query here that’s aimed at giving users that respond to a new question within 4 hours a badge.  
What I have is:

```
SELECT users.id user_id, topics.id topic_id, posts.id post_id FROM users
JOIN posts ON (users.id = posts.user_id) 
JOIN topics ON (posts.topic_id = topics.
WHERE users.id NOT IN (-1, 1, 19)
AND topics.user_id != posts.user_id  
AND timestamp posts.created_at BETWEEN timestamp topics.created_at AND topic.created_at + interval
‘4 HOURS’ ORDER BY topics.id

```

I can’t seem to get it right, I keep getting 'ERROR: syntax error at or near “posts”.

Another thought that I have is:

```
SELECT users.id user_id, topics.id topic_id, posts.id post_id
FROM users 
JOIN posts ON (users.id = posts.user_id) 
JOIN topics ON (posts.topic_id = topics.id)
WHERE EXTRACT(EPOCH FROM posts.created_at) < EXTRACT(EPOCH FROM topics.created_at + INTERVAL '4 HOURS')
AND users.id NOT IN (-1, 1, 19)
AND topics.user_id != posts.user_id  
ORDER BY topics.id

```

But I’m also getting the same syntax error.

For the time-being, I’m not worried about the ‘granted\_at’ and ‘:backfill’ as I’m testing in our my postgresql database.

Any help would be appreciated!

---

_[View the full topic](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399)._
