Receive a badge for a post into a topic containing specific tag

Side note that this query will only return the latest post that qualifies. If you have a badge that can be awarded multiple times, use the following version:

SELECT p.user_id, p.created_at granted_at, p.id post_id
FROM badge_posts p
JOIN topics t ON t.id = p.topic_id
JOIN topic_tags tt on t.id = tt.topic_id
JOIN tags on tags.id = tt.tag_id
AND tags.name LIKE 'featured'
and (:backfill OR ( p.id IN (:post_ids) ))
AND p.post_number = 1
2 Likes