-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram_questions.sql
More file actions
66 lines (59 loc) · 1.68 KB
/
instagram_questions.sql
File metadata and controls
66 lines (59 loc) · 1.68 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
USE instagram;
-- Question 1
-- We want to reward users who have been around the longest.
-- Find the 5 oldest users.
SELECT username
FROM users
ORDER BY created_at
LIMIT 5;
-- Question 2
-- We need to figure out when to schedule an ad campaign.
-- What day of the week do most users register on?
SELECT DAYNAME(created_at) AS day_of_the_week, COUNT(*) AS users_registered
FROM users
GROUP BY day_of_the_week
ORDER BY users_registered DESC
LIMIT 2;
-- Question 3
-- 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 photos.user_id = users.id
WHERE photos.id IS NULL;
-- Question 4
-- We're running a new contest to see who can get the most likes on a single photo.
-- Who won?
SELECT image_url, username, COUNT(*) AS total_likes
FROM likes
JOIN photos
ON photos.id = likes.photo_id
JOIN users
ON photos.user_id = users.id
GROUP BY image_url
ORDER BY total_likes DESC
LIMIT 1;
-- Question 5
-- Our investor want to know how many times the average user posts.
SELECT
(SELECT COUNT(*) FROM photos) / (SELECT COUNT(*) FROM users) AS avg_user_posts;
-- Question 6
-- A brand wants to know which hastags to use in a post.
-- What are the top 5 most commonly used hashtags?
SELECT tag_name, COUNT(*) AS total_count
FROM tags
JOIN photo_tags
ON photo_tags.tag_id = tags.id
GROUP BY tag_name
ORDER BY total_count DESC
LIMIT 5;
-- Question 7
-- We have a small problem with bots on our site
-- Find users who have liked every single photo on the site
SELECT username, COUNT(*) AS photos_liked
FROM users
JOIN likes
ON users.id = likes.user_id
GROUP BY username
HAVING photos_liked = (SELECT COUNT(*) FROM photos);