MySQL Assignment 2
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
JOIN
WHERE
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
JOIN
JOIN
WHERE
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
JOIN
GROUP BY
s.store_id, s.store_name
ORDER BY
avg_order_quantity DESC;
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
JOIN
JOIN
WHERE
GROUP BY
s.store_id, s.store_name
ORDER BY
total_children_bikes_sold DESC
LIMIT 1;
Query: SELECT
c.state,
SUM(oi.quantity) AS total_bikes_sold
FROM
sales.order_items oi
JOIN
JOIN
JOIN
production.products p ON oi.product_id = p.product_id
JOIN
WHERE
GROUP BY
c.state
ORDER BY
total_bikes_sold DESC
LIMIT 1;
Query: SELECT
p.product_name AS most_stocked_bike,
SUM(s.quantity) AS total_quantity_in_stock
FROM
production.stocks s
JOIN
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
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,
FROM
sales.orders o
JOIN
GROUP BY
o.order_id
ORDER BY
sale_value DESC
LIMIT 1;
Query: SELECT
p.product_name AS cheapest_bike_model,
FROM
sales.order_items oi
JOIN
JOIN
YEAR(o.order_date) = 2017
GROUP BY
p.product_name
ORDER BY
min_sale_price ASC
LIMIT 1;
Query: SELECT
FROM
sales.order_items oi
JOIN
JOIN
WHERE
Query: SELECT
YEAR(o.order_date) AS sales_year,
FROM
sales.orders o
JOIN
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,
FROM
sales.staffs s
JOIN
JOIN
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
FROM
sales.orders o
WHERE
14. Which store has the shortest average fulfilment time per order?
Query: SELECT
s.store_id,
s.store_name,
sales.orders o
JOIN
WHERE
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
LEFT JOIN
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
JOIN
JOIN
WHERE
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
FROM
sales.orders o
JOIN
JOIN
JOIN
WHERE
19. Find the top 10 customers who bought the maximum quantity of products.
Query: SELECT
c.customer_id,
SUM(oi.quantity) AS total_quantity
FROM
sales.customers c
JOIN
JOIN
GROUP BY
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,
COUNT(oi.quantity) AS total_bikes_sold
FROM
sales.staffs s
JOIN
JOIN
JOIN
JOIN
GROUP BY
ORDER BY
total_bikes_sold DESC
LIMIT 1;
Query: SELECT
c.city,
SUM(oi.quantity) AS total_bikes_sold
FROM
sales.orders o
JOIN
JOIN
JOIN
JOIN
WHERE
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
JOIN
JOIN
WHERE
GROUP BY
p.brand_id, b.brand_name
ORDER BY
total_sales DESC
SELECT
p.brand_id,
b.brand_name,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
JOIN
JOIN
WHERE
GROUP BY
p.brand_id, b.brand_name
ORDER BY
total_sales ASC
24. Find the Month-Year that have maximum sales brand wise.
Query: SELECT
b.brand_id,
b.brand_name,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
JOIN
JOIN
GROUP BY
HAVING
SUM(oi.quantity) = (
SELECT
MAX(total_sales)
FROM (
SELECT
b.brand_id,
SUM(oi.quantity) AS total_sales
FROM
sales.order_items oi
JOIN
JOIN
JOIN
GROUP BY
) 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,
FROM
sales.orders
JOIN
JOIN
WHERE
GROUP BY
ORDER BY
max_delivery_time DESC
LIMIT 10;