@@ -10,4 +10,128 @@ WITH User_Post_Count AS (
1010
1111SELECT  AVG (Post_Count) Avg_Posts_Per_User
1212FROM  User_Post_Count
13+ 	
14+ 
15+ --  2. Find the top 5 most used hashtags.
16+ USE ig_clone;
17+ SELECT  tag_name, COUNT (* ) tags_count
18+ FROM  tags
19+ GROUP BY  tag_name
20+ ORDER BY  tags_count
21+ LIMIT  5 
22+ 
23+ 
24+ --  3. Find users who have liked every single photo on the site.
25+ 
26+ USE ig_clone;
27+ SELECT  u .id , u .username 
28+ FROM  users u 
29+ INNER JOIN  photos p 
30+ 	ON  u .id  =  p .user_id 
31+ LEFT JOIN  likes l 
32+ 	ON  p .id  =  l .photo_id 
33+ GROUP BY  u .id , u .username 
34+ HAVING  COUNT (DISTINCT p .id ) =  COUNT (l .photo_id )
35+ ORDER BY  u .id 
36+ 
37+ 
38+ --  4. Retrieve a list of users along with their usernames and the 
39+ --  rank of their account creation, ordered by the creation date in ascending order.
40+ 
41+ SELECT  * ,
42+ 		RANK() OVER (ORDER BY  created_at) ac_creation_rank
43+ FROM  users
44+ ORDER BY  created_at
45+ 
46+ 
47+ 
48+ --  5. List the comments made on photos with their comment texts, 
49+ --  photo URLs, and usernames of users who posted the comments. Include the comment count for each photo
50+ USE ig_clone;
51+ 
52+ SELECT  c .comment_text , p .image_url , u .username ,
53+ 	   (SELECT  COUNT (* ) 
54+         FROM  comments c2 
55+         WHERE  c2 .photo_id  =  c .photo_id ) count_of_comment
56+ FROM  comments c
57+ INNER JOIN  photos p 
58+ 	ON  c .user_id  =  p .user_id 
59+ INNER JOIN  users u 
60+ 	ON  p .user_id  =  u .id 
61+ 
62+ 
63+ 
64+ 
65+ --  6. For each tag, show the tag name and the number of 
66+ --  photos associated with that tag. Rank the tags by the number of photos in descending order.
67+ USE ig_clone;
68+ 
69+ SELECT  t .tag_name , COUNT (pt .photo_id ) count_of_photo
70+ FROM  tags t 
71+ LEFT JOIN  photo_tags pt 
72+ 	ON  t .id  =  pt .tag_id  
73+ GROUP BY  t .tag_name 
74+ ORDER BY  count_of_photo
75+ 
76+ 
77+ 
78+ --  7. List the usernames of users who have posted photos along 
79+ --  with the count of photos they have posted. Rank them by the number of photos in descending order.
80+ 
81+ 
82+ USE ig_clone;
83+ SELECT  u .username , COUNT (p .id ) AS  photo_count
84+ FROM  users u
85+ LEFT JOIN  photos p 
86+ 	ON  u .id  =  p .user_id 
87+ GROUP BY  u .username 
88+ ORDER BY  photo_count DESC ;
89+ 
90+ 
91+ 
92+ --  8. Display the username of each user along with the
93+ --  creation date of their first posted photo and the creation date of their next posted photo.
94+ 
95+ USE ig_clone;
96+ 
97+ WITH UserPhotoDates AS  (
98+     SELECT 
99+         p .user_id ,
100+         MIN (p .created_at )  first_photo_date,
101+         LEAD(MIN (p .created_at )) OVER (PARTITION BY p .user_id  ORDER BY  MIN (p .created_at ))  next_photo_date
102+     FROM  photos p
103+     GROUP BY  p .user_id 
104+ )
105+ 
106+ SELECT 
107+     u .username ,
108+     upd .first_photo_date ,
109+     upd .next_photo_date 
110+ FROM  users u
111+ INNER JOIN  UserPhotoDates upd
112+ 	ON  u .id  =  upd .user_id ;
113+ 
114+ 
115+ 
116+ --  9. For each comment, show the comment text, the username of the commenter, 
117+ --  and the comment text of the previous comment made on the same photo.
118+ 
119+ WITH CommentWithPrevious AS  (
120+     SELECT 
121+ 		u .username  AS  commenter_username,
122+         c1 .comment_text  AS  current_comment_text,
123+ 			LAG(c1 .comment_text ) OVER (PARTITION BY c1 .photo_id  ORDER BY  c1 .created_at ) AS  previous_comment_text
124+     FROM  comments c1
125+     INNER JOIN  users u 
126+ 		ON  c1 .user_id  =  u .id 
127+ )
128+ 
129+ SELECT 
130+     current_comment_text,
131+     commenter_username,
132+     previous_comment_text
133+ FROM 
134+     CommentWithPrevious;
135+ 
136+ 
13137
0 commit comments