SELECT t.id, t.headline, t.posted_at, r.id r.posted_at, CASE WHEN r.posted_at > t.posted_at THEN r.posted_at ELSE t.posted_at END AS LastPostedAt FROM threads AS t JOIN replies AS r ON r.thread_id = t.id ORDER BY CASE WHEN r.posted_at > t.posted_at THEN r.posted_at ELSE t.posted_at END DESC LIMIT 100