Skip to content

Commit fbc50c4

Browse files
Update Q1.sql
Used Instagram-Like database and analyzed some of the important insights using SQL
1 parent 211f51a commit fbc50c4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Q1.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,34 @@ FROM
135135

136136

137137

138+
-- 10.Show the username of each user along with the number
139+
-- of photos they have posted and the number of photos posted by the user
140+
-- before them and after them, based on the creation date.
141+
142+
USE ig_clone;
143+
WITH UserPhotoCounts AS (
144+
SELECT
145+
u.id,
146+
u.username,
147+
COUNT(p.id) photo_count,
148+
MIN(p.created_at) first_photo_date
149+
150+
FROM users u
151+
LEFT JOIN photos p ON u.id = p.user_id
152+
GROUP BY u.id, u.username
153+
),
154+
UserPhotoCountsPrevNext AS (
155+
SELECT
156+
upc.*,
157+
ROW_NUMBER() OVER (ORDER BY upc.first_photo_date) AS rw_number
158+
FROM UserPhotoCounts upc
159+
)
160+
161+
SELECT
162+
upc.username,
163+
upc.photo_count,
164+
LAG(upc.photo_count) OVER (ORDER BY upc.rw_number) AS prev_photo_count,
165+
LEAD(upc.photo_count) OVER (ORDER BY upc.rw_number) AS next_photo_count
166+
FROM UserPhotoCountsPrevNext upc
167+
ORDER BY upc.rw_number;
168+

0 commit comments

Comments
 (0)