-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL Queries.sql
More file actions
156 lines (138 loc) · 4.38 KB
/
SQL Queries.sql
File metadata and controls
156 lines (138 loc) · 4.38 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use ig_clone;
-- q1
/* We want to reward our users who have been around the longest.
Find the 5 oldest users.*/
SELECT * FROM users
ORDER BY created_at
LIMIT 5;
-- q2
/* What day of the week do most users register on?
We need to figure out when to schedule an ad campgain.*/
SELECT date_format(created_at,'%W') AS 'day of the week', COUNT(*) AS 'total registration'
FROM users
GROUP BY 1
ORDER BY 2 DESC;
/*Another method*/
SELECT DAYNAME(created_at) AS day, COUNT(*) AS total
FROM users
GROUP BY day
ORDER BY total DESC;
-- q3
/*We want to target our inactive users with an email campaign.
Find the users who have never posted a photo*/
SELECT username
FROM users
LEFT JOIN photos ON users.id = photos.user_id
WHERE photos.id IS NULL;
-- q4
/*We are running a new contest to see who can get the most likes on a single photo.
WHO WON??!!*/
SELECT users.username, photos.id, photos.image_url, COUNT(*) AS Total_Likes
FROM likes
JOIN photos ON photos.id = likes.photo_id
JOIN users ON users.id = likes.user_id
GROUP BY photos.id
ORDER BY Total_Likes DESC
LIMIT 1;
/* Another Method */
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;
-- q5
/* Our Investors want to know...
How many times does the average user post?
/*total number of photos/total number of users */
SELECT ROUND((SELECT COUNT(*)FROM photos)/(SELECT COUNT(*) FROM users),2);
-- q6
/* User ranking by postings higher to lower */
SELECT users.username,COUNT(photos.image_url)
FROM users
JOIN photos ON users.id = photos.user_id
GROUP BY users.id
ORDER BY 2 DESC;
-- q7
/* Total Posts by users. */
SELECT SUM(user_posts.total_posts_per_user)
FROM (SELECT users.username,COUNT(photos.image_url) AS total_posts_per_user
FROM users
JOIN photos ON users.id = photos.user_id
GROUP BY users.id) AS user_posts;
-- q8
/* Total numbers of users who have posted at least one time */
SELECT COUNT(DISTINCT(users.id)) AS total_number_of_users_with_posts
FROM users
JOIN photos ON users.id = photos.user_id;
-- q9
/* A brand wants to know which hashtags to use in a post
What are the top 5 most commonly used hashtags? */
SELECT tag_name, COUNT(tag_name) AS total
FROM tags
JOIN photo_tags ON tags.id = photo_tags.tag_id
GROUP BY tags.id
ORDER BY total DESC;
-- q10
/* We have a small problem with bots on our site...
Find users who have liked every single photo on the site */
SELECT users.id,username, COUNT(users.id) As total_likes_by_user
FROM users
JOIN likes ON users.id = likes.user_id
GROUP BY users.id
HAVING total_likes_by_user = (SELECT COUNT(*) FROM photos);
-- q11
/* We also have a problem with celebrities
Find users who have never commented on a photo */
SELECT distinct username,comment_text
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING comment_text IS NULL;
-- q12
-- /* Find the count of users who have never commented on photo */
SELECT COUNT(*) FROM
(SELECT username,comment_text
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING comment_text IS NULL) AS total_number_of_users_without_comments;
-- q13
/* Find users who have ever commented on a photo */
SELECT username,comment_text
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING comment_text IS NOT NULL;
SELECT COUNT(*) FROM
(SELECT username,comment_text
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING comment_text IS NOT NULL) AS total_number_users_with_comments;
-- q14
/* Are we overrun with bots and celebrity accounts?
Find the percentage of our users who have either never commented on a photo or have commented on photos before */
WITH UsersWithoutComments AS (
SELECT username
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING COUNT(comment_text) = 0
),
UsersWithComments AS (
SELECT username
FROM users
LEFT JOIN comments ON users.id = comments.user_id
GROUP BY users.id
HAVING COUNT(comment_text) > 0
)
SELECT
COUNT(*) AS 'Number Of Users who never commented',
(COUNT(*) / (SELECT COUNT(*) FROM users)) * 100 AS '%',
(SELECT COUNT(*) FROM UsersWithComments) AS 'Number of Users who commented on photos',
(SELECT COUNT(*) / (SELECT COUNT(*) FROM users)) * 100 AS '%'
FROM UsersWithoutComments;