-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExploring Sales Data in SQL.sql
197 lines (137 loc) · 4.21 KB
/
Exploring Sales Data in SQL.sql
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
--In this project we explore a sales dataset and generate various
--analytics and insights from customers' past purchase behavior.
--Inspecting data
select *
from PortfolioProject..sales_data_sample
--checking unique values
select distinct STATUS
from sales_data_sample
select distinct YEAR_ID
from sales_data_sample
select distinct PRODUCTLINE
from sales_data_sample
select distinct COUNTRY
from sales_data_sample
select distinct DEALSIZE
from sales_data_sample
select distinct TERRITORY
from sales_data_sample
--ANALYSIS
---Grouping the sales by country
select COUNTRY,
sum(SALES) as Total_sales
from sales_data_sample
group by COUNTRY
order by 2 desc
---Grouping the sales by Productline
select PRODUCTLINE,
sum(SALES) as Total_sales
from sales_data_sample
group by PRODUCTLINE
order by 2 desc
---Grouping the sales by Year_id
select YEAR_ID,
sum(SALES) as Total_sales
from sales_data_sample
group by YEAR_ID
order by 2 desc
---Grouping the sales by Territory
select TERRITORY,
sum(SALES) as Total_sales
from sales_data_sample
group by TERRITORY
order by 2 desc
---Grouping the sales by Dealsize
select DEALSIZE,
sum(SALES) as Total_sales
from sales_data_sample
group by DEALSIZE
order by 2 desc
-- Looking at the distinct month id of each year
select distinct MONTH_ID
from sales_data_sample
where YEAR_ID = '2003'
select distinct MONTH_ID
from sales_data_sample
where YEAR_ID = '2004'
select distinct MONTH_ID
from sales_data_sample
where YEAR_ID = '2005'
--By the above queries we got the information that in 2005 sales is lowest,
--it may be beacause of they have operated only for 5 years in the year 2005
--what was the best month for sale in the specific year,how much was earned that month?
select
MONTH_ID,
COUNT(ORDERNUMBER) AS Total_orders,
SUM(SALES) as Total_Sales
from sales_data_sample
where YEAR_ID ='2003'
group by MONTH_ID
order by 2 desc
-- * In 2003 the best month for the sale is 11(November)
select
MONTH_ID,
SUM(SALES) as Total_Sales
from sales_data_sample
where YEAR_ID ='2004'
group by MONTH_ID
order by 2 desc
-- * In 2004 the best month for the sale is 11(November)
select
MONTH_ID,
SUM(SALES) as Total_Sales
from sales_data_sample
where YEAR_ID ='2005'
group by MONTH_ID
order by 2 desc
-- * In 2005 the best month for the sale is 5(May)
--November seems to be the month, what product do they sell in November
select
PRODUCTCODE,
COUNT(PRODUCTCODE) AS Total_no_products_sold
from sales_data_sample
where YEAR_ID ='2003' and MONTH_ID = '11'
group by PRODUCTCODE
order by 2 desc
--Which product line they have used most
select
PRODUCTLINE,
COUNT(PRODUCTLINE) AS Total_no_productline
from sales_data_sample
where YEAR_ID ='2003' and MONTH_ID = '11'
group by PRODUCTLINE
order by 2 desc
--* From above we got the result that Classic Cars is the productline which they have used most of the time
select *
from PortfolioProject..sales_data_sample
--Who is best customer ?
-- To calculate this we have to calculate the total sales of the individual customers and order it in
-- decending order
select CUSTOMERNAME,
COUNT(ORDERNUMBER) as Total_orders
from PortfolioProject..sales_data_sample
group by CUSTOMERNAME
order by COUNT(ORDERNUMBER) desc
select CUSTOMERNAME,
sum(SALES) as Total_sales_per_customer
from PortfolioProject..sales_data_sample
group by CUSTOMERNAME
order by COUNT(SALES) desc
-- Conc -> From the above queries we can see that Euro shopping channel is the best customer
-- Which city has the highest nummber of the sales in the specific country
select CITY,
sum(SALES) as Total_sales
from PortfolioProject..sales_data_sample
where COUNTRY = 'USA'-- by changing the country we can get the data for diffferent country
group by CITY
order by Total_sales desc
-- Which is the best product in the United State
select PRODUCTCODE,
COUNT(PRODUCTCODE) as Total_product_sold
from PortfolioProject..sales_data_sample
where COUNTRY = 'USA'
group by PRODUCTCODE
order by Total_product_sold desc
-- So, S18_3232 productcode is the best product in the USA
select *
from PortfolioProject..sales_data_sample