From 0f4e53ba4a93fbb4187e3f1cefc49cb934cf1488 Mon Sep 17 00:00:00 2001 From: youronlydimwit <79888425+youronlydimwit@users.noreply.github.com> Date: Wed, 3 Jan 2024 16:12:06 +0700 Subject: [PATCH] Add files via upload --- SQL/Bike Store.sql | 119 ++++++++++++++++++++++++++++++++++++ SQL/main.sql | 146 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+) create mode 100644 SQL/Bike Store.sql create mode 100644 SQL/main.sql diff --git a/SQL/Bike Store.sql b/SQL/Bike Store.sql new file mode 100644 index 0000000..a8019e9 --- /dev/null +++ b/SQL/Bike Store.sql @@ -0,0 +1,119 @@ +create database BikeStore + +-- Adding Foreign Keys +---- Products +ALTER TABLE products +ADD CONSTRAINT Brand_to_Prod +FOREIGN KEY (brand_id) REFERENCES brands(brand_id); + +ALTER TABLE products +ADD CONSTRAINT Cat_to_Prod +FOREIGN KEY (category_id) REFERENCES categories(category_id); + +---- Staffs +ALTER TABLE staffs +ALTER COLUMN store_id TINYINT + +ALTER TABLE staffs +ADD CONSTRAINT Store_to_Staff +FOREIGN KEY (store_id) REFERENCES stores(store_id); + +---- Stocks +ALTER TABLE stocks +ADD CONSTRAINT Store_to_Stock +FOREIGN KEY (store_id) REFERENCES stores(store_id); + +ALTER TABLE stocks +ADD CONSTRAINT Prod_to_Stock +FOREIGN KEY (product_id) REFERENCES products(product_id); + +---- Orders +ALTER TABLE orders +ADD CONSTRAINT PK_Orders +PRIMARY KEY (order_id); + +ALTER TABLE orders +ADD CONSTRAINT Cust_to_Orders +FOREIGN KEY (customer_id) REFERENCES customers(customer_id); + +ALTER TABLE orders +ADD CONSTRAINT Store_to_Orders +FOREIGN KEY (store_id) REFERENCES stores(store_id); + +ALTER TABLE orders +ADD CONSTRAINT Staff_to_Orders +FOREIGN KEY (staff_id) REFERENCES staffs(staff_id); + +--- Order_Items +ALTER TABLE order_items +ADD CONSTRAINT orderItems_to_orders +FOREIGN KEY (order_id) REFERENCES orders(order_id); + +-- BEGINNER LEVELS +--- Retrieve a list of full names and ID's of customers in the database. +SELECT DISTINCT +[Customer ID] = customer_id, +[Full Name] = CONCAT(first_name, ' ',last_name) +FROM +BikeStore.dbo.customers + +--- Find the total number of orders placed by each customer, sort it from largest to smallest. +SELECT +[Customer ID] = C.customer_id, +[Full Name] = CONCAT(C.first_name, ' ',C.last_name), +[Order Count] = COUNT(O.order_id) +FROM +BikeStore.dbo.customers C join BikeStore.dbo.orders O on C.customer_id = O.customer_id +GROUP BY +C.customer_id, C.first_name, C.last_name +ORDER BY +[Order Count] desc + +--- List all the products in a specific category. +SELECT +[Category] = category_name, +[Product Name] = product_name +FROM products P join categories C on P.category_id = C.category_id + +--- Calculate the total sales (without discount) for a specific store. +SELECT +[Store Name] = store_name, +[Total Sales] = CAST(SUM(O.list_price*quantity) as INT) +FROM +stores S join orders D on S.store_id = D.store_id join order_items O on D.order_id = O.order_id +GROUP BY +store_name +ORDER BY +[Store Name] desc + +--- Get the names of staff members working at Baldwin Bikes. +SELECT +[Full Name] = CONCAT(S.first_name, ' ',S.last_name) +FROM +staffs S join stores T on S.store_id = T.store_id +WHERE T.store_name like 'Baldwin Bikes' + +--- Find the brands of products that are out of stock. +SELECT DISTINCT +[Brand] = B.brand_name, +[Stock] = S.quantity +FROM brands B join products P on B.brand_id = P.brand_id join stocks S on S.product_id = P.product_id +WHERE S.quantity = 0 + +--- Identify the store names, couunt of them buying from that store, placed by Onita Johns. +SELECT +[Full Name] = CONCAT(C.first_name, ' ',C.last_name), +[Store Name] = S.store_name, +[Count of Orders] = COUNT(order_id) +FROM customers C join orders O on C.customer_id = O.customer_id join stores S on O.store_id = S.store_id +WHERE C.first_name like 'Onita' AND C.last_name like 'Johns' +GROUP BY +C.first_name, C.last_name, S.store_name + +--- List all products that are only ordered <=5 times. +SELECT +[Product Name] = P.product_name, +[Order Count] = COUNT(O.order_id) +FROM order_items O join products P on P.product_id = O.product_id +GROUP BY P.product_name +HAVING COUNT(O.order_id) < 5 or COUNT(O.order_id) = 5 \ No newline at end of file diff --git a/SQL/main.sql b/SQL/main.sql new file mode 100644 index 0000000..3f0400c --- /dev/null +++ b/SQL/main.sql @@ -0,0 +1,146 @@ +-- Calculate the total revenue generated by each store for year 2018. +SELECT +[Store Name] = S.store_name, +[Gross Revenue] = FORMAT(SUM(OI.list_price*OI.quantity), 'N0'), +[Net Revenue] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)), 'N0') +FROM +order_items OI join orders O on O.order_id = OI.order_id join stores S on O.store_id = S.store_id +WHERE +YEAR(O.order_date) = 2018 +GROUP BY +S.store_name + +-- Find the top 5 selling products across all stores based on the quantity sold. +WITH RankedProducts AS ( + SELECT + [Product Name] = P.product_name, + [qty_Sold] = SUM(OI.quantity), + [RANK] = DENSE_RANK() OVER (ORDER BY SUM(OI.quantity) DESC) + FROM + order_items OI + JOIN products P ON OI.product_id = P.product_id + GROUP BY + P.product_name +) +SELECT DISTINCT + [Product Name], + [qty_Sold], + [RANK] +FROM + RankedProducts +WHERE + [RANK] <= 5; + + +-- Retrieve Jamaal Albert and Melia Brady's order history, including the order date, product details, and total amount spent. +SELECT +[Customer Name] = CONCAT(CU.first_name , ' ' , CU.last_name), +[Order Date] = O.order_date, +[Product details] = P.product_name, +[Category] = C.category_name, +[Amount spent] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)),'N0') +FROM +orders O +join order_items OI on O.order_id = OI.order_id +join products P on OI.product_id = P.product_id +join categories C on P.category_id = C.category_id +join customers CU on CU.customer_id = O.customer_id +WHERE (CU.first_name = 'Jamaal' and CU.last_name = 'Albert') OR (CU.first_name = 'Melia' and CU.last_name = 'Brady') +GROUP BY +CONCAT(CU.first_name, ' ' , CU.last_name), O.order_date, P.product_name, C.category_name +ORDER BY +[Customer Name] asc + + +-- List the products that are running low on stock (quantity less than 10) in each store. +SELECT +[Store Name] = S.store_name, +[Product Name] = P.product_name, +[Stocks] = SUM(ST.quantity) +FROM +stores S join stocks ST on S.store_id = ST.store_id join products P on ST.product_id = P.product_id +GROUP BY store_name, product_name +HAVING SUM(ST.quantity) < 10 +ORDER BY [Stocks] desc + +-- List the products that are running low on stock (quantity less than 10) in each store. +SELECT +[Store Name], +[Product Name], +[Sum Qty] +FROM ( + SELECT + [Store Name] = S.store_name, + [Product Name] = P.product_name, + [Sum Qty] = SUM(ST.quantity) + FROM + stores S join stocks ST on S.store_id = ST.store_id join products P on ST.product_id = P.product_id + GROUP BY + store_name, product_name +) as SUBQQ +WHERE [Sum Qty] < 10 + +-- Identify the top 5 staff members who have processed the highest total value of orders. +SELECT TOP 5 +[Staff Name] = CONCAT(ST.first_name, ' ' , ST.last_name), +[Orders Processed] = COUNT(O.order_id) +FROM staffs ST join orders O on ST.staff_id = O.staff_id +GROUP BY ST.first_name, ST.last_name +ORDER BY [Orders Processed] desc + +-- category wise sales percentage XX +SELECT +[Category Name] = C.category_name, +[Total Sales] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)),'N0'), +[Percentage] = FORMAT((SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)) / + SUM(SUM((OI.list_price * OI.quantity) - ((OI.list_price * OI.quantity) * OI.discount))) OVER ()),'#.##%') +FROM +categories C join products P on C.category_id = P.category_id join order_items OI on OI.product_id = P.product_id +GROUP BY C.category_name +ORDER BY [Percentage] desc + +-- Identify staff members who are marked as inactive (1) and have not processed any orders. +SELECT +[Staff Name] = CONCAT(S.first_name, ' ' , S.last_name) +FROM staffs S join orders O on O.staff_id = S.staff_id +WHERE S.active = 1 +GROUP BY S.first_name, S.last_name +HAVING count(O.order_id) = 0 + +-- Customer Loyalty +WITH Loyalty AS( + SELECT + [Customer Name] = CONCAT(C.first_name, ' ' , C.last_name), + [Stores] = S.store_name, + [Store_Orders] = COUNT(DISTINCT S.store_name), + [Count_Orders] = COUNT(O.order_id), + [Avg_Spending] = AVG((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)) + FROM + customers C + join orders O on C.customer_id = O.customer_id + join order_items OI on O.order_id = OI.order_id + join stores S on S.store_id = O.store_id + GROUP BY + C.first_name, C.last_name, S.store_name, O.order_id +) +SELECT +[Customer Name], +[Stores], +[Count_Orders], +[Avg_Spending] +FROM +Loyalty +WHERE [Store_Orders] > 1 + +-- Calculate the average discount amount applied to products in each product category. +SELECT +[Product Category] = C.category_name, +[Average Discount] = FORMAT(AVG(OI.list_price*OI.discount),'N0') +FROM +categories C +join products P on C.category_id = P.category_id +join order_items OI on OI.product_id = P.product_id +GROUP BY +C.category_name + +