Shortest posts on a Stack Exchange site by body length with username of original poster (SEDE)

20 Views Asked by At

So I recently have been working on a SEDE query that outputs posts from a given Stack Exchange site that have bodies of less than 50 characters.

While I have figured out how to get the links to those posts, I would also like to get the usernames under which those questions were posted.

Here is my current code:

SELECT p.Id AS [Post Link], Len(Body) AS [Body Length], p.Score, p.ViewCount, p.Tags
FROM Posts p
WHERE Len(Body) < 50
AND p.PostTypeId = 1
ORDER BY Len(Body) DESC

What I have tried to do to get my desired result:

  • While p.OwnerUserId does produce a new column, it only produces the given user IDs, while I would like the username specifically.
  • However, if I change p.OwnerUserId to p.OwnerDisplayName, this only produces a blank column with nothing in it.
  • It also seems like if I try to call u.OwnerDisplayName, it produces the error

    Line 1: The multi-part identifier "u.OwnerDisplayName" could not be bound.

My question is: How can I also get the username of the original poster of the questions into its own column?


Revisions that include previous attempts (if it would be helpful to look at):

1

There are 1 best solutions below

1
Ian Kemp On BEST ANSWER

You just need to join to the Users table:

SELECT p.Id          AS [Post Link]
     , u.DisplayName
     , len(p.Body)   AS [Body Length]
     , p.Score
     , p.ViewCount
     , p.Tags
  FROM Posts p
           join
       Users u on u.Id = p.OwnerUserId
 WHERE len(p.Body)  < 50
   AND p.PostTypeId = 1
 ORDER BY len(p.Body) DESC