-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRETAIL_SALES_ANALYSIS_sql_query.sql
More file actions
115 lines (89 loc) · 3.39 KB
/
Copy pathRETAIL_SALES_ANALYSIS_sql_query.sql
File metadata and controls
115 lines (89 loc) · 3.39 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
-- PROJECT -> RETAIL SALES ANALYSIS
CREATE DATABASE sql_project1;
USE sql_project1;
-- CREATING TABLE
DROP TABLE IF EXISTS retail_sales;
CREATE TABLE retail_sales (
transactions_id int primary key,
sale_date date,
sale_time time,
customer_id int,
gender varchar(8),
age int(2),
category varchar(15),
quantiy int,
price_per_unit float,
cogs float,
total_sale float
);
select * from retail_sales limit 10;
select count(*) from retail_sales;
select * from retail_sales limit 500;
-- CHECKING NULL VALUES - DATA CLEANING
select * from retail_sales
where
transactions_id is null
or
sale_date is null
or
sale_time is null
or
customer_id is null
or
gender is null
or
age is null
or
category is null
or
quantiy is null
or
price_per_unit is null
or
cogs is null
or total_sale is null;
-- DATA EXPLORATION
-- How many sales do we have?
SELECT COUNT(TRANSACTIONS_ID) AS TOTAL_SALES FROM RETAIL_SALES;
-- How many unique customers do we have?
select count(distinct customer_id) from retail_sales;
-- DATA ANALYSIS FOR BUSINESS KEY PROBLEMS
-- My Analysis & Findings
-- Q.1 Write a SQL query to retrieve all columns for sales made on '2022-11-05
-- Q.2 Write a SQL query to retrieve all transactions where the category is 'Clothing' and the quantity sold is more than 4 in the month of Nov-2022
-- Q.3 Write a SQL query to calculate the total sales (total_sale) for each category.
-- Q.4 Write a SQL query to find the average age of customers who purchased items from the 'Beauty' category.
-- Q.5 Write a SQL query to find all transactions where the total_sale is greater than 1000.
-- Q.6 Write a SQL query to find the total number of transactions (transaction_id) made by each gender in each category.
-- Q.7 Write a SQL query to calculate the average sale for each month. Find out best selling month in each year
-- Q.8 Write a SQL query to find the top 5 customers based on the highest total sales
-- Q.9 Write a SQL query to find the number of unique customers who purchased items from each category.
-- Q.10 Write a SQL query to create each shift and number of orders (Example Morning <=12, Afternoon Between 12 & 17, Evening >17)
-- SOLUTION Q1
SELECT * FROM retail_sales where sale_date = '2022-11-05';
-- SOLUTION Q2
SELECT * from retail_sales where category = 'Clothing' and quantiy >= 4 and date_format(sale_date, '%Y-%m') = '2022-11';
-- SOLUTION Q3
SELECT category,sum(total_sale) as net_sale, count(*) as total_orders from retail_sales group by category;
-- SOLUTION Q4
SELECT avg(age) FROM retail_sales where category = 'Beauty';
-- SOLUTION Q5
SELECT * from retail_sales where total_sale >1000;
-- SOLUTION Q6
SELECT category,gender, count(transactions_id) from retail_sales group by category,gender order by category asc;
-- SOLUTION Q7
SELECT Year(sale_date) as Year,Month(sale_date) as Month, avg(total_sale) as Average_Sale from retail_sales group by Year,Month;
-- SOLUTION Q8
SELECT customer_id, SUM(total_sale) AS total_sales FROM retail_sales GROUP BY customer_id ORDER BY total_sales DESC LIMIT 5;
-- SOLUTION Q9
select category, count(distinct(customer_id)) as No_of_customers from retail_sales group by category;
-- SOLUTION Q10
SELECT
CASE
WHEN HOUR(sale_time) < 12 THEN 'Morning'
WHEN HOUR(sale_time) BETWEEN 12 AND 17 THEN 'Afternoon'
ELSE 'Evening'
END AS shift,
COUNT(transactions_id) AS number_of_orders
FROM retail_sales
GROUP BY shift;