MySQL Assignment – 2
1. What are the top 5 selling (No of Units Sold) Bikes Model in the data set
Query: SELECT
p.product_name AS bike_model,
SUM(oi.quantity) AS total_units_sold
FROM
sales.order_items oi
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name = 'Electric Bikes'
GROUP BY
p.product_name
ORDER BY
total_units_sold DESC
LIMIT 5;
2. Show a trend in electric bike sales (number of units sold) from 2016 to 2018, aggregate
data by month & year.
Query: SELECT
YEAR(o.order_date) AS sales_year,
MONTH(o.order_date) AS sales_month,
SUM(oi.quantity) AS total_units_sold
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name = 'Electric Bikes' AND p.model_year BETWEEN 2016 AND 2018
GROUP BY
YEAR(o.order_date), MONTH(o.order_date)
ORDER BY
sales_year, sales_month;
3. Sort the stores based on average order quantity.
Query: SELECT
s.store_id,
s.store_name,
AVG(oi.quantity) AS avg_order_quantity
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
sales.stores s ON o.store_id = s.store_id
GROUP BY
s.store_id, s.store_name
ORDER BY
avg_order_quantity DESC;
4. Which store sold the most children bikes?
Query: SELECT
s.store_id,
s.store_name,
SUM(oi.quantity) AS total_children_bikes_sold
FROM
sales.order_items oi
JOIN
sales.orders o ON oi.order_id = o.order_id
JOIN
sales.stores s ON o.store_id = s.store_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name = 'Children Bicycles'
GROUP BY
s.store_id, s.store_name
ORDER BY
total_children_bikes_sold DESC
LIMIT 1;
5. Customers from which state bought the most bikes?
Query: SELECT
c.state,
SUM(oi.quantity) AS total_bikes_sold
FROM
sales.order_items oi
JOIN
sales.orders o ON oi.order_id = o.order_id
JOIN
sales.customers c ON o.customer_id = c.customer_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories cat ON p.category_id = cat.category_id
WHERE
cat.category_name in ('Electric Bikes', 'Mountain Bikes', 'Road Bikes')
GROUP BY
c.state
ORDER BY
total_bikes_sold DESC
LIMIT 1;
6. Which is the most stocked bike in stores?
Query: SELECT
p.product_name AS most_stocked_bike,
SUM(s.quantity) AS total_quantity_in_stock
FROM
production.stocks s
JOIN
production.products p ON s.product_id = p.product_id
GROUP BY
p.product_name
ORDER BY
total_quantity_in_stock DESC
LIMIT 1;
7. Which bike model was sold at the highest discount(use average discount over all
sales)?
Query: SELECT
p.product_name AS bike_model,
AVG(oi.discount) AS avg_discount
FROM
sales.order_items oi
JOIN
production.products p ON oi.product_id = p.product_id
GROUP BY
p.product_name
ORDER BY
avg_discount DESC
LIMIT 1;
8. What was the sale value of the single large order?
Query: SELECT
o.order_id,
SUM(oi.quantity * oi.list_price * (1 - oi.discount)) AS sale_value
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
GROUP BY
o.order_id
ORDER BY
sale_value DESC
LIMIT 1;
9. What was the cheapest bike model sold in 2017?
Query: SELECT
p.product_name AS cheapest_bike_model,
MIN(oi.list_price * (1 - oi.discount)) AS min_sale_price
FROM
sales.order_items oi
JOIN
sales.orders o ON oi.order_id = o.order_id
JOIN
production.products p ON oi.product_id = p.product_id
WHERE
YEAR(o.order_date) = 2017
GROUP BY
p.product_name
ORDER BY
min_sale_price ASC
LIMIT 1;
10. Total number of bikes that have been sold.
Query: SELECT
COUNT(DISTINCT oi.product_id) AS total_bikes_sold
FROM
sales.order_items oi
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name in ('Electric Bikes', 'Mountain Bikes', 'Road Bikes');
11. Total sales per year(price)
Query: SELECT
YEAR(o.order_date) AS sales_year,
SUM(oi.quantity * oi.list_price * (1 - oi.discount)) AS total_sales
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
GROUP BY
YEAR(o.order_date)
ORDER BY
sales_year;
12. List the sales (total amount ) driven by each staff member in descending order
Query: SELECT
s.staff_id,
CONCAT(s.first_name, ' ', s.last_name) AS staff_name,
SUM(oi.quantity * oi.list_price * (1 - oi.discount)) AS total_sales
FROM
sales.staffs s
JOIN
sales.orders o ON s.staff_id = o.staff_id
JOIN
sales.order_items oi ON o.order_id = oi.order_id
GROUP BY
s.staff_id, s.first_name, s.last_name
ORDER BY
total_sales DESC;
13. What's the average order fulfilment time ( Shippeddate -order date)?
Query: SELECT
AVG(DATEDIFF(o.shipped_date, o.order_date)) AS avg_fulfillment_time
FROM
sales.orders o
WHERE
o.shipped_date IS NOT NULL;
14. Which store has the shortest average fulfilment time per order?
Query: SELECT
s.store_id,
s.store_name,
AVG(DATEDIFF(o.shipped_date, o.order_date)) AS avg_fulfillment_time
FROM
sales.orders o
JOIN
sales.stores s ON o.store_id = s.store_id
WHERE
o.shipped_date IS NOT NULL
GROUP BY
s.store_id, s.store_name
ORDER BY
avg_fulfillment_time ASC
LIMIT 1;
15. What are the least 2 selling brands (Number of units sold) bikes models in the data set.
Query: SELECT
p.brand_id,
b.brand_name,
p.product_id,
p.product_name,
SUM(oi.quantity) AS total_units_sold
FROM
production.products p
LEFT JOIN
sales.order_items oi ON p.product_id = oi.product_id
LEFT JOIN
production.brands b ON p.brand_id = b.brand_id
GROUP BY
p.brand_id, b.brand_name, p.product_id, p.product_name
ORDER BY
total_units_sold ASC
LIMIT 2;
16. what are the 3 months with most bike sales '.
Query: SELECT
YEAR(o.order_date) AS sales_year,
MONTH(o.order_date) AS sales_month,
SUM(oi.quantity) AS total_bike_sales
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name in ('Mountain Bikes', 'Road Bikes', 'Electirc Bikes')
GROUP BY
YEAR(o.order_date), MONTH(o.order_date)
ORDER BY
total_bike_sales DESC
LIMIT 3;
17. Find the total number of Customer who have bought category "Mountain Bikes"
Query: SELECT
COUNT(DISTINCT o.customer_id) AS total_customers
FROM
sales.orders o
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name = 'Mountain Bikes';
18. Find the customers from NY whose name starts with D.
Query: SELECT * FROM sales.customers
WHERE state = 'NY' AND first_name LIKE 'D%'
19. Find the top 10 customers who bought the maximum quantity of products.
Query: SELECT
c.customer_id,
CONCAT(c.first_name, ' ', c.last_name) AS customer_name,
SUM(oi.quantity) AS total_quantity
FROM
sales.customers c
JOIN
sales.orders o ON c.customer_id = o.customer_id
JOIN
sales.order_items oi ON o.order_id = oi.order_id
GROUP BY
c.customer_id, c.first_name, c.last_name
ORDER BY
total_quantity DESC
LIMIT 10;
20. Find the store having the highest stock for Road bikes
Query: SELECT
s.store_id,
s.store_name,
SUM(ps.quantity) AS total_stock
FROM
sales.stores s
JOIN
production.stocks ps ON s.store_id = ps.store_id
JOIN
production.products p ON ps.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
WHERE
c.category_name = 'Road Bikes'
GROUP BY
s.store_id, s.store_name
ORDER BY
total_stock DESC
LIMIT 1;
21. Find the Staff who sold maximum bikes
Query: SELECT
s.staff_id,
CONCAT(s.first_name, ' ', s.last_name) AS staff_name,
COUNT(oi.quantity) AS total_bikes_sold
FROM
sales.staffs s
JOIN
sales.orders o ON s.staff_id = o.staff_id
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
GROUP BY
s.staff_id, s.first_name, s.last_name
ORDER BY
total_bikes_sold DESC
LIMIT 1;
22. Which City observes maximum sale of bikes
Query: SELECT
c.city,
SUM(oi.quantity) AS total_bikes_sold
FROM
sales.orders o
JOIN
sales.customers c ON o.customer_id = c.customer_id
JOIN
sales.order_items oi ON o.order_id = oi.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories cat ON p.category_id = cat.category_id
WHERE
cat.category_name in ('Electric Bikes', 'Mountain Bikes', 'Road Bikes')
GROUP BY
c.city
ORDER BY
total_bikes_sold DESC
LIMIT 1;
23. Which brand of Bike has maximum and minimum sales under category "Mountain
Bikes"
Query: SELECT
p.brand_id,
b.brand_name,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
JOIN
production.brands b ON p.brand_id = b.brand_id
WHERE
c.category_name = 'Mountain Bikes'
GROUP BY
p.brand_id, b.brand_name
ORDER BY
total_sales DESC
LIMIT 1; -- Maximum sales
SELECT
p.brand_id,
b.brand_name,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.categories c ON p.category_id = c.category_id
JOIN
production.brands b ON p.brand_id = b.brand_id
WHERE
c.category_name = 'Mountain Bikes'
GROUP BY
p.brand_id, b.brand_name
ORDER BY
total_sales ASC
LIMIT 1; -- Minimum sales
24. Find the Month-Year that have maximum sales brand wise.
Query: SELECT
b.brand_id,
b.brand_name,
DATE_FORMAT(o.order_date, '%Y-%m') AS month_year,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
sales.orders o ON oi.order_id = o.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.brands b ON p.brand_id = b.brand_id
GROUP BY
b.brand_id, b.brand_name, DATE_FORMAT(o.order_date, '%Y-%m')
HAVING
SUM(oi.quantity) = (
SELECT
MAX(total_sales)
FROM (
SELECT
b.brand_id,
DATE_FORMAT(o.order_date, '%m-%Y') AS month_year,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
sales.orders o ON oi.order_id = o.order_id
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
production.brands b ON p.brand_id = b.brand_id
GROUP BY
b.brand_id, DATE_FORMAT(o.order_date, '%m-%Y')
) AS brand_month_sales
WHERE
brand_month_sales.brand_id = b.brand_id
);
25. Find the top 10 Store Name, Staff name(Last and first name) who took maximum time
to deliver orders(shipped date - order date)
Query: SELECT
s.store_name,
CONCAT(staffs.first_name, ' ', staffs.last_name) AS staff_name,
MAX(DATEDIFF(orders.shipped_date, orders.order_date)) AS max_delivery_time
FROM
sales.orders
JOIN
sales.stores s ON orders.store_id = s.store_id
JOIN
sales.staffs ON orders.staff_id = staffs.staff_id
WHERE
orders.shipped_date IS NOT NULL
GROUP BY
s.store_name, staffs.first_name, staffs.last_name
ORDER BY
max_delivery_time DESC
LIMIT 10;