1
+ /* We want to reward our users who have been around the longest.
2
+ Find the 5 oldest users.*/
3
+ SELECT * FROM users
4
+ ORDER BY created_at
5
+ LIMIT 5 ;
6
+
7
+
8
+ /* What day of the week do most users register on?
9
+ We need to figure out when to schedule an ad campgain*/
10
+ SELECT date_format(created_at,' %W' ) AS ' day of the week' , COUNT (* ) AS ' total registration'
11
+ FROM users
12
+ GROUP BY 1
13
+ ORDER BY 2 DESC ;
14
+
15
+ /* version 2*/
16
+ SELECT
17
+ DAYNAME(created_at) AS day,
18
+ COUNT (* ) AS total
19
+ FROM users
20
+ GROUP BY day
21
+ ORDER BY total DESC
22
+ LIMIT 2 ;
23
+
24
+
25
+ /* We want to target our inactive users with an email campaign.
26
+ Find the users who have never posted a photo*/
27
+ SELECT username
28
+ FROM users
29
+ LEFT JOIN photos ON users .id = photos .user_id
30
+ WHERE photos .id IS NULL ;
31
+
32
+
33
+ /* We're running a new contest to see who can get the most likes on a single photo.
34
+ WHO WON??!!*/
35
+ SELECT users .username ,photos .id ,photos .image_url ,COUNT (* ) AS Total_Likes
36
+ FROM likes
37
+ JOIN photos ON photos .id = likes .photo_id
38
+ JOIN users ON users .id = likes .user_id
39
+ GROUP BY photos .id
40
+ ORDER BY Total_Likes DESC
41
+ LIMIT 1 ;
42
+
43
+ /* version 2*/
44
+ SELECT
45
+ username,
46
+ photos .id ,
47
+ photos .image_url ,
48
+ COUNT (* ) AS total
49
+ FROM photos
50
+ INNER JOIN likes
51
+ ON likes .photo_id = photos .id
52
+ INNER JOIN users
53
+ ON photos .user_id = users .id
54
+ GROUP BY photos .id
55
+ ORDER BY total DESC
56
+ LIMIT 1 ;
57
+
58
+
59
+ /* Our Investors want to know...
60
+ How many times does the average user post?*/
61
+ /* total number of photos/total number of users*/
62
+ SELECT ROUND((SELECT COUNT (* )FROM photos)/ (SELECT COUNT (* ) FROM users),2 );
63
+
64
+
65
+ /* user ranking by postings higher to lower*/
66
+ SELECT users .username ,COUNT (photos .image_url )
67
+ FROM users
68
+ JOIN photos ON users .id = photos .user_id
69
+ GROUP BY users .id
70
+ ORDER BY 2 DESC ;
71
+
72
+
73
+ /* Total Posts by users (longer versionof SELECT COUNT(*)FROM photos) */
74
+ SELECT SUM (user_posts .total_posts_per_user )
75
+ FROM (SELECT users .username ,COUNT (photos .image_url ) AS total_posts_per_user
76
+ FROM users
77
+ JOIN photos ON users .id = photos .user_id
78
+ GROUP BY users .id ) AS user_posts;
79
+
80
+
81
+ /* total numbers of users who have posted at least one time */
82
+ SELECT COUNT (DISTINCT(users .id )) AS total_number_of_users_with_posts
83
+ FROM users
84
+ JOIN photos ON users .id = photos .user_id ;
85
+
86
+
87
+ /* A brand wants to know which hashtags to use in a post
88
+ What are the top 5 most commonly used hashtags?*/
89
+ SELECT tag_name, COUNT (tag_name) AS total
90
+ FROM tags
91
+ JOIN photo_tags ON tags .id = photo_tags .tag_id
92
+ GROUP BY tags .id
93
+ ORDER BY total DESC ;
94
+
95
+
96
+ /* We have a small problem with bots on our site...
97
+ Find users who have liked every single photo on the site*/
98
+ SELECT users .id ,username, COUNT (users .id ) As total_likes_by_user
99
+ FROM users
100
+ JOIN likes ON users .id = likes .user_id
101
+ GROUP BY users .id
102
+ HAVING total_likes_by_user = (SELECT COUNT (* ) FROM photos);
103
+
104
+
105
+ /* We also have a problem with celebrities
106
+ Find users who have never commented on a photo*/
107
+ SELECT username,comment_text
108
+ FROM users
109
+ LEFT JOIN comments ON users .id = comments .user_id
110
+ GROUP BY users .id
111
+ HAVING comment_text IS NULL ;
112
+
113
+ SELECT COUNT (* ) FROM
114
+ (SELECT username,comment_text
115
+ FROM users
116
+ LEFT JOIN comments ON users .id = comments .user_id
117
+ GROUP BY users .id
118
+ HAVING comment_text IS NULL ) AS total_number_of_users_without_comments;
119
+
120
+ /* Mega Challenges
121
+ Are we overrun with bots and celebrity accounts?
122
+ Find the percentage of our users who have either never commented on a photo or have commented on every photo*/
123
+
124
+ SELECT tableA .total_A AS ' Number Of Users who never commented' ,
125
+ (tableA .total_A / (SELECT COUNT (* ) FROM users))* 100 AS ' %' ,
126
+ tableB .total_B AS ' Number of Users who likes every photos' ,
127
+ (tableB .total_B / (SELECT COUNT (* ) FROM users))* 100 AS ' %'
128
+ FROM
129
+ (
130
+ SELECT COUNT (* ) AS total_A FROM
131
+ (SELECT username,comment_text
132
+ FROM users
133
+ LEFT JOIN comments ON users .id = comments .user_id
134
+ GROUP BY users .id
135
+ HAVING comment_text IS NULL ) AS total_number_of_users_without_comments
136
+ ) AS tableA
137
+ JOIN
138
+ (
139
+ SELECT COUNT (* ) AS total_B FROM
140
+ (SELECT users .id ,username, COUNT (users .id ) As total_likes_by_user
141
+ FROM users
142
+ JOIN likes ON users .id = likes .user_id
143
+ GROUP BY users .id
144
+ HAVING total_likes_by_user = (SELECT COUNT (* ) FROM photos)) AS total_number_users_likes_every_photos
145
+ )AS tableB;
146
+
147
+
148
+ /* Find users who have ever commented on a photo*/
149
+ SELECT username,comment_text
150
+ FROM users
151
+ LEFT JOIN comments ON users .id = comments .user_id
152
+ GROUP BY users .id
153
+ HAVING comment_text IS NOT NULL ;
154
+
155
+
156
+ SELECT COUNT (* ) FROM
157
+ (SELECT username,comment_text
158
+ FROM users
159
+ LEFT JOIN comments ON users .id = comments .user_id
160
+ GROUP BY users .id
161
+ HAVING comment_text IS NOT NULL ) AS total_number_users_with_comments;
162
+
163
+
164
+ /* Are we overrun with bots and celebrity accounts?
165
+ Find the percentage of our users who have either never commented on a photo or have commented on photos before*/
166
+
167
+ SELECT tableA .total_A AS ' Number Of Users who never commented' ,
168
+ (tableA .total_A / (SELECT COUNT (* ) FROM users))* 100 AS ' %' ,
169
+ tableB .total_B AS ' Number of Users who commented on photos' ,
170
+ (tableB .total_B / (SELECT COUNT (* ) FROM users))* 100 AS ' %'
171
+ FROM
172
+ (
173
+ SELECT COUNT (* ) AS total_A FROM
174
+ (SELECT username,comment_text
175
+ FROM users
176
+ LEFT JOIN comments ON users .id = comments .user_id
177
+ GROUP BY users .id
178
+ HAVING comment_text IS NULL ) AS total_number_of_users_without_comments
179
+ ) AS tableA
180
+ JOIN
181
+ (
182
+ SELECT COUNT (* ) AS total_B FROM
183
+ (SELECT username,comment_text
184
+ FROM users
185
+ LEFT JOIN comments ON users .id = comments .user_id
186
+ GROUP BY users .id
187
+ HAVING comment_text IS NOT NULL ) AS total_number_users_with_comments
188
+ )AS tableB
0 commit comments