-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL Query.sql
More file actions
201 lines (124 loc) · 4.87 KB
/
Copy pathSQL Query.sql
File metadata and controls
201 lines (124 loc) · 4.87 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
200
201
select * from details
Sure! Here are the questions without answers:
### Basic SQL Questions
1. Retrieve all orders placed by a specific customer.
select * from details
where "Customer_Name" = 'Darren Powers'
2. Calculate the total sales for each product.
select "Product_Name" as product, sum("Sales") as totalsales from details
group by product order by totalsales desc
3. Count the number of orders for each shipping mode.
select "Ship_Mode" as shipping_mode, count("Order_ID") as c_ord
from details
group by shipping_mode
order by c_ord desc
4. Find the total sales for each region.
select "Region" as region, sum("Sales") as totalsales from details
group by region order by totalsales desc
5. List all unique customer names.
select distinct "Customer_Name" from details
### Intermediate SQL Questions
6. Identify the top 5 most profitable products.
select "Product_Name" as products, sum("Profit") as profit from details
group by products order by profit desc limit 5;
7. Calculate total sales for each month in the dataset.
SELECT TO_CHAR("Order_Date"::timestamp, 'Month') AS order_month,
extract(month from "Order_Date"::timestamp) as months,
sum("Sales") as total_sales
FROM details
group by order_month,months
order by months asc
;
8. Find all orders where a discount was applied.
select * from details
where "Discount" > 0
9. Calculate the average profit per order for each segment.
select "Segment" as segment, round(avg("Profit")::numeric,2) as profits from details
group by segment
order by profits desc
10. Retrieve the total quantity sold for each product category.
select "Category" as category, sum("Quantity") as quantity from details
group by category
order by quantity desc
### Advanced SQL Questions
11. Find the top 3 customers who contributed the highest total sales.
select "Customer_Name" as customers, sum("Sales") as total_sales from details
group by customers
order by total_sales desc limit 3
12. Identify the most frequently ordered product in each region.
select "Region" as region, "Product_Name" as product, sum("Quantity") as total_quantity from details
group by region,product
order by total_quantity desc
13. Calculate the average shipping time (days) for orders by ship mode.
select "Ship_Mode" as ship_mode, avg("Order_Date"-"Ship_Date") as ship_time from details
group by ship_mode
14. Identify products with a profit margin (profit/sales) below a specified threshold.
SELECT "Product_Name", SUM("Profit") / NULLIF(SUM("Sales"), 0) AS profit_margin
FROM details
GROUP BY "Product_Name"
HAVING (SUM("Profit") / NULLIF(SUM("Sales"), 0)) < 0.2;
select sum("Profit") / NULLIF(sum("Sales"),0) as profit_margin
from details
15. Retrieve the total sales, quantity, and profit for each city, ordered by profit.
select "City" as city, sum("Sales") as sales, sum("Quantity") as quantity,
sum("Profit") as profit from details
group by city
order by profit desc
16. Find the top 5 profitable customers for each region.
SELECT *
FROM (
SELECT "Region" AS region,
"Customer_Name" AS customers,
SUM("Profit") AS profits,
DENSE_RANK() OVER (PARTITION BY "Region" ORDER BY SUM("Profit") DESC) AS ranks
FROM details
GROUP BY "Region", "Customer_Name"
) AS ranked_customers
WHERE ranks <= 5
ORDER BY region, profits DESC;
17. Calculate the cumulative sales for each product over time.
SELECT "Product_Name",
"Order_Date",
SUM("Sales") OVER (PARTITION BY "Product_Name" ORDER BY "Order_Date" ASC) AS cumulative_sales
FROM details
ORDER BY "Product_Name", "Order_Date";
18. Compare total sales and profit for each category across different regions.
SELECT "Region" AS region,
"Category" AS category,
SUM("Sales") AS sales,
SUM("Profit") AS profit
FROM details
GROUP BY region, category
ORDER BY region, category;
19. Identify the top 3 profitable products for each month.
select * from
(SELECT TO_CHAR("Order_Date"::timestamp, 'Month') AS months,
extract(month from "Order_Date"::timestamp) as months_no,
"Product_Name" AS product,
SUM("Profit") AS total_profit,
DENSE_RANK() OVER (PARTITION BY TO_CHAR("Order_Date"::timestamp, 'Month')
ORDER BY SUM("Profit") DESC) AS ranks
FROM details
GROUP BY months,months_no,product) as a
where ranks <= 3
order by months_no,ranks asc
20. Determine the customer with the highest average sales per order in each segment.
create view Customer_Avg_Sales AS (
SELECT "Segment" AS segment,
"Customer_Name" AS customers,
AVG("Sales") AS avg_sales
FROM details
GROUP BY "Segment", "Customer_Name"
)
SELECT segment,
customers,
avg_sales
FROM (
SELECT segment,
customers,
avg_sales,
ROW_NUMBER() OVER (PARTITION BY segment ORDER BY avg_sales DESC) AS rank
FROM Customer_Avg_Sales
) AS ranked_customers
WHERE rank = 1
ORDER BY segment;