-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.sql
More file actions
199 lines (163 loc) · 5.25 KB
/
Copy pathQuery.sql
File metadata and controls
199 lines (163 loc) · 5.25 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/********************************************************************************************
* Project Title: SQL Retail Sales Analysis (P1)
* Author: Dhanesh Gaikwad
* Description:
* This project demonstrates SQL skills through data cleaning, exploration, and analysis
* using a simulated retail sales dataset. The objective is to extract valuable business
* insights, identify key trends, and answer analytical questions from transactional data.
********************************************************************************************/
-- STEP 1: CREATE DATABASE
CREATE DATABASE sql_project_p2;
-- STEP 2: CREATE TABLE
DROP TABLE IF EXISTS retail_sales;
CREATE TABLE retail_sales (
transaction_id INT PRIMARY KEY,
sale_date DATE,
sale_time TIME,
customer_id INT,
gender VARCHAR(15),
age INT,
category VARCHAR(15),
quantity INT,
price_per_unit FLOAT,
cogs FLOAT,
total_sale FLOAT
);
-- STEP 3: VIEW SAMPLE DATA
SELECT *
FROM retail_sales
LIMIT 10;
-- STEP 4: RECORD COUNT
SELECT COUNT(*) AS total_records
FROM retail_sales;
--------------------------------------------------------------------------------------------
-- SECTION 1: DATA CLEANING
--------------------------------------------------------------------------------------------
-- Checking for missing or null values in key columns
SELECT * FROM retail_sales
WHERE transaction_id IS NULL
OR sale_date IS NULL
OR sale_time IS NULL
OR gender IS NULL
OR category IS NULL
OR quantity IS NULL
OR cogs IS NULL
OR total_sale IS NULL;
-- Deleting records with missing values
DELETE FROM retail_sales
WHERE transaction_id IS NULL
OR sale_date IS NULL
OR sale_time IS NULL
OR gender IS NULL
OR category IS NULL
OR quantity IS NULL
OR cogs IS NULL
OR total_sale IS NULL;
--------------------------------------------------------------------------------------------
-- SECTION 2: DATA EXPLORATION
--------------------------------------------------------------------------------------------
-- Total number of sales records
SELECT COUNT(*) AS total_sales
FROM retail_sales;
-- Total number of unique customers
SELECT COUNT(DISTINCT customer_id) AS unique_customers
FROM retail_sales;
-- Distinct product categories
SELECT DISTINCT category
FROM retail_sales;
--------------------------------------------------------------------------------------------
-- SECTION 3: BUSINESS QUESTIONS & ANALYSIS
--------------------------------------------------------------------------------------------
/*
Q1. Retrieve all sales made on '2022-11-05'.
Q2. Retrieve all transactions where category = 'Clothing'
and quantity sold > 4 during November 2022.
Q3. Calculate total sales and order count per category.
Q4. Find the average age of customers who purchased from 'Beauty'.
Q5. Retrieve all transactions where total_sale > 1000.
Q6. Find total transactions by gender for each category.
Q7. Calculate the average monthly sales and identify the best-selling month each year.
Q8. Find the top 5 customers based on highest total sales.
Q9. Find the number of unique customers per category.
Q10. Categorize sales by shift (Morning, Afternoon, Evening) based on sale_time.
*/
-- Q1: Retrieve all sales made on '2022-11-05'
SELECT *
FROM retail_sales
WHERE sale_date = '2022-11-05';
-- Q2: Clothing category transactions with quantity > 4 in November 2022
SELECT *
FROM retail_sales
WHERE category = 'Clothing'
AND TO_CHAR(sale_date, 'YYYY-MM') = '2022-11'
AND quantity > 4;
-- Q3: Total sales and order count per category
SELECT
category,
SUM(total_sale) AS total_revenue,
COUNT(*) AS total_orders
FROM retail_sales
GROUP BY category;
-- Q4: Average age of customers purchasing from 'Beauty'
SELECT
ROUND(AVG(age), 2) AS avg_age
FROM retail_sales
WHERE category = 'Beauty';
-- Q5: Transactions with total sale > 1000
SELECT *
FROM retail_sales
WHERE total_sale > 1000;
-- Q6: Total transactions by gender in each category
SELECT
category,
gender,
COUNT(*) AS total_transactions
FROM retail_sales
GROUP BY category, gender
ORDER BY category;
-- Q7: Best-selling month each year based on average monthly sales
SELECT
year,
month,
avg_sale
FROM (
SELECT
EXTRACT(YEAR FROM sale_date) AS year,
EXTRACT(MONTH FROM sale_date) AS month,
AVG(total_sale) AS avg_sale,
RANK() OVER (PARTITION BY EXTRACT(YEAR FROM sale_date)
ORDER BY AVG(total_sale) DESC) AS rank
FROM retail_sales
GROUP BY 1, 2
) ranked_sales
WHERE rank = 1;
-- Q8: Top 5 customers based on total sales
SELECT
customer_id,
SUM(total_sale) AS total_sales
FROM retail_sales
GROUP BY customer_id
ORDER BY total_sales DESC
LIMIT 5;
-- Q9: Unique customers per product category
SELECT
category,
COUNT(DISTINCT customer_id) AS unique_customers
FROM retail_sales
GROUP BY category;
-- Q10: Orders by time-of-day (shift classification)
WITH hourly_sales AS (
SELECT *,
CASE
WHEN EXTRACT(HOUR FROM sale_time) < 12 THEN 'Morning'
WHEN EXTRACT(HOUR FROM sale_time) BETWEEN 12 AND 17 THEN 'Afternoon'
ELSE 'Evening'
END AS shift
FROM retail_sales
)
SELECT
shift,
COUNT(*) AS total_orders
FROM hourly_sales
GROUP BY shift
ORDER BY total_orders DESC;