Professional Documents
Culture Documents
### 0. List all the users who have at least 1000 UpVotes.
queries[0] = """
from users
order by Id asc;
"""
### 1. List the posts (Id, Title, Tags) for all posts that are tagged 'postgres
### Hint: use ``like'' -- note that tags are enclosed in '<>' in the Tags field
### Output column order: Id, Title, Tags
queries[1] = """
from posts
order by Id asc;
"""
### 2. Write a query to output the number of years users have been on the
queries[2] = """
"""
### 3. Select all the "distinct" years that users with names starting with 'M'
queries[3] = """
from users
as Years
"""
### 4. Write a query to find users who have, on average, given at least 1 UpVot
### day they have been on the platform as of September 1, 2022
### Hint: Use subtraction on "date" to get number of days between two dates.
### Count only full days (i.e., someone who joined 1.5 days ago only needs to
queries[4] = """
with temp as
from temp
order by Id asc
"""
### 5. Write a single query to report all Badges for the users with reputation
### Output Column: Id (from Users), DisplayName, Name (from Badges), Reputation
### Order by: Id increasing
queries[5] = """
order by Id asc;
"""
### 6. Write a query to find all Posts who satisfy one of the following conditi
### - the post title contains 'postgres' and the number of views is at l
### - the post title contains 'mongodb' and the number of views is at le
### The match should be case insensitive
queries[6] = """
from posts
"""
### 7. Count the number of the Comments made by the user with DisplayName 'JHFB
### Output columns: Num_Comments
queries[7] = """
from
(select users.id
"""
### 8. Find the Users who have received badges with names: "Guru" and "Curious"
### Only report a user once even if they have received multiple badges with the
### Hint: Use Intersect.
queries[8] = """
(SELECT userid
FROM badges
INTERSECT
SELECT userid
FROM badges
"""
### 9. "Tags" field in Posts lists out the tags associated with the post in the
### Find the Posts with at least 6 tags, with one of the tags being postgresql
### Hint: use "string_to_array" and "cardinality" functions.
queries[9] = """
from posts
"""
### 10. SQL "with" clause can be used to simplify queries. It essentially allow
### specifying temporary tables to be used during the rest of the query. See Se
### 3.8.6 (6th Edition) for some examples.
###
### Write a query to find the name(s) of the user(s) with the largest number of
### We have provided a part of the query to build a temporary table.
###
queries[10] = """
with temp as (
select * from temp where num_Comments = (select num_Comments from temp LIMIT 1)
order by Id asc;
"""
### 11. List the users who posted no comments and with at least 500 views.
# Use temp to get a list of user ids, call them temp_id, that have made comment
# select id and display name for users where the userid is not in temp_id, mean
queries[11] = """
with temp as (
from Users
where (views >= 500) AND (Id NOT IN (select DISTINCT temp_id from temp))
order by Id asc;
"""
### 12. Write a query to output a list of posts with comments, such that PostTy
### and the comment has score of at least 10. So there may be multiple rows wit
### in the output.
# want posttypeid = 6
queries[12] = """
with temp as (
from temp
where temp_pti = 6
order by Id asc;
"""
### badges, and the number of users who received those badges.
### Note: A user may receive the same badge multiple times -- they should only
### Output columns: Badge_name, Num_users
queries[13] = """
with unique_badges as (
select DISTINCT name as names, userid as userids from badges order by names
),
"""
### 14. For each post, count the number of comments for that post.
###
###
### However, this takes too long, even on the relatively small database we
### have.
###
###
queries[14] = """
GROUP BY posts.id
order by Id asc;
"""
### of users with reputation between 1 and 100 (inclusive). If a particular rep
### score does not have any users (e.g., 2), then that reputation should appear
### 0 count.
###
### This is what's called a "set returning function", and the result can be use
### See: https://www.postgresql.org/docs/12/functions-srf.html
###
queries[15] = """
with temp as
(SELECT Reputation, id
FROM users
ORDER BY Reputation),
temp2 as
FROM temp
GROUP BY Reputation
ORDER BY Reputation asc),
indices as
temp3 as
FROM indices
ON indices.mySeries = temp2.Reputation
SELECT Reputation,
CASE
END AS Num_Users
FROM temp3
"""
### 16. Generalizing #14 above, associate posts with both the number of
###
### As above, using scalar subqueries won't scale to the number of tuples.
###
queries[16] = """
with temp as
GROUP BY posts.id
order by Id asc),
GROUP BY posts.id
on temp.Id = temp2.Id2
order by Id asc;
"""
### 17. Write a query to find the posts with at least 7 children (i.e., at
###
queries[17] = """
with parents as
from posts
where posttypeid = 1
),
children as
from posts
families as
order by Id asc
from families
### 18. Find posts such that, between the post and its children (i.e., answers
###
### HINT: Use "union all" to create an appropriate temp table using WITH
###
# temp is a table with a list of ids of posts that are answers and questions i.
queries[18] = """
with temp as
UNION ALL
),
temp2 as
),
temp3 as
order by Id asc
),
temp4 as
(select Id
FROM temp4
ON temp4.Id = posts.id;
"""
### 19. Write a query to find posts where the post and the accepted answer
### are both owned by the same user (i.e., have the same "OwnerUserId") and the
### user has not made any other post (outside of those two).
###
###
queries[19] = """
with temp as
ON users.id = posts.owneruserid
tempfreq as
ON users.id = posts.owneruserid),
UsersPosts as
from tempfreq
GROUP BY UserId
order by Frequency),
temp2 as
ON users.id = posts.owneruserid
temp3 as
ON temp.answerid = temp2.postid
ORDER BY Id asc
),
temp4 as
ON UsersPosts.UserId = temp3.Id
WHERE UsersPosts.Frequency =2
where Id = answererId
Order by Id asc
"""