Skip to content

Commit af084e8

Browse files
Add files via upload
1 parent 1c279ec commit af084e8

File tree

10 files changed

+147
-0
lines changed

10 files changed

+147
-0
lines changed

Q1.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- 1. How many times does the average user post?
2+
3+
WITH User_Post_Count AS (
4+
SELECT u.id, COUNT(p.id) Post_Count
5+
FROM users u
6+
LEFT JOIN photos p
7+
ON u.id = p.user_id
8+
GROUP BY u.id
9+
)
10+
11+
SELECT AVG(Post_Count) Avg_Posts_Per_User
12+
FROM User_Post_Count
13+

Q10.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- 10.Show the username of each user along with the number
2+
-- of photos they have posted and the number of photos posted by the user
3+
-- before them and after them, based on the creation date.
4+
5+
USE ig_clone;
6+
WITH UserPhotoCounts AS (
7+
SELECT
8+
u.id,
9+
u.username,
10+
COUNT(p.id) photo_count,
11+
MIN(p.created_at) first_photo_date
12+
13+
FROM users u
14+
LEFT JOIN photos p ON u.id = p.user_id
15+
GROUP BY u.id, u.username
16+
),
17+
UserPhotoCountsPrevNext AS (
18+
SELECT
19+
upc.*,
20+
ROW_NUMBER() OVER (ORDER BY upc.first_photo_date) AS rw_number
21+
FROM UserPhotoCounts upc
22+
)
23+
24+
SELECT
25+
upc.username,
26+
upc.photo_count,
27+
LAG(upc.photo_count) OVER (ORDER BY upc.rw_number) AS prev_photo_count,
28+
LEAD(upc.photo_count) OVER (ORDER BY upc.rw_number) AS next_photo_count
29+
FROM UserPhotoCountsPrevNext upc
30+
ORDER BY upc.rw_number;

Q2.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- 2. Find the top 5 most used hashtags.
2+
USE ig_clone;
3+
SELECT tag_name, COUNT(*) tags_count
4+
FROM tags
5+
GROUP BY tag_name
6+
ORDER BY tags_count
7+
LIMIT 5
8+
9+

Q3.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- 3. Find users who have liked every single photo on the site.
2+
3+
USE ig_clone;
4+
SELECT u.id, u.username
5+
FROM users u
6+
INNER JOIN photos p
7+
ON u.id = p.user_id
8+
LEFT JOIN likes l
9+
ON p.id = l.photo_id
10+
GROUP BY u.id, u.username
11+
HAVING COUNT(DISTINCT p.id) = COUNT(l.photo_id)
12+
ORDER BY u.id

Q4.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- 4. Retrieve a list of users along with their usernames and the
2+
-- rank of their account creation, ordered by the creation date in ascending order.
3+
4+
SELECT *,
5+
RANK() OVER (ORDER BY created_at) ac_creation_rank
6+
FROM users
7+
ORDER BY created_at

Q5.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- 5. List the comments made on photos with their comment texts,
2+
-- photo URLs, and usernames of users who posted the comments. Include the comment count for each photo
3+
USE ig_clone;
4+
5+
SELECT c.comment_text, p.image_url, u.username,
6+
(SELECT COUNT(*)
7+
FROM comments c2
8+
WHERE c2.photo_id = c.photo_id) count_of_comment
9+
FROM comments c
10+
INNER JOIN photos p
11+
ON c.user_id = p.user_id
12+
INNER JOIN users u
13+
ON p.user_id = u.id
14+
15+

Q6.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- 6. For each tag, show the tag name and the number of
2+
-- photos associated with that tag. Rank the tags by the number of photos in descending order.
3+
USE ig_clone;
4+
5+
SELECT t.tag_name, COUNT(pt.photo_id) count_of_photo
6+
FROM tags t
7+
LEFT JOIN photo_tags pt
8+
ON t.id = pt.tag_id
9+
GROUP BY t.tag_name
10+
ORDER BY count_of_photo

Q7.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- 7. List the usernames of users who have posted photos along
2+
-- with the count of photos they have posted. Rank them by the number of photos in descending order.
3+
4+
5+
USE ig_clone;
6+
SELECT u.username, COUNT(p.id) AS photo_count
7+
FROM users u
8+
LEFT JOIN photos p
9+
ON u.id = p.user_id
10+
GROUP BY u.username
11+
ORDER BY photo_count DESC;

Q8.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- 8. Display the username of each user along with the
2+
-- creation date of their first posted photo and the creation date of their next posted photo.
3+
4+
USE ig_clone;
5+
6+
WITH UserPhotoDates AS (
7+
SELECT
8+
p.user_id,
9+
MIN(p.created_at) first_photo_date,
10+
LEAD(MIN(p.created_at)) OVER (PARTITION BY p.user_id ORDER BY MIN(p.created_at)) next_photo_date
11+
FROM photos p
12+
GROUP BY p.user_id
13+
)
14+
15+
SELECT
16+
u.username,
17+
upd.first_photo_date,
18+
upd.next_photo_date
19+
FROM users u
20+
INNER JOIN UserPhotoDates upd
21+
ON u.id = upd.user_id;

Q9.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- 9. For each comment, show the comment text, the username of the commenter,
2+
-- and the comment text of the previous comment made on the same photo.
3+
4+
WITH CommentWithPrevious AS (
5+
SELECT
6+
u.username AS commenter_username,
7+
c1.comment_text AS current_comment_text,
8+
LAG(c1.comment_text) OVER (PARTITION BY c1.photo_id ORDER BY c1.created_at) AS previous_comment_text
9+
FROM comments c1
10+
INNER JOIN users u
11+
ON c1.user_id = u.id
12+
)
13+
14+
SELECT
15+
current_comment_text,
16+
commenter_username,
17+
previous_comment_text
18+
FROM
19+
CommentWithPrevious;

0 commit comments

Comments
 (0)