Repository navigation
Expand file tree
/
Copy pathcode2.sql
More file actions
179 lines (136 loc) · 4.58 KB
/
Copy pathcode2.sql
File metadata and controls
179 lines (136 loc) · 4.58 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
create table customers(
customer_id int not null primary key,
customer_name varchar(50) not null,
email varchar(50) not null,
phone varchar(25) not null
);
create table product(
product_id varchar(25) not null primary key,
product_name varchar(50) not null,
description varchar(100) not null,
product_category varchar(50) not null,
unit_price numeric(10,2) not null
);
create table orders(
order_id int not null primary key,
customer_id int not null,
order_date date not null,
product_id varchar(25) not null,
quantity int not null,
delivery_status varchar(20) not null,
foreign key (customer_id) references customers(customer_id),
foreign key (product_id) references product(product_id)
);
create table payment(
payment_id varchar(25) not null primary key,
order_id int not null,
payment_date date not null,
foreign key (order_id) references orders(order_id)
);
create table credit_card(
card_number numeric(15,0) not null primary key,
customer_id int not null,
card_expirydate date not null,
bank_name varchar(40) not null,
foreign key (customer_id) references customers(customer_id)
);
select * from orders;
select * from customers;
select * from product;
select * from payment;
select * from credit_card;
---Removing the records in orders and payment table
truncate orders cascade
truncate payment cascade
alter table product
add column unit_cost numeric(10,5);
alter table product
alter column unit_cost type numeric(10,4);
update product
set unit_cost= unit_price * 0.95;
select * from product where product_category like 'Health and Beauty';
---What is the sum of the product category, minimum unitprice, and also fetch the average of the product category
select product_category, min(unit_price), sum(unit_price), round(avg(unit_price),2)
from product
group by product_category
having sum(unit_price) > 500
select product_name, max(unit_price)as maximum_price
from product
group by product_name
order by maximum_price desc
limit 1
select product_name, min(unit_price) as minimum_price
from product
group by product_name
order by minimum_price desc
limit 1
---Find the highest priced products with their prices
select product_name, unit_price, 'Maximum_price' as price_category
from product
where unit_price = (select max(unit_price) from product)
union all
---Find the lowest priced products with their prices
select product_name, unit_price ,'Minimum_price' as price_category
from product
where unit_price = (select min(unit_price) from product)
--Adding year column to order table
ALTER TABLE orders
ADD years int
--Extracting the year from the order_date and filling up the year column
UPDATE orders
SET years = extract(year from order_date)
--Adding month column to order table
ALTER TABLE orders
add column months varchar(15)
--Extracting the month from the order_date and filling up the month column
UPDATE orders
SET months = to_char(order_date, 'Month')
--Find the total number of orders in each month in year 2022
SELECT months,SUM(quantity) AS total_order
FROM orders
WHERE years = 2022
GROUP BY months
ORDER BY SUM(quantity) DESC
--Find the Average unitprice and unitcost for each product category
Select product_category, round(avg(unit_price),2) as Averageunitprice, round(avg(unit_cost),2)as Averagecostprice
from product
group by product_category
--Find all orders that were placed on or after August 1, 2022
SELECT order_id,product_id, order_date
FROM orders
WHERE order_date >= '2022-08-01'
--Count the number of payments made on March/April 14,2023
SELECT Count(payment_id) as number_of_payment, payment_date
FROM payment
WHERE payment_date = '2023-03-12'
GROUP BY payment_date
--Which customer id has the highest orders placed in the order table
select customer_id, count(*) from orders
group by customer_id
order by count(*) desc
--What is the total number of orders made by each customer id
select customer_id, sum(quantity) from orders
group by customer_id
order by sum(quantity) desc
--How many orders were delivered between January and February 2023
Select count(order_id) as total_orders, months from orders
where order_date >= '2023-01-01'
and order_date < '2023-03-01'
group by months
----Creating a credit_card table
create table credit_card(
card_number numeric(15,0) not null primary key,
customer_id int not null,
card_expirydate date not null,
bank_name varchar(40) not null,
foreign key (customer_id) references customers(customer_id)
);
select * from credit_card
--Total number of cards that has expired
Select count(*) Totalexpiredcard
from credit_card
where card_expirydate < '2023-03-01'
--List of Card Informations that has expired
Select *
from credit_card
where card_expirydate < '2023-03-01'