From 51824ffcedeb17adbe15ef492094578da1466bc2 Mon Sep 17 00:00:00 2001 From: dodymds Date: Fri, 11 Mar 2022 22:57:49 +0700 Subject: [PATCH 1/3] add query for data analysis --- ....Instagram Clone Database - Challenges.sql | 113 +++++++++++++----- 1 file changed, 84 insertions(+), 29 deletions(-) diff --git a/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql b/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql index 9fd9191..2ed6ce7 100644 --- a/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql +++ b/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql @@ -1,44 +1,99 @@ /*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; - +Find the 10 oldest users.*/ +SELECT + * +FROM + users +ORDER BY + created_at +LIMIT + 10; + +/*We want to know our users who have been around the shortest. +Find the 5 youngest users.*/ +SELECT + * +FROM + users +ORDER BY + created_at desc +LIMIT + 5; /*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; +We need to figure out when to schedule an ad campaign*/ +SELECT + date_format(created_at, '%W') AS 'day of the week', + COUNT(*) AS 'total registration' +FROM + users +GROUP BY + 1 +ORDER BY + 2 DESC; /*version 2*/ -SELECT - DAYNAME(created_at) AS day, - COUNT(*) AS total -FROM users -GROUP BY day -ORDER BY total DESC -LIMIT 2; - +SELECT + DAYNAME(created_at) AS day, + COUNT(*) AS total +FROM + users +GROUP BY + day +ORDER BY + total DESC +LIMIT + 2; + +/*What time of the day do most users register on? +We need to figure out when to schedule an ad campaign*/ +SELECT + date_format(created_at, '%H') AS 'time of the day', + COUNT(*) AS 'total registration' +FROM + users +GROUP BY + 1 +ORDER BY + 2 DESC; /*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; +SELECT + username +FROM + users + LEFT JOIN photos ON users.id = photos.user_id +WHERE + photos.id IS NULL; +/*We want to target our inactive users with an email campaign. +Find the users who have never posted a comments*/ +SELECT + username +FROM + users + LEFT JOIN comments ON users.id = comments.user_id +WHERE + comments.id IS NULL; /*We're 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; +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; /*version 2*/ SELECT From fb1be05bd665ca2ae6517ef40e9a65b93edccb0f Mon Sep 17 00:00:00 2001 From: dodymds Date: Sat, 12 Mar 2022 11:51:53 +0700 Subject: [PATCH 2/3] finishing add new query --- ....Instagram Clone Database - Challenges.sql | 63 +++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql b/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql index 9fd9191..4655710 100644 --- a/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql +++ b/Instagram Clone Project/15.Instagram Clone Database - Challenges.sql @@ -1,8 +1,13 @@ /*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; +Find the 10 oldest users.*/ +SELECT + * +FROM + users +ORDER BY + created_at +LIMIT + 10; /*What day of the week do most users register on? @@ -12,6 +17,14 @@ FROM users GROUP BY 1 ORDER BY 2 DESC; +/*What time of the day do most users register on? +We need to figure out when to schedule an ad campaign*/ +SELECT date_format(created_at,'%H') AS 'time of the day', COUNT(*) AS 'total registration' +FROM users +GROUP BY 1 +ORDER BY 2 DESC +LIMIT 5; + /*version 2*/ SELECT DAYNAME(created_at) AS day, @@ -29,6 +42,12 @@ FROM users LEFT JOIN photos ON users.id = photos.user_id WHERE photos.id IS NULL; +/*We want to target our inactive users with an email campaign. +Find the users who have never posted a comment*/ +SELECT users.id, username +FROM users +LEFT JOIN comments ON users.id = comments.user_id +WHERE comments.id IS NULL; /*We're running a new contest to see who can get the most likes on a single photo. WHO WON??!!*/ @@ -144,6 +163,42 @@ FROM HAVING total_likes_by_user = (SELECT COUNT(*) FROM photos)) AS total_number_users_likes_every_photos )AS tableB; +/*Mega Challenges +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 every photo*/ + +SELECT + COUNT(*) AS total_account_anomaly, + COUNT(*) AS percentage_of_account_anomaly +FROM + ( + SELECT + users.id, + username + FROM + users + LEFT JOIN comments ON users.id = comments.user_id + WHERE + comments.id IS NULL + UNION + SELECT + users.id, + username + FROM + users + JOIN ( + SELECT + user_id, + COUNT(*) AS total_comments + FROM + comments + GROUP BY + user_id + HAVING + COUNT(*) = 257 + ) com ON users.id = com.user_id + ) tabel; + /*Find users who have ever commented on a photo*/ SELECT username,comment_text From 79d3742837f890f12f7a50184e5da1e9d53fbad6 Mon Sep 17 00:00:00 2001 From: dodymds Date: Sat, 12 Mar 2022 12:21:45 +0700 Subject: [PATCH 3/3] correcting the punctuation --- Ultimate MySQL/05.CRUD Commands.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ultimate MySQL/05.CRUD Commands.sql b/Ultimate MySQL/05.CRUD Commands.sql index fa8723b..8535f02 100644 --- a/Ultimate MySQL/05.CRUD Commands.sql +++ b/Ultimate MySQL/05.CRUD Commands.sql @@ -53,7 +53,7 @@ UPDATE cats SET breed = 'British Shorthair' WHERE name = 'Ringo'; -#Update both Maine Coons' ages to be 12 +#Update both Maine Coons ages to be 12 UPDATE cats SET age = 12 WHERE breed = 'Maine Coon';