Skip to content

Commit 9f62414

Browse files
Add files via upload
1 parent 56a09f6 commit 9f62414

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
This file contains the solutions of all the tasks. After completing the assigned tasks or Questions refer this file for correction
3+
4+
Thank You
5+
*/
6+
select * from customers;
7+
# Sample:- How many customers are male and female?
8+
9+
select gender , count(*) from customers
10+
group by gender;
11+
12+
/* BASIC SQL QUESTIONS */
13+
14+
# Q1. How many customers do not have DOB information available?
15+
16+
select count(*) from customers where dob is null;
17+
18+
19+
# Q2-- How many customers are there in each pincode and gender combination?
20+
select pincode, gender, count(cust_id) from customers
21+
group by pincode, gender
22+
order by pincode desc;
23+
24+
25+
# Q3-- Print product name and mrp for products which have more than 50000 MRP?
26+
27+
select product_name, mrp
28+
from products
29+
where mrp>50000;
30+
31+
# Q4. How many delivery personal are there in each pincode?
32+
33+
select pincode, count(person_id) from delivery_person
34+
group by pincode;
35+
36+
/* Q5. For each Pin code, print the count of orders, sum of total amount paid, average amount paid, maximum amount paid,
37+
minimum amount paid for the transactions which were paid by 'cash'.
38+
Take only 'buy' order types
39+
*/
40+
41+
select delivery_pincode as pd , count(order_id), sum(total_amount_paid), avg(total_amount_paid), max(total_amount_paid)
42+
,min(total_amount_paid)
43+
from orders
44+
where payment_type="cash" and order_type="buy"
45+
group by pd;
46+
47+
/*
48+
Q6. For each delivery_person_id, print the count of orders and
49+
total amount paid for product_id = 12350 or 12348 and total units > 8.
50+
Sort the output by total amount paid in descending order. Take only 'buy' order types
51+
*/
52+
53+
select delivery_person_id as pd, count(order_id), sum(total_amount_paid) as tap from orders
54+
where product_id IN (12348,12350)
55+
and tot_units>8 and order_type="buy"
56+
group by pd
57+
order by tap desc;
58+
59+
# Q7. Print the Full names (first name plus last name) for customers that have email on "gmail.com"?
60+
select concat(first_name," ",last_name) as Name from customers
61+
where email like "%gmail%";
62+
63+
# Q8. How many orders had #units between 1-3, 4-6 and 7+? Take only 'buy' order types
64+
select
65+
case
66+
when tot_units<3 then "1-3"
67+
when tot_units>=4 and tot_units<=6 then "4-6"
68+
else "7+"
69+
end as cat,
70+
count(order_id) from orders
71+
where order_type="buy"
72+
group by cat
73+
order by cat;
74+
75+
# Q9. Which pincode has average amount paid more than 150,000? Take only 'buy' order types
76+
77+
select delivery_pincode as pd, avg(total_amount_paid) as avge
78+
from orders
79+
where order_type="buy"
80+
group by pd
81+
having avge>150000;
82+
83+
/* Q10. Create following columns from order_dim data -
84+
85+
order_date
86+
Order day
87+
Order month
88+
Order year */
89+
90+
select order_date,
91+
substr(order_date,1,2) as order_day,
92+
substr(order_date,4,2) as order_month,
93+
substr(order_date,7) as order_year
94+
from orders
95+
where order_type="buy";
96+
97+
/* Q11. How many total orders were there in each month and how many of them were returned? Add a column for return rate too.
98+
return rate = (100.0 * total return orders) / total buy orders
99+
Hint: You will need to combine SUM() with CASE WHEN
100+
*/
101+
with cte as
102+
(
103+
select substr(order_date, 4,2) as mon,
104+
sum(case when order_type="return" then 1 else 0 end) as Total_Returns,
105+
sum(case when order_type="buy" then 1 else 0 end) as Total_buys
106+
from orders
107+
group by mon)
108+
select *, round((total_returns/total_buys)*100,1) as Return_Rate from cte
109+
order by return_rate desc;
110+
111+
112+
# QUESTION ON SQL JOINS
113+
114+
# Q12. How many units have been sold by each brand? Also get total returned units for each brand.
115+
116+
select p.brand, sum(case when order_type="buy" then o.tot_units end) from products p inner join orders o ON
117+
p.product_id=o.product_id
118+
group by p.brand;
119+
120+
121+
# Q13. How many distinct customers and delivery boys are there in each state?
122+
123+
select p.state, count(distinct c.cust_id) as Customers, count(distinct dp.person_id) as Delivery_Man
124+
from pincode p inner join customers c
125+
ON p.pin_id=c.pincode
126+
inner join delivery_person dp
127+
ON p.pin_id=dp.pincode
128+
group by p.state;
129+
130+
131+
/* Q14. For every customer, print how many total units were ordered, how many units were
132+
ordered from their primary_pincode and how many were ordered not from the primary_pincode.
133+
Also calulate the percentage of total units which were ordered from
134+
primary_pincode(remember to multiply the numerator by 100.0). Sort by the percentage column in descending order.
135+
*/
136+
137+
select c.cust_id, sum(o.tot_units)as Total_orders,
138+
sum(case when c.pincode=o.delivery_pincode then o.tot_units else 0 end) as Same_city,
139+
sum(case when c.pincode!=o.delivery_pincode then o.tot_units else 0 end) as Diff_city
140+
from customers c
141+
inner join orders o
142+
ON c.cust_id=o.cust_id
143+
where o.order_type="buy"
144+
group by c.cust_id;
145+
146+
147+
/* Task 15
148+
For each product name, print the sum of number of units, total amount paid,
149+
total displayed selling price, total mrp of these units, and finally the net discount from selling price
150+
(i.e. 100.0 - 100.0 * total amount paid / total displayed selling price)
151+
AND the net discount from mrp (i.e. 100.0 - 100.0 * total amount paid / total mrp)
152+
*/
153+
154+
with cte as
155+
(
156+
select p.product_name, sum(o.tot_units) as units, sum(o.total_amount_paid) as Total_Amount_Paid,
157+
sum(o.tot_units*o.displayed_selling_price_per_unit) as Total_Display_Price, sum(o.tot_units*p.mrp) as Total_MRP
158+
from products p left join orders o
159+
ON p.product_id = o.product_id
160+
group by p.product_name
161+
)
162+
select *, 100-((total_amount_paid/total_display_price)*100) as Net_SP_discount,
163+
100-((total_amount_paid/total_mrp)*100) as Net_MRP_discount from cte;
164+
165+
/* Task 16
166+
For every order_id (exclude returns), get the product name and calculate the discount
167+
percentage from selling price. Sort by highest discount and print only
168+
those rows where discount percentage was above 10.10%.
169+
*/
170+
171+
select o.order_id,p.product_name,
172+
round((((o.tot_units*o.displayed_selling_price_per_unit)-o.total_amount_paid)/(o.tot_units*o.displayed_selling_price_per_unit))*100,2) as discount
173+
from orders o left join products p ON o.product_id=p.product_id
174+
where order_type="buy"
175+
group by o.order_id, p.product_name
176+
having discount>=10.10;
177+
178+
/* Task 17
179+
Using the per unit procurement cost in product_dim, find which product category has made the most profit in both absolute amount and percentage
180+
Absolute Profit = Total Amt Sold - Total Procurement Cost
181+
Percentage Profit = 100.0 * Total Amt Sold / Total Procurement Cost - 100.0
182+
*/
183+
184+
with cte as
185+
(
186+
select p.category as cat, sum(o.tot_units) as Units_Sold, sum(o.tot_units*procurement_cost_per_unit) as Total_Cost, sum(total_amount_paid) as Revenue
187+
from products p left join orders o
188+
ON p.product_id=o.product_id
189+
group by p.category
190+
)
191+
select cat, Revenue-total_cost as Absolute_Profit, 100-((total_cost/revenue)*100) as Percentage_Profit from cte;
192+
193+
/* Task 18
194+
For every delivery person(use their name), print the total number of order ids (exclude returns)
195+
by month in seperate columns i.e. there should be one row for each delivery_person_id and 12 columns for every month in the year
196+
*/
197+
198+
select dp.name as Delivery_Man,
199+
sum(case when substr(o.delivery_date,4,2)=01 then 1 else 0 end) as Jan,
200+
sum(case when substr(o.delivery_date,4,2)=02 then 1 else 0 end) as Feb,
201+
sum(case when substr(o.delivery_date,4,2)=03 then 1 else 0 end) as Mar,
202+
sum(case when substr(o.delivery_date,4,2)=04 then 1 else 0 end) as Apr,
203+
sum(case when substr(o.delivery_date,4,2)=05 then 1 else 0 end) as May,
204+
sum(case when substr(o.delivery_date,4,2)=06 then 1 else 0 end) as Jun,
205+
sum(case when substr(o.delivery_date,4,2)=07 then 1 else 0 end) as Jul,
206+
sum(case when substr(o.delivery_date,4,2)=08 then 1 else 0 end) as Aug,
207+
sum(case when substr(o.delivery_date,4,2)=09 then 1 else 0 end) as Sept,
208+
sum(case when substr(o.delivery_date,4,2)=10 then 1 else 0 end) as Oct,
209+
sum(case when substr(o.delivery_date,4,2)=11 then 1 else 0 end) as Nov,
210+
sum(case when substr(o.delivery_date,4,2)=12 then 1 else 0 end) as Decem
211+
from delivery_person dp inner join orders o
212+
ON dp.person_id=o.delivery_person_id
213+
where o.order_type="buy"
214+
group by dp.name
215+
order by dp.name;
216+
217+
/* Task 19
218+
For each gender - male and female - find the absolute and percentage profit (like in Q16) by product name
219+
*/
220+
221+
222+
with cte as
223+
(
224+
select c.gender,p.category as cat, sum(tot_units) as Total_units, sum(total_amount_paid) as Amount_Paid,
225+
sum(p.procurement_cost_per_unit*o.tot_units) as Total_Cost
226+
from customers c left join orders o
227+
ON c.cust_id = o.cust_id
228+
inner join products p
229+
ON o.product_id=p.product_id
230+
where o.order_type="buy"
231+
group by c.gender,p.category
232+
order by c.gender
233+
)
234+
select gender,cat, Amount_paid-total_cost as Abs_Profit,
235+
100-((total_cost/amount_paid)*100) as Profit from cte;
236+
237+
238+
/* Task 20
239+
Generally the more numbers of units you buy, the more discount seller will give you.
240+
For 'Dell AX420' is there a relationship between number of units ordered and average discount
241+
from selling price? Take only 'buy' order types
242+
*/
243+
244+
select o.tot_units, (avg(o.displayed_selling_price_per_unit*o.tot_units)-avg(o.total_amount_paid)) as Average_discount
245+
from orders o inner join products p
246+
ON o.product_id=p.product_id
247+
where p.product_name="Dell AX420" and o.order_type="buy"
248+
group by o.tot_units
249+
order by o.tot_units;
250+

s1- sales_database.xlsx

93.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)