Target SQL
Target SQL
select column_name,data_type
from `dsml-march-384117.Target_SQL.INFORMATION_SCHEMA.COLUMNS`
where table_name='customers'
--2. Get the time range between which the orders were
placed. (done)
select max(order_purchase_timestamp) as latest_date,min(order_purchase_timestamp)
as earliest_date
from `Target_SQL.Orders`;
INSIGHT: We can see that the first order was placed on 2016-09-04 and the latest
order is plaed on 2018-10-17
with cte as (
select extract(year from order_purchase_timestamp) as year,sum(t2.payment_value) as
total_payment_value from `Target_SQL.Orders` t1 join `Target_SQL.payments` t2 on
t1.order_id=t2.order_id
where extract(year from order_purchase_timestamp) >= 2017 and extract(month from
order_purchase_timestamp)<=8
group by extract(year from order_purchase_timestamp)),
cte2 as (
select max(case when year=2018 then total_payment_value end) as sum_2018, max(case
when year=2017 then total_payment_value end) as sum_2017
from cte)
select round(100*(sum_2018-sum_2017)/(sum_2017),2) as percent_increase from cte2;
-- 2.Find out the top 5 states with the highest & lowest
average freight value. (done )
with cte as (
select seller_state,avg(freight_value) as average_freight from
`Target_SQL.order_items`t1 join `Target_SQL.Orders` t2 on t1.order_id=t2.order_id
join `Target_SQL.sellers` t3 on t3.seller_id=t1.seller_id
group by seller_state)
select * from cte order by average_freight desc limit 5;
--3.Find out the top 5 states with the highest & lowest
average delivery time. (done)
(highest 5 states)
with cte as (
select
customer_state,avg(date_diff(order_delivered_customer_date,order_purchase_timestamp
,day)) as avg_time_to_delivery_in_days
from `Target_SQL.Orders` t1 join `Target_SQL.customers` t2 on
t1.customer_id=t2.customer_id
group by customer_state )
select * from cte order by cte.avg_time_to_delivery_in_days desc limit 5;
(lowest 5 states)
with cte as (
select
customer_state,avg(date_diff(order_delivered_customer_date,order_purchase_timestamp
,day)) as avg_time_to_delivery_in_days
from `Target_SQL.Orders` t1 join `Target_SQL.customers` t2 on
t1.customer_id=t2.customer_id
group by customer_state )
select * from cte order by cte.avg_time_to_delivery_in_days limit 5;
-- 4.Find out the top 5 states where the order delivery is
really fast as compared to the estimated date of delivery.
(done)
with cte as (
select
customer_state,avg(date_diff(order_estimated_delivery_date,order_delivered_customer
_date,day)) as day_diff from `Target_SQL.Orders` t1 join `Target_SQL.customers` t2
on t1.customer_id=t2.customer_id
group by customer_state)
select * from cte order by day_diff desc limit 5
Insights: There are the in which the deliveries are delivered quicker compared to the estimated
delivery time.
-- 6 Analysis based on the payments:
(done)
-- 1.Find the month on month no. of orders placed
using different payment types. (done)
select extract(year from order_purchase_timestamp
) as year_of_purchase,extract(month from
order_purchase_timestamp) as
month_of_purchase,payment_type,count(t1.order_id) as
number_of_orders
from `Target_SQL.payments` t1 join `Target_SQL.Orders` t2 on
t1.order_id=t2.order_id
group by payment_type,extract(year from
order_purchase_timestamp),extract(month from
order_purchase_timestamp);
INSIGHTS:
Number of orders for the payment_installment 1 is outweighing the other payment instalments.
OVERALL INSIGHTS:
• Customer behaviour: Customers tend to buy more between 12:00 and 19:00. We can
introduce discounts, offers and deals during this period to increase the sales further.
• Operational Efficiency: States AC RO AP MP RR are efficient in terms of delivering the orders
way within time.
• Performance metrics: The sales have increased around 136% from the previous previous
year for in 2017 and 2018 for the months jan to august.
• Payment type: Majority of the payments include credit card as the payment method. So we
can issue some offers or discounts on the credit card payments to increase the sales further.
• Delivery time: States like RR ap am al pl have the highest average delivery time. This could be
because of the operational inefficiency or the lack of proper infrastructure. We should invest
in these states.