-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmanagement-1.sql
296 lines (89 loc) · 3.12 KB
/
libmanagement-1.sql
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
SELECT * FROM books;
SELECT * FROM branch;
SELECT * FROM employees;
SELECT * FROM issued_status;
SELECT * FROM return_status;
SELECT * FROM members;
--Project Task
--Task 1. Create a New Book Record
-- "978-1-60129-456-2', 'To Kill a Mockingbird', 'Classic', 6.00, 'yes', 'Harper Lee', 'J.B. Lippincott & Co.')"
INSERT INTO books(isbn,book_title,category,rental_price,status,author,publisher)
VALUES ('978-1-60129-456-2', 'To Kill a Mockingbird', 'Classic', 6.00, 'yes', 'Harper Lee', 'J.B. Lippincott & Co.');
--Task 2: Update an Existing Member's Address
UPDATE members
SET member_address = '125 Main St'
WHERE member_id = 'C101'
--Task 3: Delete a Record from the Issued Status Table
-- Objective: Delete the record with issued_id = 'IS121' from the issued_status table.
DELETE FROM issued_status
WHERE issued_id = 'IS121'
SELECT * FROM issued_status
WHERE issued_id = 'IS121'
--Task 4: Retrieve All Books Issued by a Specific Employee
-- Objective: Select all books issued by the employee with emp_id = 'E101'.
SELECT * FROM issued_status
WHERE issued_emp_id='E101';
--Task 5: List Members Who Have Issued More Than One Book
-- Objective: Use GROUP BY to find members who have issued more than one book.
SELECT issued_emp_id, COUNT(issued_id) AS total_books_issued
FROM issued_status
GROUP BY issued_emp_id
HAVING COUNT(issued_id) > 1
ORDER BY COUNT(issued_id)
--Task 6: Create Summary Tables: Used CTAS to generate new tables based on query results
-- each book and total book_issued_cnt**
CREATE TABLE book_cnts
AS
SELECT
b.isbn,
b.book_title,
COUNT(ist.issued_book_isbn) AS no_issued
FROM books as b
JOIN
issued_status as ist
ON ist.issued_book_isbn = b.isbn
GROUP BY b.isbn
SELECT * FROM book_cnts
--Task 7. Retrieve All Books in a Specific Category:
SELECT * FROM books
WHERE category = 'Classic'
--Task 8: Find Total Rental Income by Category:
SELECT
b.category,
SUM(b.rental_price) AS Rental_Income,
COUNT(*) AS Num_of_times_issued
FROM books b
JOIN issued_status ist
ON ist.issued_book_isbn = b.isbn
GROUP BY b.category
--Task 9: List Members Who Registered in the Last 180 Days:
SELECT * FROM members
WHERE members.reg_date >= CURRENT_DATE - 180
INSERT INTO members(member_id, member_name, member_address, reg_date)
VALUES('C118', 'Sam', '145 Main St', '2024-06-01')
DELETE FROM members WHERE member_id = 'C118'
SELECT * FROM branch;
SELECT * FROM branch
--Task 10 List Employees with Their Branch Manager's Name and their branch details:
SELECT e1.*,
b.branch_id,
e2.emp_name AS manager
FROM employees AS e1
JOIN branch b
ON b.branch_id = e1.branch_id
JOIN
employees AS e2
ON e2.emp_id = b.manager_id
--Task 11. Create a Table of Books with Rental Price Above a Certain Threshold:
CREATE TABLE expensive_books
AS
SELECT * FROM books
WHERE rental_price > 7 ;
--Task 12: Retrieve the List of Books Not Yet Returned
SELECT *
FROM
issued_status AS isd
LEFT JOIN
return_status AS ret
ON isd.issued_id = ret.issued_id
WHERE ret.issued_id IS NULL