Q&A for professionals and students of engineering
WITH SortedComments AS ( SELECT PostId, Text AS Comment, CreationDate, ROW_NUMBER() OVER (PARTITION BY PostId ORDER BY CreationDate DESC) AS RN FROM Comments WHERE PostId in (74996040,39033194) ) SELECT p.Id, sc.Comment FROM Posts p LEFT JOIN SortedComments sc ON p.Id = sc.PostId WHERE ISNULL(sc.RN,0) <= 1 and p.id in (7978932,74996040,39033194)