Repository navigation
Expand file tree
/
Copy pathsql querries.sql
More file actions
147 lines (118 loc) · 2.95 KB
/
Copy pathsql querries.sql
File metadata and controls
147 lines (118 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- missing and duplicates
SELECT COUNT(*)
FROM orders
WHERE order_no IS NULL;
-- Checking for duplicates
SELECT order_no, COUNT(order_no)
FROM orders
GROUP BY order_no
HAVING COUNT(order_no) > 1;
SELECT *
FROM orders
WHERE
order_no = '5768-2'
OR order_no = '6159-2';
create database Order_DB;
use Order_DB;
-- Checking DataTypes
Describe orders;
Describe account;
describe products;
update orders
set order_date=str_to_date(order_date,'%m/%d/%Y'),
ship_date=str_to_date(ship_date,'%m/%d/%Y');
alter table orders
modify column order_date date,
modify column ship_date date;
-- 1. What is the total revenue generated by each product category?
SELECT
product_category, ROUND(SUM(total), 2) AS Revenue
FROM
products
JOIN
orders ON products.product_id = orders.product_id
GROUP BY product_category;
-- 2.How many unique products have been ordered?
SELECT DISTINCT
COUNT(product_name) AS Unique_Products
FROM
products;
-- 3. What is the total revenue generated each year?
SELECT
EXTRACT(YEAR FROM order_date) AS Year,
FORMAT(SUM(total), 2) AS Revenue
FROM
orders
GROUP BY year;
-- 4. What is the date of the latest and earliest order?
SELECT
MIN(order_date) AS Earliest_Date,
MAX(order_date) AS Latest_Date
FROM
orders;
-- 6. What product category has the lowest average price of products?
SELECT
product_category,
ROUND(AVG(retail_price), 2) AS Average_Price
FROM
orders
JOIN
products USING (product_id)
GROUP BY product_category
ORDER BY Average_Price
LIMIT 1;
-- 7. What are the top 10 highest performing products?
SELECT
product_name, ROUND(SUM(total), 2) AS Revenue
FROM
products
JOIN
orders USING (product_id)
GROUP BY product_name
ORDER BY Revenue DESC
LIMIT 10;
-- 8. Show the total revenue and profit generated by each account_manager?
SELECT
account_manager,
ROUND(SUM(total), 2) AS Revenue,
ROUND(SUM(total) - SUM(cost_price), 2) AS Profit
FROM
account
JOIN
orders USING (account_id)
GROUP BY account_manager
ORDER BY Revenue DESC;
-- What is the name,city & account manager of the 5highest selling product in 2017?
SELECT
product_name,
city,
account_manager,
ROUND(SUM(total), 2) AS Revenue
FROM
orders
JOIN
products USING (product_id)
JOIN
account USING (account_id)
WHERE
EXTRACT(YEAR FROM order_date) = 2017
GROUP BY product_name , city , account_manager
ORDER BY Revenue DESC
LIMIT 5;
-- 10. Find the mean amount spent per order by each Customer type?
SELECT
customer_type, ROUND(SUM(total), 2) AS Spent_Amount
FROM
orders
GROUP BY customer_type
ORDER BY Spent_Amount;
-- 11. What is the 5th highest selling product?
SELECT
product_name, ROUND(SUM(total), 2) AS Revenue
FROM
orders
JOIN
products USING (product_id)
GROUP BY product_name
ORDER BY Revenue DESC
LIMIT 1 OFFSET 4;