0% found this document useful (0 votes)
9 views12 pages

Target Junaid

The document outlines various SQL queries and insights related to customer orders, including data on order trends, customer distribution, and delivery times. Key findings indicate a significant increase in orders from 2017 to 2018, with a notable decline in orders after August 2018, and a concentration of customers in a few states. Recommendations include investigating the reasons for order declines and improving customer engagement in underrepresented states.

Uploaded by

justanswerjunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Target Junaid

The document outlines various SQL queries and insights related to customer orders, including data on order trends, customer distribution, and delivery times. Key findings indicate a significant increase in orders from 2017 to 2018, with a notable decline in orders after August 2018, and a concentration of customers in a few states. Recommendations include investigating the reasons for order declines and improving customer engagement in underrepresented states.

Uploaded by

justanswerjunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Question 1:
1. Data type of all columns in the "customers" table.

SELECT *
FROM Target.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'customers';

Insights: “customer_id” column in Customers Table can be nullable is observed from the schema, so I
believe it is not a primary key.

2. Get the time range between which the orders were placed
SELECT
MAX(order_purchase_timestamp), MIN(order_purchase_timestamp)
FROM
`Target.orders`

Insights: This shows that the orders data we have is from Sep-2016 9:15 pm to Oct-2018 5:30 pm which is
close to 2 years 1 month’s data

3. Count the Cities & States of customers who ordered during the given period.

SELECT
COUNT(distinct customer_state) as `No_of_States`,
COUNT(distinct customer_city) as `No_of_Cities`
FROM
`Target. Customers` as c
INNER JOIN
`Target.orders` as o
on c.customer_id = o.customer_id

Insights: Every state of the Brazil has placed orders through Target (including 26 states and 1 Federal
District). Back by 2019 Target was serving 73% (total cities 5573 by 2019) cities in Brazil according to the
given data.
2. Question:
1. Is there a growing trend in the no. of orders placed over the past years?
SELECT
EXTRACT(year FROM order_purchase_timestamp) as `Years`,
COUNT(order_id) AS `Number_of_orders`
FROM `Target.orders`
GROUP BY `Years`
ORDER BY `Years` ASC

Insights: 19% more orders are placed in the year 2018 (10 months) compared to year the 2017 (12 months).
(Avoided year 2016, as the data is available for last quarter). However, the orders in 2016 in the last quarter
is very less (329)

Recommendations: Monthly analysis would give us more insights to understand the behaviour of orders.

2. Can we see some kind of monthly seasonality in terms of the no. of orders being placed?
With
Y2016 AS
(SELECT
EXTRACT(year FROM order_purchase_timestamp) as `Years_2016`,
EXTRACT(month FROM order_purchase_timestamp) as `Months_2016`,
COUNT(order_id) AS `Number_of_orders_2016`
FROM `Target.orders`
GROUP BY `Years_2016`, `Months_2016`
HAVING `Years_2016` = 2016
ORDER BY `Years_2016` asc, `Months_2016` asc),

Y2017 AS
(SELECT
EXTRACT(year FROM order_purchase_timestamp) as `Years_2017`,
EXTRACT(month FROM order_purchase_timestamp) as `Months_2017`,
COUNT(order_id) AS `Number_of_orders_2017`
FROM `Target.orders`
GROUP BY `Years_2017`, `Months_2017`
HAVING `Years_2017` = 2017
ORDER BY `Years_2017` asc, `Months_2017` asc),

Y2018 AS
(SELECT
EXTRACT(year FROM order_purchase_timestamp) as `Years_2018`,
EXTRACT(month FROM order_purchase_timestamp) as `Months_2018`,
COUNT(order_id) AS `Number_of_orders_2018`
FROM `Target.orders`
GROUP BY `Years_2018`, `Months_2018`
HAVING `Years_2018` = 2018
ORDER BY `Years_2018` asc, `Months_2018` asc)

SELECT
Months_2016,Number_of_orders_2016,Months_2017,Number_of_orders_2017,Months_2018,Number
_of_orders_2018
FROM
(SELECT *
FROM
Y2016 RIGHT JOIN Y2017
ON Months_2016 = Months_2017) as `Y_16_17`
LEFT JOIN
Y2018 ON Months_2017= Months_2018
order by Months_2017

