-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_instagram_queries.sql
67 lines (44 loc) · 1.05 KB
/
14_instagram_queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- finding 5 oldest users
SELECT
*
FROM
users
ORDER BY created_at
LIMIT 5
;
-- what the most day of week do most users register on
SELECT
DAYNAME(created_at) AS day,
COUNT(*) AS total
FROM users
GROUP BY day
ORDER BY total DESC
LIMIT 2;
-- identify inactive users
SELECT username,image_url FROM users
LEFT JOIN photos
ON users.id = photos.user_id
WHERE photos.id IS NULL;
-- most like photos
SELECT username,photos.id,photos.image_url,COUNT(*) AS total
FROM photos
INNER JOIN likes
ON likes.photo_id = photos.id
INNER JOIN users
ON photos.user_id = users.id
GROUP BY photos.id
ORDER BY total DESC
LIMIT 1;
-- how many times average user posts
SELECT (SELECT COUNT(*) FROM photos)/(SELECT COUNT(*) FROM users) AS avg;
-- most popular hashtag
SELECT tags.tag_name, COUNT(*) AS total FROM photo_tags
JOIN tags
ON photo_tags.tag_id = tags.id
GROUP BY tags.id
ORDER BY total;
-- find user who have liked every photo (FIND BOTS)
SELECT username,user_id, COUNT(*) AS total FROM users
INNER JOIN likes
ON users.id = likes.user_id
GROUP BY likes.user_id;