Q&A for programmers
--1. Имена всех пользователей. --select DisplayName from Users; -- 2. Заголовок, текст и имя создателя для 100 последних созданных вопросов. --select top 100 Posts.Title, Posts.Body, Users.DisplayName from Posts, Users --where Posts.PostTypeId = 1 and Posts.OwnerUserId = Users.Id --order by Posts.CreationDate desc; --3. Количество зарегистрированных пользователей, вопросов, ответов и тэгов. --select count(Id) from Users; --select count(Id) from Posts where PostTypeId = 1; --select count(Id) from Posts where PostTypeId = 2; --select count(Id) from Tags; --4. Имена и места проживания(Location) пользователей, зарегистрированных в 2015 -- году, старше 25 лет. --select DisplayName,Location from Users --where year(CreationDate) = 2015 and Age > 25; --5. Количество пользователей, зарегистрированных в 2015 году, старше 25 лет. --select count(Id) from Users --where year(CreationDate) = 2015 and Age > 25; --6. Количество пользователей, у которых в качестве места проживания указана -- Germany. --select count(Id) from Users --where Location = 'Germany'; --7. Места проживания и количество пользователей, указавших соответствующее -- место проживания в своём профиле. --select Location, count(Id) from Users --group by Location; --8. Первых 10 пользователей с максимальной репутацией, а также количество -- вопросов, созданных ими. --select top 100 DisplayName, isNull(QuestionsCount, 0) from Users --left join (select OwnerUserId, count(Id) as QuestionsCount from Posts where PostTypeId = 1 group by OwnerUserId) YOLO on Users.Id = YOLO.OwnerUserId --order by Reputation desc --9. Всех пользователей, задавших не менее трёх вопросов с тэгом 'sql' и -- ответивших не менее чем на 10 вопросов с тэгом 'java'. --select Users.DisplayName from Users, --(select Users.Id --from Users, Posts,PostTags,Tags --where Users.Id = Posts.OwnerUserId --and Posts.PostTypeId=1 --and Posts.Id = PostTags.PostId --and PostTags.TagId = Tags.Id --and Tags.TagName = 'sql' --group by Users.Id --having count(Posts.Id)>=3 --intersect --select SWAG.Id from (select distinct Users.Id, Posts.ParentId from Users,Posts --where Users.Id = Posts.OwnerUserId --and Posts.PostTypeId=2) as SWAG , Tags, PostTags --where ParentId=PostTags.PostId --and PostTags.TagId = Tags.Id --and Tags.TagName = 'java' --group by SWAG.Id --having count(SWAG.Id)>=10) --as NeedUsers where Users.Id = NeedUsers.Id --10. Все пары пользователей, таких что каждый из двух оставил -- хотя бы один комментарий под некоторым ответом другого пользователя. --select LeftUser.Id, RightUser.Id --from Users as LeftUser, Users as RightUser, Posts as LeftPosts, Posts as RightPosts, Comments as LeftComments, Comments as RightComments --where LeftUser.Id = LeftPosts.OwnerUserId and LeftPosts.PostTypeId = 2 and LeftPosts.Id = RightComments.PostId and --RightUser.Id = RightComments.UserId --and RightUser.Id = RightPosts.OwnerUserId and RightPosts.PostTypeId = 2 and --RightPosts.Id = LeftComments.PostId and LeftUser.Id = LeftComments.UserId --and LeftUser.Id <> RightUser.Id --group by LeftUser.Id, RightUser.Id; --11. Количество вопросов, в которых выбранный ответ содержит в два или -- более раза меньше комментариев, чем некоторый другой ответ на этот же вопрос. select count(Id) from (select distinct Quests.Id from Posts as Quests, Posts as AnswerOne, Posts as AnswerTwo where Quests.PostTypeId = 1 and AnswerOne.PostTypeId = 2 and AnswerTwo.PostTypeId = 2 and AnswerOne.ParentId = Quests.Id and AnswerTwo.ParentId = Quests.Id and AnswerOne.CommentCount * 2 < AnswerTwo.CommentCount) as WATA --12. Список всех вопросов с самым комментируемым ответом на данный вопрос. --13. Найдите всех пользователей, которые либо непосредственно отвечали на вопросы -- либо оставляли комментарии(как комментарии к самому вопросу, так и комментарии к некоторому из ответов на вопрос) -- к вопросам, помеченных хотя бы 2 из 10 самых часто используемых в вопросах тэгов.