Q&A for speakers of other languages learning English
with source_data as ( select p.id as postid , votetypeid , count(v.id) as votecount from posts p join votes v on p.id = v.postid where votetypeid = 2 or votetypeid = 3 group by p.id, votetypeid ), top_ten as ( select top 10 * , 'upvoted' AS votetype from source_data where votetypeid = 2 order by votecount desc ), lowest_ten as ( select top 10 * , 'downvoted' AS votetype from source_data where votetypeid = 3 order by votecount desc ) select postid, votetype, votecount from top_ten union all select postid, votetype, votecount from lowest_ten