Insights: From the results we can observe that there was an increase in orders from Jan 2017 till Aug
2018 and after that there is a huge downfall in orders (from 6k to 4). I could say is there is no monthly
or seasonal effect on the orders. It looks like this could be a new supermarket opened during 2016 and
orders increased over a period of 1.5 years and something might have happened in Aug 2018.

Recommendations: We need to investigate the circumstances which made the downfall happen.

3. During what time of the day, do the Brazilian customers mostly place their orders? (Dawn, Morning,
Afternoon or Night)
▪ 0-6 hrs : Dawn
▪ 7-12 hrs : Mornings
▪ 13-18 hrs : Afternoon
▪ 19-23 hrs : Night
SELECT
if(hrs>=0 and hrs<7,"Dawn",if(hrs>=7 and hrs<13,"Mornings",if(hrs>=13 and
hrs<19,"Afternoon","Night"))) as Time_of_day,
sum(orders) as `Orders`
FROM
(
SELECT
EXTRACT(HOUR FROM TIME(order_purchase_timestamp)) as hrs, count(order_id) as `orders`
FROM
`Target.orders`
group by hrs
order by hrs
)
group by Time_of_day
Insights: More orders were placed during Afternoon.

Recommendations: NA

3. Question:
1. Get the month on month no. of orders placed in each state:
With

Year_2016 AS
(SELECT
c.customer_state as state_16,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2016`,
count(o.order_id) as `orders_2016`
FROM
`Target.customers` as c
INNER JOIN
`Target.orders` as o
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state, `Year`,`Month_2016`
HAVING
`Year` = 2016
order by `Month_2016`, c.customer_state),

Year_2017 AS
(SELECT
c.customer_state as state_17,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2017`,
count(o.order_id) as `orders_2017`
FROM
`Target.customers` as c
INNER JOIN
`Target.orders` as o
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state, `Year`,`Month_2017`
HAVING
`Year` = 2017
order by `Month_2017`, c.customer_state),

Year_2018 AS
(SELECT
c.customer_state as state_18,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2018`,
count(o.order_id) as `orders_2018`
FROM
`Target.customers` as c
INNER JOIN
`Target.orders` as o
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state, `Year`,`Month_2018`
HAVING
`Year` = 2018
order by `Month_2018`, c.customer_state)

SELECT
state_16,Month_2016,orders_2016,state_17,Month_2017,orders_2017,state_18,Month_2018,orders_
2018
FROM
(SELECT
*
FROM
Year_2016 RIGHT JOIN Year_2017
ON state_16 = state_17 and Month_2016 = Month_2017) as `Y_16_17`
LEFT JOIN
Year_2018
ON
state_17 = state_18 and Month_2017 = Month_2018
order by state_17 ASC, Month_2017 ASC

Insights:
State wise insights: Increased(↑), Reduced (↓), No Change (↔)
Orders (↑) across Years: BA, DF, ES, GO, MS, MT, PB, PE, PR, RS, CE (↓in Aug 2018), PA (↓in Aug 2018),
MG (↓in Sep 2018), SC (↓in Sep 2018), SP (↓in Months Sep & Oct)
Orders (↑) in 2017 and (↓) in 2018: PI, RJ
(↔) across the years: AC, AL, AM, AP, MA, RN, RO, RR, SE, TO

Overall, there is good business from 10 states and in other five states it started reducing from Aug 2018
onwards.
In 10 other states the business is almost constant but not great numbers.
In 2 states business reduced.

Recommendations: We need to investigate on 7 states where the sudden drop is seen in business and
need to work on improving the business in the states where the business is constant and order
numbers are nominal
2. How are the customers distributed across all the states?
SELECT
customer_state as State, COUNT(*) AS Total_Cusotmers,
ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM `Target.customers`), 2) AS
percentage_of_Customer_Spread
FROM
`Target.customers`
GROUP BY
customer_state
ORDER BY
percentage_of_Customer_Spread

Insights: 66% of the customer base is from 3 States out of 27 states, 15 states have <1% customer base,
rest of the 9 states have customer base between 1% to 5%
Recommendations: Need to run campaigns, release offers in rest of the states to improve customer
base

4. Question:
1. Get the % increase in the cost of orders from year 2017 to 2018 (include months between Jan to Aug
only).
WITH
Year_2018 AS
(SELECT
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2018`,
Round(SUM(p.payment_value),2) as Total_purchase_2018
from
`Target.payments` as p
INNER JOIN
`Target.orders` as o
ON
o.order_id = p.order_id
GROUP BY
Year, Month_2018
having
Year = 2018 and Month_2018 between 1 and 8
order by
Month_2018),
Year_2017 AS
(SELECT
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2017`,
Round(SUM(p.payment_value),2) as Total_purchase_2017
from
`Target.payments` as p
INNER JOIN
`Target.orders` as o
ON
o.order_id = p.order_id
GROUP BY
Year, Month_2017
having
Year = 2017 and Month_2017 between 1 and 8
order by
Month_2017)

SELECT
Round(Sum(Total_purchase_2017),2) as Total_purchanse_2017,
Round(Sum(Total_purchase_2018),2) as Total_purchanse_2018,
Round(((Sum(Total_purchase_2018)-Sum(Total_purchase_2017))/Sum(Total_purchase_2017))*100,2)
FROM
Year_2017
INNER JOIN
Year_2018
ON Month_2017 = Month_2018

Insights: 137% (136.98%) increase is there in revenue generated.


Recommendations: NA

2. Calculate the Total & Average value of order price for each state.
SELECT
c.customer_state as `State`, Round(Sum(oi.price),2) as `Price`, Round(Avg(oi.price),2) as `Avg_Price`
from
`Target.order_items` AS oi
INNER JOIN
`Target.orders` AS o
ON
o.order_id = oi.order_id
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
State
order by Avg_Price Desc, State ASC
Insights: Min Average Price is $109 and Max Average Price is $191. Where the customer spread is more
the Average Price is less in the state SP($109)
Recommendations: NA

3. Calculate the Total & Average value of order freight for each state.
SELECT
c.customer_state as `State`, Round(Sum(oi.freight_value),2) as `Price`, Round(Avg(oi.freight_value),2)
as `Avg_freight_value`
from
`Target.order_items` AS oi
INNER JOIN
`Target.orders` AS o
ON
o.order_id = oi.order_id
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
State
order by Avg_freight_value Desc, State ASC

Insights: Min Average Freight Value is $43 and Max Average Price is $16. Where the customer spread is
more the freight value is less in the state SP ($15)
Recommendations: NA

5. Question:
1. Find the no. of days taken to deliver each order from the order’s purchase date as delivery time.
Also, calculate the difference (in days) between the estimated & actual delivery date of an order.
Do this in a single query.

SELECT
order_purchase_timestamp, order_delivered_customer_date,order_estimated_delivery_date,
DATE_DIFF(order_delivered_customer_date, order_purchase_timestamp, DAY) AS Delivery_Time,
DATE_DIFF(order_delivered_customer_date,order_estimated_delivery_date,DAY) AS Estimated_Time
FROM `target-414407.Target.orders`
WHERE
order_delivered_customer_date IS NOT NULL
ORDER BY Estimated_time Desc

Insights: 6535 orders were delayed from the estimated time out of 96476 which is around 6.7%.
2754 orders delivered on estimated time out of 96476 which is around 2.8%
Hence, we have 90.5% of happy customers who got their purchased item before time

2. Find out the top 5 states with the highest & lowest average freight value.

SELECT
c.customer_state as `State`, Round(Sum(oi.price),2) as `Price`, Round(Avg(oi.price),2) as
`Avg_freight_value`
from
`Target.order_items` AS oi
INNER JOIN
`Target.orders` AS o
ON
o.order_id = oi.order_id
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
State
order by Avg_freight_value Desc
Limit 5

Insights: NA

SELECT
c.customer_state as `State`, Round(Sum(oi.price),2) as `Price`, Round(Avg(oi.price),2) as
`Avg_freight_value`
from
`Target.order_items` AS oi
INNER JOIN
`Target.orders` AS o
ON
o.order_id = oi.order_id
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
State
order by Avg_freight_value Asc
Limit 5

Insights: NA
3. Find out the top 5 states with the highest & lowest average delivery time.

SELECT
c.customer_state,
Round(Avg(DATE_DIFF(o.order_delivered_customer_date, o.order_purchase_timestamp, DAY)),2)
AS Delivery_Time,
FROM
`target-414407.Target.orders` AS o
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state
ORDER BY Delivery_Time ASC
Limit 5

SELECT
c.customer_state,
Round(Avg(DATE_DIFF(o.order_delivered_customer_date, o.order_purchase_timestamp, DAY)),2)
AS Delivery_Time,
FROM
`target-414407.Target.orders` AS o
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state
ORDER BY Delivery_Time DESC
Limit 5
Insights: NA

4. Find out the top 5 states where the order delivery is really fast as compared to the estimated date of
delivery.

SELECT
c.customer_state,
Round(Avg(DATE_DIFF(o.order_delivered_customer_date, o.order_purchase_timestamp, DAY)) -
Avg(DATE_DIFF(o.order_delivered_customer_date, o.order_estimated_delivery_date, DAY)),2) AS
Fast_Delivery_Time
FROM
`target-414407.Target.orders` AS o
INNER JOIN
`Target.customers` AS c
ON
o.customer_id = c.customer_id
GROUP BY
c.customer_state
ORDER BY Fast_Delivery_Time DESC Limit 5

Insights: NA

6. Question:
1. Find the month-on-month no. of orders placed using different payment types.

With
Year_2016 AS
(SELECT
p.payment_type as payment_type_16,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2016`,
count(o.order_id) as `orders_2016`
FROM
`Target.payments` as p
INNER JOIN
`Target.orders` as o
ON
o.order_id = p.order_id
GROUP BY
p.payment_type, `Year`,`Month_2016`
HAVING
`Year` = 2016
order by `Month_2016`),

Year_2017 AS
(SELECT
p.payment_type as payment_type_17,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2017`,
count(o.order_id) as `orders_2017`
FROM
`Target.payments` as p
INNER JOIN
`Target.orders` as o
ON
o.order_id = p.order_id
GROUP BY
p.payment_type, `Year`,`Month_2017`
HAVING
`Year` = 2017
order by `Month_2017`),

Year_2018 AS
(SELECT
p.payment_type as payment_type_18,
EXTRACT(year FROM o.order_purchase_timestamp) as `Year`,
EXTRACT(month FROM o.order_purchase_timestamp) as `Month_2018`,
count(o.order_id) as `orders_2018`
FROM
`Target.payments` as p
INNER JOIN
`Target.orders` as o
ON
o.order_id = p.order_id
GROUP BY
p.payment_type, `Year`,`Month_2018`
HAVING
`Year` = 2018
order by `Month_2018`)

SELECT
payment_type_16,Month_2016,orders_2016,payment_type_17,Month_2017,orders_2017,payment_ty
pe_18, Month_2018,orders_2018
FROM
(SELECT
*
FROM
Year_2016 RIGHT JOIN Year_2017
ON payment_type_16 = payment_type_17 and Month_2016 = Month_2017) as `Y_16_17`
LEFT JOIN
Year_2018
ON
payment_type_17 = payment_type_18 and Month_2017 = Month_2018
order by Payment_Type_17 ASC, Month_2017 ASC

Insights: Most of the payments happened through Credit Card in the 2017 & 2018

2. Find the no. of orders placed on the basis of the payment instalments that have been paid.

SELECT
COUNT(o.order_id) as No_of_orders_via_Installment_paymemts
FROM
`Target.orders` as o
INNER JOIN
`Target.payments` as p
ON
o.order_id = p.order_id
WHERE
p.payment_installments =1

Insights: More than 50% of the customers have purchased in instalment method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy