forked from ssahibsingh/Social-Media-Database-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
99 lines (75 loc) · 2.78 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- 1. Location of User
SELECT * FROM post
WHERE location IN ('agra' ,'maharashtra','west bengal');
-- 2. Most Followed Hashtag
SELECT
hashtag_name AS 'Hashtags', COUNT(hashtag_follow.hashtag_id) AS 'Total Follows'
FROM hashtag_follow, hashtags
WHERE hashtags.hashtag_id = hashtag_follow.hashtag_id
GROUP BY hashtag_follow.hashtag_id
ORDER BY COUNT(hashtag_follow.hashtag_id) DESC LIMIT 5;
-- 3. Most Used Hashtags
SELECT
hashtag_name AS 'Trending Hashtags',
COUNT(post_tags.hashtag_id) AS 'Times Used'
FROM hashtags,post_tags
WHERE hashtags.hashtag_id = post_tags.hashtag_id
GROUP BY post_tags.hashtag_id
ORDER BY COUNT(post_tags.hashtag_id) DESC LIMIT 10;
-- 4. Most Inactive User
SELECT user_id, username AS 'Most Inactive User'
FROM users
WHERE user_id NOT IN (SELECT user_id FROM post);
-- 5. Most Likes Posts
SELECT post_likes.user_id, post_likes.post_id, COUNT(post_likes.post_id)
FROM post_likes, post
WHERE post.post_id = post_likes.post_id
GROUP BY post_likes.post_id
ORDER BY COUNT(post_likes.post_id) DESC ;
-- 6. Average post per user
SELECT ROUND((COUNT(post_id) / COUNT(DISTINCT user_id) ),2) AS 'Average Post per User'
FROM post;
-- 7. no. of login by per user
SELECT user_id, email, username, login.login_id AS login_number
FROM users
NATURAL JOIN login;
-- 8. User who liked every single post (CHECK FOR BOT)
SELECT username, Count(*) AS num_likes
FROM users
INNER JOIN post_likes ON users.user_id = post_likes.user_id
GROUP BY post_likes.user_id
HAVING num_likes = (SELECT Count(*) FROM post);
-- 9. User Never Comment
SELECT user_id, username AS 'User Never Comment'
FROM users
WHERE user_id NOT IN (SELECT user_id FROM comments);
-- 10. User who commented on every post (CHECK FOR BOT)
SELECT username, Count(*) AS num_comment
FROM users
INNER JOIN comments ON users.user_id = comments.user_id
GROUP BY comments.user_id
HAVING num_comment = (SELECT Count(*) FROM comments);
-- 11. User Not Followed by anyone
SELECT user_id, username AS 'User Not Followed by anyone'
FROM users
WHERE user_id NOT IN (SELECT followee_id FROM follows);
-- 12. User Not Following Anyone
SELECT user_id, username AS 'User Not Following Anyone'
FROM users
WHERE user_id NOT IN (SELECT follower_id FROM follows);
-- 13. Posted more than 5 times
SELECT user_id, COUNT(user_id) AS post_count FROM post
GROUP BY user_id
HAVING post_count > 5
ORDER BY COUNT(user_id) DESC;
-- 14. Followers > 40
SELECT followee_id, COUNT(follower_id) AS follower_count FROM follows
GROUP BY followee_id
HAVING follower_count > 40
ORDER BY COUNT(follower_id) DESC;
-- 15. Any specific word in comment
SELECT * FROM comments
WHERE comment_text REGEXP'good|beautiful';
-- 16. Longest captions in post
SELECT user_id, caption, LENGTH(post.caption) AS caption_length FROM post
ORDER BY caption_length DESC LIMIT 5;