Skip to content

Commit 568c921

Browse files
committed
add sql for data analysis - weekender crash course
1 parent c0d9ee1 commit 568c921

File tree

5 files changed

+47331
-1
lines changed

5 files changed

+47331
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/***************************************************/
2+
/* SQL for Data Analysis - Weekender Crash Course */
3+
/***************************************************/
4+
5+
/*Before you run these queries, make sure to create Sakila DB and import the related data first*/
6+
7+
/* Email Campaigns for customers of Store 2
8+
First, Last name and Email address of customers from Store 2*/
9+
SELECT first_name, last_name,email
10+
FROM customer
11+
WHERE store_id = 2;
12+
13+
/* movie with rental rate of 0.99$*/
14+
SELECT COUNT(*) FROM film
15+
WHERE rental_rate = 0.99;
16+
17+
/* we want to see rental rate and how many movies are in each rental rate categories*/
18+
SELECT rental_rate, COUNT(*) AS total_number_of_movies
19+
FROM film
20+
GROUP BY rental_rate;
21+
22+
SELECT rental_rate, COUNT(*) AS total_number_of_movies
23+
FROM film
24+
GROUP BY 1;
25+
26+
/*Which rating do we have the most films in?*/
27+
SELECT rating,COUNT(*) AS total_number_of_movies
28+
FROM film
29+
GROUP BY 1;
30+
31+
/*Which rating is most prevalant in each store?*/
32+
SELECT s.store_id, f.rating, COUNT(f.rating) AS total_number_of_films
33+
FROM store s
34+
JOIN inventory i ON s.store_id = i.store_id
35+
JOIN film f ON f.film_id = i.film_id
36+
GROUP BY 1,2;
37+
38+
/*We want to mail the customers about the upcoming promotion*/
39+
SELECT c.customer_id, c.first_name, c.last_name, a.address
40+
FROM customer c
41+
JOIN address a ON c.address_id = a.address_id;
42+
43+
/* List of films by Film Name, Category, Language*/
44+
SELECT f.title,c.name,l.name
45+
FROM film f
46+
JOIN film_category fc ON fc.film_id = f.film_id
47+
JOIN category c ON fc.category_id = c.category_id
48+
JOIN language l ON l.language_id = f.language_id;
49+
50+
/* How many times each movie has been rented out? */
51+
SELECT i.film_id, f.title, COUNT(i.film_id) AS total_number_of_rental_times
52+
FROM rental r
53+
JOIN inventory i ON r.inventory_id = i.inventory_id
54+
JOIN film f ON f.film_id = i.film_id
55+
GROUP BY i.film_id
56+
ORDER BY 3 DESC;
57+
58+
/*Revenue per Movie */
59+
SELECT i.film_id, f.title, COUNT(i.film_id) AS total_number_of_rental_times, f.rental_rate, COUNT(i.film_id)*f.rental_rate AS revenue_per_movie
60+
FROM rental r
61+
JOIN inventory i ON r.inventory_id = i.inventory_id
62+
JOIN film f ON f.film_id = i.film_id
63+
GROUP BY i.film_id
64+
ORDER BY 5 DESC;
65+
66+
/* Most Spending Customer so that we can send him/her rewards or debate points*/
67+
SELECT c.customer_id, SUM(p.amount) AS "Total Spending"
68+
FROM customer c
69+
JOIN payment p ON c.customer_id = p.customer_id
70+
GROUP BY 1
71+
ORDER BY 2 DESC;
72+
73+
/* What Store has historically brought the most revenue */
74+
SELECT s.store_id, SUM(p.amount) AS "Total Spending"
75+
FROM store s
76+
JOIN inventory i ON i.store_id = s.store_id
77+
JOIN rental r ON r.inventory_id = i.inventory_id
78+
JOIN payment p ON p.rental_id = r.rental_id
79+
GROUP BY 1
80+
ORDER BY 2 DESC;
81+
82+
/*How many rentals we have for each month*/
83+
SELECT left(rental_date,7) AS "Month", COUNT(*)
84+
FROM rental
85+
GROUP BY 1;
86+
87+
/* Rentals per Month (such Jan => How much, etc)*/
88+
SELECT date_format(rental_date,"%M") AS "Month", COUNT(*)
89+
FROM rental
90+
GROUP BY 1
91+
ORDER BY 2 DESC;
92+
93+
/* Which date first movie was rented out ? */
94+
SELECT MIN(rental_date)
95+
FROM rental;
96+
97+
/* Which date last movie was rented out ? */
98+
SELECT MAX(rental_date)
99+
FROM rental;
100+
101+
/* For each movie, when was the first time and last time it was rented out? */
102+
SELECT f.title AS "Film Title", MIN(r.rental_date) AS "First Rented Date", MAX(r.rental_date) AS "Last Rented Date"
103+
FROM film f
104+
JOIN inventory i ON f.film_id = i.film_id
105+
JOIN rental r ON r.inventory_id = i.inventory_id
106+
GROUP BY 1;
107+
108+
/* Last Rental Date of every customer */
109+
SELECT c.customer_id, c.first_name, c.last_name, MAX(r.rental_date) AS "Last Rental Date"
110+
FROM customer c
111+
JOIN rental r ON r.customer_id = c.customer_id
112+
GROUP BY 1;
113+
114+
/* Revenue Per Month */
115+
SELECT LEFT(payment_date,7) AS "Month", SUM(amount) AS "Revenue Per Month"
116+
FROM payment
117+
GROUP BY 1;
118+
119+
/* How many distint Renters per month*/
120+
SELECT LEFT(rental_date,7) AS "Month",
121+
COUNT(DISTINCT(rental_id)) AS "Total Rentals",
122+
COUNT(DISTINCT(customer_id)) AS "Number Of Unique Renter",
123+
COUNT(DISTINCT(rental_id))/COUNT(DISTINCT(customer_id)) AS "Average Rent Per Renter"
124+
FROM rental
125+
GROUP BY 1;
126+
127+
/*Number of Distinct Film Rented Each Month */
128+
SELECT i.film_id, f.title, LEFT(r.rental_date,7) AS "Month", COUNT(i.film_id) AS "Total Number Of Rental Times"
129+
FROM rental r
130+
JOIN inventory i ON r.inventory_id = i.inventory_id
131+
JOIN film f ON f.film_id = i.film_id
132+
GROUP BY i.film_id, LEFT(r.rental_date,7)
133+
ORDER BY 1, 2, 3;
134+
135+
/* Number of Rentals in Comedy , Sports and Family */
136+
SELECT c.name, COUNT(c.name) AS "Number of Rentals"
137+
FROM film f
138+
JOIN film_category fc ON fc.film_id = f.film_id
139+
JOIN category c ON c.category_id = fc.category_id
140+
JOIN inventory i ON f.film_id = i.film_id
141+
JOIN rental r ON r.inventory_id = i.inventory_id
142+
WHERE c.name IN ("Comedy", "Sports", "Family")
143+
GROUP BY 1;
144+
145+
/*Users who have been rented at least 3 times*/
146+
SELECT c.customer_id, CONCAT(c.first_name, " ", c.last_name) AS "Customer Name", COUNT(c.customer_id) AS "Total Rentals"
147+
FROM customer c
148+
JOIN rental r ON c.customer_id = r.customer_id
149+
GROUP BY 1
150+
HAVING COUNT(c.customer_id) >= 3
151+
ORDER BY 1;
152+
153+
/*How much revenue has one single store made over PG13 and R rated films*/
154+
SELECT s.store_id, f.rating, SUM(p.amount) AS "Total Revenue"
155+
FROM store s
156+
JOIN inventory i ON i.store_id = s.store_id
157+
JOIN rental r ON r.inventory_id = i.inventory_id
158+
JOIN payment p ON p.rental_id = r.rental_id
159+
JOIN film f ON f.film_id = i.film_id
160+
WHERE f.rating IN ("PG-13", "R")
161+
GROUP BY 1,2;
162+
163+
/*******************************************************/
164+
165+
/* Active User where active = 1*/
166+
DROP TEMPORARY TABLE IF EXISTS tbl_active_users;
167+
CREATE TEMPORARY TABLE tbl_active_users(
168+
SELECT c.*, a.phone
169+
FROM customer c
170+
JOIN address a ON a.address_id = c.address_id
171+
WHERE c.active = 1);
172+
173+
174+
/* Reward Users : who has rented at least 30 times*/
175+
DROP TEMPORARY TABLE IF EXISTS tbl_rewards_user;
176+
CREATE TEMPORARY TABLE tbl_rewards_user(
177+
SELECT r.customer_id, COUNT(r.customer_id) AS total_rents, max(r.rental_date) AS last_rental_date
178+
FROM rental r
179+
GROUP BY 1
180+
HAVING COUNT(r.customer_id) >= 30);
181+
182+
/* Reward Users who are also active */
183+
SELECT au.customer_id, au.first_name, au.last_name, au.email
184+
FROM tbl_rewards_user ru
185+
JOIN tbl_active_users au ON au.customer_id = ru.customer_id;
186+
187+
/* All Rewards Users with Phone */
188+
SELECT ru.customer_id, c.email, au.phone
189+
FROM tbl_rewards_user ru
190+
LEFT JOIN tbl_active_users au ON au.customer_id = ru.customer_id
191+
JOIN customer c ON c.customer_id = ru.customer_id;

0 commit comments

Comments
 (0)