0% found this document useful (0 votes)
26 views31 pages

Dba Course Project

DBMS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views31 pages

Dba Course Project

DBMS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

DBA COURSE PROJECT

TEAM DETAILS:
1. SANDEEP - 117
2. MANISH - 127
3. HRISHIKESH - 118
4. SUHAS - 128
ER DIAGRAM
SCHEMA DIAGRAM
**SQL QUERIES**

DROP TABLE orders CASCADE constraints;


DROP TABLE delivery_note CASCADE constraints;
DROP TABLE customer CASCADE constraints;
DROP TABLE administrator CASCADE constraints;
DROP TABLE complaints CASCADE constraints;

CREATE TABLE delivery_note (


D_id INT PRIMARY KEY,
order_id INT,
delivery_date DATE,
status VARCHAR(50),
delivery_address VARCHAR(255),
delivery_person_name VARCHAR(255),
delivery_person_phone VARCHAR(20),
foreign key(order_id) references orders(O_id)
);
CREATE TABLE customer (
C_id INT PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
phone VARCHAR(20),
email_id VARCHAR(30),
gender char(1)
);

CREATE TABLE administrator (


A_id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
password VARCHAR(255),
phone VARCHAR(15)
);

CREATE TABLE complaints (


Co_id INT PRIMARY KEY,
customer_id INT,
ord_id int,
complaint_details VARCHAR(255),
status VARCHAR(50),
date_filed DATE,
date_resolved DATE,
resolution_details VARCHAR(255),
foreign key(ord_id) references orders(O_id),
foreign key(customer_id) references customer(C_id)
);

CREATE TABLE orders (


O_id INT PRIMARY KEY,
cst_id INT,
admin_id int,
date1 DATE,
quantity INT,
price DECIMAL(35),
status VARCHAR(50),
product_id INT,
delivery_address VARCHAR(255),
foreign key(cst_id) references
customer(C_id) , foreign key (admin_id) references
administrator(A_id)
);

INSERT INTO customer VALUES(1, 'John Smith', '123


Main St.', '555-1234', '[email protected]', 'M');
INSERT INTO customer VALUES(2, 'Jane Doe', '456 Oak
St.', '555-5678', '[email protected]', 'F');
INSERT INTO customer VALUES(3, 'Bob Johnson', '789
Elm St.', '555-9012', '[email protected]', 'M');
INSERT INTO customer VALUES(4, 'Alice Williams', '111
Pine St.', '555-3456', '[email protected]',
'F');
INSERT INTO customer VALUES(5, 'Tom Brown', '222
Cedar St.', '555-7890', '[email protected]',
'M');
INSERT INTO customer VALUES(6, 'Sara Davis', '333
Maple St.', '555-2345', '[email protected]', 'F');
INSERT INTO customer VALUES(7, 'Mike Lee', '444
Walnut St.', '555-6789', '[email protected]', 'M');
INSERT INTO customer VALUES(8, 'Jen Chen', '555 Birch
St.', '555-1234', '[email protected]', 'F');
INSERT INTO customer VALUES(9, 'Tim Green', '666 Ash
St.', '555-5678', '[email protected]', 'M');
INSERT INTO customer VALUES(10, 'Lisa Taylor', '777
Pine St.', '555-9012', '[email protected]', 'F');

INSERT INTO administrator VALUES(1, 'Admin 1',


'[email protected]', 'password1', '555-1234');
INSERT INTO administrator VALUES(2, 'Admin 2',
'[email protected]', 'password2', '555-5678');
INSERT INTO administrator VALUES(3, 'Admin 3',
'[email protected]', 'password3', '555-9012');
INSERT INTO administrator VALUES(4, 'Admin 4',
'[email protected]', 'password4', '555-3456');
INSERT INTO administrator VALUES(5, 'Admin 5',
'[email protected]', 'password5', '555-7890');
INSERT INTO administrator VALUES(6, 'Admin 6',
'[email protected]', 'password6', '555-2345');
INSERT INTO administrator VALUES(7, 'Admin 7',
'[email protected]', 'password7', '555-6789');
INSERT INTO administrator VALUES(8, 'Admin 8',
'[email protected]', 'password8', '555-1234');
INSERT INTO administrator VALUES(9, 'Admin 9',
'[email protected]', 'password9', '555-5678');
INSERT INTO administrator VALUES(10, 'Admin 10',
'[email protected]', 'password10', '555-9012');

INSERT INTO orders VALUES(1, 1, 2,TO_DATE('2022-01-


01','YYYY-MM-DD'), 10, 100.00, 'pending', 1, '123 Main
St.');
INSERT INTO orders VALUES(2, 2, 3, TO_DATE('2022-
01-02','YYYY-MM-DD'), 5, 50.00, 'shipped', 2, '456 Oak
St.');
INSERT INTO orders VALUES(3, 3, 1, TO_DATE('2022-
01-03','YYYY-MM-DD'), 8, 80.00, 'delivered', 3, '789 Elm
St.');
INSERT INTO orders VALUES(4, 4, 4, TO_DATE('2022-
01-04','YYYY-MM-DD'), 4, 90.00, 'out for delivery',4,
'111 Pine St.');
INSERT INTO orders VALUES(5, 5, 5, TO_DATE('2022-
01-05','YYYY-MM-DD'), 3, 30.00, 'shipped', 5, '222
Cedar St.');
INSERT INTO orders VALUES(6, 6, 6, TO_DATE('2022-
01-06','YYYY-MM-DD'), 6, 60.00, 'delivered', 6, '333
Maple St.');
INSERT INTO orders VALUES(7, 7, 7, TO_DATE('2022-
01-07','YYYY-MM-DD'), 9, 90.00, 'pending', 7, '444
Walnut St.');
INSERT INTO orders VALUES(8, 8, 8, TO_DATE('2022-
01-08','YYYY-MM-DD'), 15, 150.00, 'shipped', 8, '555
Birch St.');
INSERT INTO orders VALUES(9, 9, 9, TO_DATE('2022-
01-09','YYYY-MM-DD'), 4, 40.00, 'delivered', 9, '666 Ash
St.');
INSERT INTO orders VALUES(10, 10, 10,
TO_DATE('2022-01-10','YYYY-MM-DD'), 7, 70.00,
'pending', 10, '777 Pine St.');
INSERT INTO delivery_note VALUES(1,
1,TO_DATE( '2022-01-11','YYYY-MM--DD'), 'delivered',
'123 Main St.', 'Delivery Person 1', '555-1234');
INSERT INTO delivery_note VALUES(2,
2,TO_DATE( '2022-01-12','YYYY-MM--DD') , 'delivered',
'456 Oak St.', 'Delivery Person 2', '555-5678');
INSERT INTO delivery_note VALUES(3, 3,
TO_DATE( '2022-01-13','YYYY-MM--DD'), 'delivered',
'789 Elm St.', 'Delivery Person 3', '555-9012');
INSERT INTO delivery_note VALUES(4, 4,
TO_DATE( '2022-01-14','YYYY-MM--DD'), 'pending',
'111 Pine St.', 'Delivery Person 4', '555-3456');
INSERT INTO delivery_note VALUES(5, 5,
TO_DATE( '2022-01-15','YYYY-MM--DD'), 'pending',
'222 Cedar St.', 'Delivery Person 5', '555-7890');
INSERT INTO delivery_note VALUES(6,
6,TO_DATE( '2022-01-16','YYYY-MM--DD') , 'pending',
'333 Maple St.', 'Delivery Person 6', '555-2345');
INSERT INTO delivery_note VALUES(7,
7,TO_DATE( '2022-01-17','YYYY-MM--DD') , 'shipped',
'444 Walnut St.', 'Delivery Person 7', '555-6789');
INSERT INTO delivery_note VALUES(8,
8,TO_DATE( '2022-01-18','YYYY-MM--DD') , 'shipped',
'555 Birch St.', 'Delivery Person 8', '555-1234');
INSERT INTO delivery_note VALUES(9,
9,TO_DATE( '2022-01-19','YYYY-MM--DD') , 'shipped',
'666 Ash St.', 'Delivery Person 9', '555-5678');
INSERT INTO delivery_note VALUES(10,
10,TO_DATE( '2022-01-20','YYYY-MM--DD'), 'delivered',
'777 Pine St.', 'Delivery Person 10', '555-9012');

INSERT INTO complaints VALUES(1, 1, 2, 'Product


arrived damaged', 'pending', TO_DATE('2022-01-
03','YYYY-MM-DD'), null, null);
INSERT INTO complaints VALUES(2, 2, 3, 'Wrong
product delivered', 'resolved', TO_DATE('2022-01-
04','YYYY-MM-DD'), TO_DATE('2022-01-06','YYYY-MM-
DD'), 'Product replaced');
INSERT INTO complaints VALUES(3, 3, 4, 'Product never
arrived', 'pending', TO_DATE('2022-01-05','YYYY-MM-
DD'), null, null);
INSERT INTO complaints VALUES(4, 4, 5, 'Product not
as described', 'resolved', TO_DATE('2022-01-06','YYYY-
MM-DD'), TO_DATE('2022-01-09','YYYY-MM-DD'),
'Refund issued');
INSERT INTO complaints VALUES(5, 5, 6, 'Shipping took
too long', 'resolved', TO_DATE('2022-01-07','YYYY-MM-
DD'), TO_DATE('2022-01-10','YYYY-MM-DD'), 'Partial
refund issued');
INSERT INTO complaints VALUES(6, 6, 7, 'Product
arrived damaged', 'resolved', TO_DATE('2022-01-
08','YYYY-MM-DD'), TO_DATE('2022-01-11','YYYY-MM-
DD'), 'Product replaced');
INSERT INTO complaints VALUES(7, 7, 8, 'Product never
arrived', 'resolved', TO_DATE('2022-01-09','YYYY-MM-
DD'), TO_DATE('2022-01-12','YYYY-MM-DD'), 'Product
re-shipped');
INSERT INTO complaints VALUES(8, 8, 9, 'Product not
as described', 'pending', TO_DATE('2022-01-10','YYYY-
MM-DD'), null, null);
INSERT INTO complaints VALUES(9, 9, 10, 'Shipping
took too long', 'resolved', TO_DATE('2022-01-11','YYYY-
MM-DD'), TO_DATE('2022-01-14','YYYY-MM-DD'),
'Partial refund issued');
INSERT INTO complaints VALUES(10, 10, 1, 'Product
arrived damaged', 'pending', TO_DATE('2022-01-
12','YYYY-MM-DD'), null, null);

SELECT * FROM ORDERS;


SELECT * FROM ADMINISTRATOR;
SELECT * FROM CUSTOMER;
SELECT * FROM COMPLAINTS;
SELECT * FROM DELIVERY_NOTE;

*****QUERIES*****

***** BASIC AND SIMPLE QUERIES******

1 . Write a query to find the names of customers


who have placed at least one order for every product
category.

select name
from customer, Orders
where orders.cst_id = customer.C_id
having count(O_id) >= 1
group by name;
2. What is the total price for all orders that are still
pending?

SELECT SUM(price) AS total_price


FROM orders
WHERE status = 'pending';

3. Write a query to retrieve the total revenue


generated from orders placed in the last 90 days.

SELECT SUM(price) AS revenue


FROM orders
WHERE date1 >= (SELECT MAX(date1) - INTERVAL '90'
DAY FROM orders);

4. Write a query to retrieve the number of orders


placed by each customer,
sorted in descending order by the number of orders.
SELECT cst_id, COUNT(*) AS num_orders
FROM orders
GROUP BY cst_id
ORDER BY num_orders DESC;

5. Write a query to retrieve the name, address,


and email of all customers who have placed an order
in the last 30 days.

SELECT c.name, c.address, c.email_id


FROM customer c
JOIN orders o ON c.C_id = o.cst_id
WHERE o.date1 >= DATE '2022-02-08' - 30;

*****AGGREGATE FUNCTIONS*****

1)Find the total number of orders:

SELECT COUNT(*) as total_orders


FROM orders;

2)Find the average price of an order:

SELECT AVG(price) as average_price


FROM orders;

3)Find the total number of orders with status


"delivered":

SELECT COUNT(*) as total_delivered


FROM orders
WHERE status='delivered';

4)Find the highest price of an order:

SELECT MAX(price) as highest_price


FROM orders;
5)Find the total revenue from orders:

SELECT SUM(price * quantity) as total_revenue


FROM orders;

******Complex queries******:

1)Find the name and email of customers who have


filed complaints:

SELECT DISTINCT c.name, c.email_id


FROM customer c
JOIN complaints co ON c.C_id = co.customer_id;

2)Find the name and email of administrators who have


handled complaints:

SELECT DISTINCT a.name, a.email


FROM administrator a
JOIN orders o ON a.A_id = o.admin_id
JOIN complaints co ON o.O_id = co.ord_id;

3)Find the name and address of customers who have


placed orders that are out for delivery:

SELECT DISTINCT c.name, o.delivery_address


FROM customer c
JOIN orders o ON c.C_id = o.cst_id
WHERE o.status = 'out for delivery';

4)Find the number of complaints filed by each


customer:

SELECT c.name, COUNT(*) as num_complaints


FROM customer c
JOIN complaints co ON c.C_id = co.customer_id
GROUP BY c.name;
5)List all customers along with the number of orders
they have placed.

SELECT customer.name, COUNT(*)


FROM orders
JOIN customer ON orders.cst_id = customer.C_id
GROUP BY customer.name;

*****Queries with all 6 clauses******

1)Select all customers who have filed a complaint and


have ordered more than 5 items,
and order the results by the number of items ordered:

SELECT customer.name, COUNT(*) AS


num_items_ordered
FROM customer
JOIN orders ON customer.c_id = orders.cst_id
JOIN complaints ON customer.c_id =
complaints.customer_id
WHERE orders.quantity > 5
GROUP BY customer.name
HAVING COUNT(*) > 0
ORDER BY num_items_ordered DESC;

2)Retrieve the delivery notes for orders that have been


delivered or are out for delivery,
sorted by delivery_date in ascending order:

SELECT delivery_note.*
FROM delivery_note
INNER JOIN orders ON delivery_note.order_id =
orders.O_id
WHERE orders.status = 'delivered' OR orders.status =
'out for delivery'
ORDER BY delivery_date ASC;
3)Find the name and email of customers who have
placed orders that are out for delivery,
and order the results by the delivery address in
ascending order:

SELECT c.name, c.email_id, o.delivery_address


FROM customer c
JOIN orders o ON c.C_id = o.cst_id
WHERE o.status = 'out for delivery'
GROUP BY c.name, c.email_id, o.delivery_address
HAVING count(*) = 1
ORDER BY o.delivery_address ASC;

4)Select all orders that have not been delivered and


group them by their status:

SELECT status, COUNT(*) AS num_orders


FROM orders
WHERE status != 'delivered'
GROUP BY status
HAVING COUNT(*) > 0
ORDER BY num_orders DESC;

*****Correlated queries******

1) find the customers who have made atleast 1 orders:

SELECT name
FROM customer
WHERE C_id IN (SELECT cst_id
FROM orders
WHERE cst_id = customer.C_id
GROUP BY cst_id
HAVING COUNT(*) >= 1);

2)Retrieve the complaints filed by a specific customer:


SELECT * FROM complaints
WHERE customer_id =
(SELECT C_id FROM customer
WHERE name = 'John Smith');

*****Nested queries*****

1)Retrieve the details of customers who have placed an


order:

SELECT * FROM customer


WHERE C_id IN
(SELECT cst_id FROM orders);

2)Find the administrators who have not placed any


orders:

SELECT name
FROM administrator
WHERE A_id NOT IN (SELECT admin_id
FROM orders
WHERE admin_id = administrator.A_id);

3)Retrieve the delivery notes for orders that were


delivered:

SELECT * FROM delivery_note


WHERE order_id IN
(SELECT O_id FROM orders
WHERE status = 'delivered');

4)Find all orders that have complaints filed against


them:

SELECT *
FROM orders o
WHERE EXISTS (
SELECT *
FROM complaints c
WHERE c.ord_id = o.O_id);
Table - Customer

customer_id name address phone email gender


1 John Smith 123 Main St. 555-1234 [email protected] M
2 Jane Doe 456 Oak St. 555-5678 [email protected] F
3 Bob Johnson 789 Elm St. 555-9012 [email protected] M
4 Alice Williams 111 Pine St. 555-3456 [email protected] F
5 Tom Brown 222 Cedar St. 555-7890 [email protected] M
6 Sara Davis 333 Maple St. 555-2345 [email protected] F
7 Mike Lee 444 Walnut St. 555-6789 [email protected] M
8 Jen Chen 555 Birch St. 555-1234 [email protected] F
9 Tim Green 666 Ash St. 555-5678 [email protected] M
10 Lisa Taylor 777 Pine St. 555-9012 [email protected] F

Table: administrator

administrator_id name email password phone


1 Admin 1 [email protected] password1 555-1234
2 Admin 2 [email protected] password2 555-5678
3 Admin 3 [email protected] password3 555-9012
4 Admin 4 [email protected] password4 555-3456
5 Admin 5 [email protected] password5 555-7890
6 Admin 6 [email protected] password6 555-2345
7 Admin 7 [email protected] password7 555-6789
8 Admin 8 [email protected] password8 555-1234
9 Admin 9 [email protected] password9 555-5678
10 Admin 10 [email protected] password10 555-9012

Table : Orders
order custome produc order_d quant unit_pr delivery_no delivery_ad
status
_id r_id t_id ate ity ice te_id dress

2022- pendin
1 1 2 10 100.00 1 123 Main St.
01-01 g

2022- shippe
2 2 3 5 50.00 2 456 Oak St.
01-02 d

2022- deliver
3 3 1 8 80.00 3 789 Elm St.
01-03 ed

out for
2022-
4 4 4 4 90.00 deliver 4 111 Pine St.
01-04
y

2022- shippe 222 Cedar


5 5 5 3 30.00 5
01-05 d St.

2022- deliver 333 Maple


6 6 6 6 60.00 6
01-06 ed St.

2022- pendin 444 Walnut


7 7 7 9 90.00 7
01-07 g St.

2022- shippe
8 8 8 15 150.00 8 555 Birch St.
01-08 d

2022- deliver
9 9 9 4 40.00 9 666 Ash St.
01-09 ed

2022- pendin
10 10 10 7 70.00 10 777 Pine St.
01-10 g

delivery_i order_i delivery_dat contact_numbe


status address delivery_person
d d e r

1 1 2022-01-11 delivered 123 Main St. Delivery Person 1 555-1234

2 2 2022-01-12 delivered 456 Oak St. Delivery Person 2 555-5678


delivery_i order_i delivery_dat contact_numbe
status address delivery_person
d d e r

3 3 2022-01-13 delivered 789 Elm St. Delivery Person 3 555-9012

4 4 2022-01-14 pending 111 Pine St. Delivery Person 4 555-3456

5 5 2022-01-15 pending 222 Cedar St. Delivery Person 5 555-7890

6 6 2022-01-16 pending 333 Maple St. Delivery Person 6 555-2345

444 Walnut
7 7 2022-01-17 shipped Delivery Person 7 555-6789
St.

8 8 2022-01-18 shipped 555 Birch St. Delivery Person 8 555-1234

9 9 2022-01-19 shipped 666 Ash St. Delivery Person 9 555-5678

Delivery Person
10 10 2022-01-20 delivered 777 Pine St. 555-9012
10

TABLE : DELIVERY_NOTE

TABLE : COMPLAINTS
complai order custom complaint_de complaint_ complaint resolutio resolution_co
nt_id _id er_id scription status _date n_date mment
Product arrived 2022-01-
1 1 2 pending null Null
damaged 03
Wrong product 2022-01- 2022-01- Product
2 2 3 resolved
delivered 04 06 replaced
Product never 2022-01-
3 3 4 pending null Null
arrived 05
Product not as 2022-01- 2022-01-
4 4 5 resolved Refund issued
described 06 09
Shipping took 2022-01- 2022-01- Partial refund
5 5 6 resolved
too long 07 10 issued
Product arrived 2022-01- 2022-01- Product
6 6 7 resolved
damaged 08 11 replaced
Product never 2022-01- 2022-01- Product re-
7 7 8 resolved
arrived 09 12 shipped
Product not as 2022-01-
8 8 9 pending null null
described 10
Shipping took 2022-01- 2022-01- Partial refund
9 9 10 resolved
too long 11 14 issued
Product arrived 2022-01-
10 10 10 pending null null
damaged 12

You might also like