I'd like to extract posts from Stack Overflow using Data.SE, that contain code in them. I am mainly interested in the posts that come from the tag [artificial-intelligence]. Here is the code that I wrote to do that:

select Body,PostId, Tags as 'PostTag',TagName, Posts.AcceptedAnswerId, Posts.Title
from PostTags
left join Posts
on PostTags.PostId = Posts.Id
left join Tags
on PostTags.TagId = Tags.Id
where TagName in (select 
from tags t
left join Posts e
  on t.ExcerptPostId = e.Id
left join Posts w
on t.WikiPostId = w.Id
where t.id in (3615))
and Posts.AcceptedAnswerId IS NOT NULL

However, many of these posts have no code in them. Here is my attempt to filter posts with only code. I added the following line to the query above:

and body like '%<code>%'

Does this attempt seem correct?

1 Answer 1


I guess that would be correct, but it's easier to just specify the TagName, like this query.

Note that answers won't end up in this query (or yours) since they're not directly linked to tags, only through their parent question.

The Body column contains the rendered HTML of the post, and <code> is used to display, you guess it, code. Not just inline code; code blocks are rendered with an additional <pre> element around them.

  • I am curious what does the term "<code>" do here? I am familiar with the LIKE operator as well as the use of '% %'. However, I don't understand how '%<code>%' works. If this seems like a separate question, I could post one. Dec 15, 2022 at 13:55
  • 1
    @desert_ranger that's HTML - I updated my answer with a little clarification.
    – Glorfindel Mod
    Dec 15, 2022 at 13:59
  • We have done this before, right? meta.stackexchange.com/a/379052
    – rene
    Dec 15, 2022 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .