submission_template_coded_project
submission_template_coded_project
Introduction to SQL
Problem Statement
Business Context
A lot of people in the world share a common desire: to own a vehicle. A car or an automobile is seen as an object that
gives the freedom of mobility. Many now prefer pre-owned vehicles because they come at an affordable cost, but at the
same time, they are also concerned about whether the after-sales service provided by the resale vendors is as good as
the care you may get from the actual manufacturers.
New-Wheels, a vehicle resale company, has launched an app with an end-to-end service from listing the vehicle on the
platform to shipping it to the customer's location. This app also captures the overall after-sales feedback given by the
customer.
Objective
New-Wheels sales have been dipping steadily in the past year, and due to the critical customer feedback and ratings
online, there has been a drop in new customers every quarter, which is concerning to the business. The CEO of the
company now wants a quarterly report with all the key metrics sent to him so he can assess the health of the business
and make the necessary decisions.
As a data analyst, you see that there is an array of questions that are being asked at the leadership level that need to be
answered using data. Import the dump file that contains various tables that are present in the database. Use the data to
answer the questions posed and create a quarterly business report for the CEO.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 1
Table of Contents:
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 2
Business Questions
Question 1: Find the total number of customers who have placed orders. What is the distribution of the
customers across states?
Solution Query:
SELECT
COUNT(DISTINCT customer_id) AS total_customers
FROM
order_t;
SELECT
c.state,
COUNT(DISTINCT o.customer_id) AS total_customers
FROM
order_t o
JOIN
customer_t c ON o.customer_id = c.customer_id
GROUP BY
c.state;
Output:
Fig.1
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 3
Fig.2
The total number of customers who have placed orders is identified as 994.
A distribution of customers across states is presented in Fig. 2, highlighting regions with declining customer activity. It
indicates that Texas, California, Florida, and New York show strong demand, with a higher number of orders.
The majority of New-Wheels customers are concentrated in states such as Texas, California, Florida, New York, and
the District of Columbia. These states account for the largest customer bases, suggesting an opportunity for the company
to explore and target other regions as well.
Question 2: Which are the top 5 vehicle makers preferred by the customers?
Solution Query:
SELECT
vehicle_maker,
COUNT(DISTINCT customer_id) AS customer_count
FROM order_t
JOIN product_t ON order_t.product_id = product_t.product_id
GROUP BY vehicle_maker
ORDER BY customer_count DESC
LIMIT 5;
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 4
Output:
Solution Query:
SELECT
c.state,
p.vehicle_maker,
COUNT(o.order_id) AS total_orders
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id
JOIN
customer_t c ON o.customer_id = c.customer_id
GROUP BY
c.state, p.vehicle_maker
ORDER BY
total_orders DESC
LIMIT 5;
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 5
Output:
There are more than 1 preferred vehicle for California state, the most preferred is Chevrolet for Texas, then
Florida and Texas have different favorites.
In some states, there is a preference for more than one vehicle manufacturer, indicating diversity in customer
preferences.
Question 4: Find the overall average rating given by the customers. What is the average rating in each quarter?
Consider the following mapping for ratings: “Very Bad”: 1, “Bad”: 2, “Okay”: 3, “Good”: 4, “Very Good”: 5
Solution Query:
SELECT
quarter_number,
AVG(CASE
WHEN customer_feedback = 'Very Bad' THEN 1
WHEN customer_feedback = 'Bad' THEN 2
WHEN customer_feedback = 'Okay' THEN 3
WHEN customer_feedback = 'Good' THEN 4
WHEN customer_feedback = 'Very Good' THEN 5
END) AS avg_rating_per_quarter
FROM order_t
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 6
GROUP BY quarter_number
ORDER BY quarter_number;
SELECT
AVG(CASE
WHEN customer_feedback = 'Very Bad' THEN 1
WHEN customer_feedback = 'Bad' THEN 2
WHEN customer_feedback = 'Okay' THEN 3
WHEN customer_feedback = 'Good' THEN 4
WHEN customer_feedback = 'Very Good' THEN 5
END) AS overall_avg_rating
FROM order_t;
Output:
● The analysis revealed that the highest average rating, at 3.5548, was observed during the first quarter. However,
there appears to be a consistent decline in the average customer rating from one quarter to the next, culminating
in the fourth quarter, which recorded the lowest average rating of 2.397. Notably, this average falls below the
standard average of 2.4.
● The fourth quarter had the lowest average customer rating, falling below the standard average of 2.5. This
indicates a need for improvement in customer satisfaction and service quality.
Question 5: Find the percentage distribution of feedback from the customers. Are customers getting more
dissatisfied over time?
Solution Query:
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 7
SELECT
customer_feedback,
COUNT(*) * 100.0 / (SELECT COUNT(*) FROM order_t) AS
feedback_percentage
FROM order_t
GROUP BY customer_feedback
ORDER BY feedback_percentage DESC;
SELECT
quarter_number,
customer_feedback,
COUNT(*) * 100.0 / (SELECT COUNT(*) FROM order_t WHERE quarter_number =
o.quarter_number) AS feedback_percentage
FROM order_t o
GROUP BY quarter_number, customer_feedback
ORDER BY quarter_number, feedback_percentage DESC;
Output:
● In the first quarter, the combined percentage of "good" and "very good" feedback was 59%.
● However, during the second quarter, this percentage decreased to 51%, indicating a decline.
● This downward trend continued into the third quarter, where the percentage dropped further to 38%.
● The fourth quarter marked the lowest point, with only 20% of feedback falling into the "good" and "very good"
categories, highlighting a significant decline in positive feedback.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 8
● The decline in positive feedback ("good" and "very good") from the first quarter (59%) to
the fourth quarter (20%) is concerning. It suggests a potential decline in customer
satisfaction or product/service quality.
SELECT
quarter_number,
COUNT(*) AS num_orders
FROM
order_t
GROUP BY
quarter_number
ORDER BY
quarter_number;
Output:
During the first quarter of the year, New Wheels company received the highest number of orders, totaling
310 out of 1000 orders. However, this figure steadily decreased throughout the year, reaching its lowest
point in the fourth quarter, with only 199 orders.
New Wheels experienced a decline in the number of orders as the year progressed, with the fourth
quarter having the lowest number of orders (199) compared to the first quarter (310).
Question 7: Calculate the net revenue generated by the company. What is the quarter-over-quarter % change in
net revenue?
Solution Query:
SELECT
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 9
o.quarter_number,
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity *
o.discount)) AS net_revenue
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id
GROUP BY
o.quarter_number
ORDER BY
o.quarter_number;
SELECT
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity * o.discount)) AS
total_net_revenue
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id;
SELECT
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity * o.discount)) AS
total_net_revenue
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id;
Output:
The net revenue of the company is 48,610,993.75 which has been declining Q-o-Q.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 10
Question 8: What is the trend of net revenue and orders by quarters?
Solution Query:
SELECT
o.quarter_number,
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity * o.discount)) AS
net_revenue,
COUNT(o.order_id) AS total_orders
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id
GROUP BY
o.quarter_number
ORDER BY
o.quarter_number;
Output:
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 11
● The first quarter saw a substantial revenue of $18,032,549.96. However, as the number of
orders declined, there was a significant drop to $8,882,298.80 in the third quarter. This
decline continued further in the fourth quarter, reaching $8,573,149.32.
Question 9: What is the average discount offered for different types of credit cards?
Solution Query:
SELECT
c.credit_card_type,
AVG(o.discount) AS avg_discount
FROM
order_t o
JOIN
customer_t c ON o.customer_id = c.customer_id
GROUP BY
c.credit_card_type
ORDER BY
c.credit_card_type;
Output:
● The average discounts provided by the credit cards are shown above. China-unionpay and americanexpress
has the maximum average discount.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 12
● Americanexpress and China-UnionPay are offering the highest discounts, which could be
a factor in attracting more customers.
Question 10: What is the average time taken to ship the placed orders for each quarter?
Solution Query:
SELECT
quarter_number,
AVG(julianday(ship_date) - julianday(order_date)) AS avg_shipping_time
FROM
order_t
GROUP BY
quarter_number
ORDER BY
quarter_number;
Output:
During the first quarter of the year, the average time to ship an order to a customer was 57 days.
However, as time progressed, the average number of days for order shipment continued to rise. The
fourth quarter marked the highest average shipping time, reaching 174 days to deliver.
The average time to ship orders increased over the year, with the fourth quarter having the highest
average shipping time (174 days). Signaling a need for better supply chain.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 13
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 14
Business Metrics Overview
Last Quarter Revenue Last quarter Orders Average Days to Ship % Good Feedback
SELECT
AVG(CASE
WHEN customer_feedback = 'Very Bad' THEN 1
WHEN customer_feedback = 'Bad' THEN 2
WHEN customer_feedback = 'Okay' THEN 3
WHEN customer_feedback = 'Good' THEN 4
WHEN customer_feedback = 'Very Good' THEN 5
END) AS average_rating
FROM order_t;
SELECT
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity * o.discount)) AS
last_quarter_revenue
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id
WHERE
o.quarter_number = (SELECT MAX(quarter_number) FROM order_t);
SELECT
COUNT(o.order_id) AS last_quarter_orders
FROM
order_t o
WHERE
o.quarter_number = (SELECT MAX(quarter_number) FROM order_t);
SELECT
AVG(JULIANDAY(o.ship_date) - JULIANDAY(o.order_date)) AS average_days_to_ship
FROM
order_t o;
SELECT
(COUNT(CASE WHEN customer_feedback = 'Good' THEN 1 END) * 100.0) / COUNT(*) AS
percent_good_feedback
FROM
order_t;
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 15
SELECT
SUM(p.vehicle_price * o.quantity - (p.vehicle_price * o.quantity *
o.discount)) AS total_revenue
FROM
order_t o
JOIN
product_t p ON o.product_id = p.product_id;
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 16
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 17
Business Recommendations
New-Wheels should implement measures to improve customer satisfaction, especially during the later quarters. This
might involve enhancing product quality, customer service, and the overall shopping experience.
New-Wheels should consider tailoring marketing and promotional strategies to attract and retain customers in states
with lower customer bases.
New-Wheels should evaluate the product range to ensure it aligns with customer preferences and market demand.
New-Wheels should streamline shipping and order fulfillment processes to reduce delivery times and improve
customer experience.
New-Wheels should continuously monitor and analyze customer feedback to identify areas for improvement and
promptly address customer concerns.
New-Wheels should develop strategies to stabilize revenue during quarters of decline, such as exploring new sales
channels or promotions.
New-Wheels should assess the impact of credit card-specific discounts on customer behavior and consider expanding
partnerships with credit card providers to attract more customers.
New-Wheels should explore opportunities to expand into new markets or regions where customer demand may be
higher.
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited. 18