-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask queries.sql
379 lines (290 loc) · 9.73 KB
/
task queries.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
--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.');
SELECT * FROM books;
-- Task 2: Update an Existing Member's Address
UPDATE members
SET member_address = '125 Main St.'
WHERE member_id = 'C101';
SELECT* FROM members;
-- 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;
-- 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
SELECT issued_emp_id,
COUNT(issued_id) as Total_books_issued
FROM issued_status
GROUP BY issued_emp_id
HAVING COUNT(issued_id)>1;
-- CTAS
-- Task 6: Create Summary Tables: Used CTAS to generate new tables based on query results
CREATE TABLE book_cnts
AS
SELECT b.isbn, b.book_title,
COUNT(ist.issued_id) as no_of_issues
FROM books as b
JOIN
issued_status as ist
ON ist.issued_book_isbn = b.isbn
GROUP BY 1,2;
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), COUNT(*)
FROM books as b
JOIN
issued_status as ist
ON ist.issued_book_isbn = b.isbn
GROUP BY 1;
-- Task 9 : List Members Who Registered in the Last 180 Days:
INSERT INTO members(member_id, member_name, member_address, reg_date)
VALUES
('C120', 'sam', '145 Main St', '2024-06-01'),
('C121', 'john', '133 Main St', '2024-05-01');
SELECT * FROM members
WHERE reg_date >= CURRENT_DATE - INTERVAL '180 days';
-- task 10 List Employees with Their Branch Manager's Name and their branch details:
SELECT
e1.*,
b.manager_id,
e2.emp_name as manager
FROM employees as e1
JOIN
branch as b
ON b.branch_id = e1.branch_id
JOIN
employees as e2
ON b.manager_id = e2.emp_id
-- Task 11. Create a Table of Books with Rental Price Above a Certain Threshold 7USD:
CREATE TABLE books_price_greater_than_seven
AS
SELECT * FROM Books
WHERE rental_price > 7
SELECT * FROM
books_price_greater_than_seven
-- Task 12: Retrieve the List of Books Not Yet Returned
SELECT
DISTINCT ist.issued_book_name
FROM issued_status as ist
LEFT JOIN
return_status as rs
ON ist.issued_id = rs.issued_id
WHERE rs.return_id IS NULL;
SELECT * FROM return_status
-- Adding new data to issued_status table
INSERT INTO issued_status(issued_id, issued_member_id, issued_book_name, issued_date, issued_book_isbn, issued_emp_id)
VALUES
('IS151', 'C118', 'The Catcher in the Rye', CURRENT_DATE - INTERVAL '24 days', '978-0-553-29698-2', 'E108'),
('IS152', 'C119', 'The Catcher in the Rye', CURRENT_DATE - INTERVAL '13 days', '978-0-553-29698-2', 'E109'),
('IS153', 'C106', 'Pride and Prejudice', CURRENT_DATE - INTERVAL '7 days', '978-0-14-143951-8', 'E107'),
('IS154', 'C105', 'The Road', CURRENT_DATE - INTERVAL '32 days', '978-0-375-50167-0', 'E101');
-- Adding new column in return_status
ALTER TABLE return_status
ADD Column book_quality VARCHAR(15) DEFAULT('Good');
UPDATE return_status
SET book_quality = 'Damaged'
WHERE issued_id
IN ('IS112', 'IS117', 'IS118');
SELECT * FROM return_status;
/*
Task 13:
Identify Members with Overdue Books
Write a query to identify members who have overdue books (assume a 30-day return period).
Display the member's_id, member's name, book title, issue date, and days overdue.
*/
SELECT
ist.issued_member_id,
m.member_name,
bk.book_title,
ist.issued_date,
-- rs.return_date,
CURRENT_DATE - ist.issued_date as over_dues_days
FROM issued_status as ist
JOIN
members as m
ON m.member_id = ist.issued_member_id
JOIN
books as bk
ON bk.isbn = ist.issued_book_isbn
LEFT JOIN
return_status as rs
ON rs.issued_id = ist.issued_id
WHERE
rs.return_date IS NULL
AND
(CURRENT_DATE - ist.issued_date) > 30
ORDER BY 1;
--
/*
Task 14: Update Book Status on Return
Write a query to update the status of books in the books table to "Yes" when they are returned (based on entries in the return_status table).
*/
SELECT * FROM issued_status
WHERE issued_book_isbn = '978-0-330-25864-8';
-- IS104
SELECT * FROM books
WHERE isbn = '978-0-451-52994-2';
UPDATE books
SET status = 'no'
WHERE isbn = '978-0-451-52994-2';
SELECT * FROM return_status
WHERE issued_id = 'IS130';
--
INSERT INTO return_status(return_id, issued_id, return_date, book_quality)
VALUES
('RS125', 'IS130', CURRENT_DATE, 'Good');
SELECT * FROM return_status
WHERE issued_id = 'IS130';
-- Store Procedures
CREATE OR REPLACE PROCEDURE add_return_records(p_return_id VARCHAR(10), p_issued_id VARCHAR(10), p_book_quality VARCHAR(10))
LANGUAGE plpgsql
AS $$
DECLARE
v_isbn VARCHAR(50);
v_book_name VARCHAR(80);
BEGIN
-- all your logic and code
-- inserting into returns based on users input
INSERT INTO return_status(return_id, issued_id, return_date, book_quality)
VALUES
(p_return_id, p_issued_id, CURRENT_DATE, p_book_quality);
SELECT
issued_book_isbn,
issued_book_name
INTO
v_isbn,
v_book_name
FROM issued_status
WHERE issued_id = p_issued_id;
UPDATE books
SET status = 'yes'
WHERE isbn = v_isbn;
RAISE NOTICE 'Thank you for returning the book: %', v_book_name;
END;
$$
-- Testing FUNCTION add_return_records
SELECT * FROM books
WHERE isbn = '978-0-307-58837-1';
SELECT * FROM issued_status
WHERE issued_book_isbn = '978-0-307-58837-1';
SELECT * FROM return_status
WHERE issued_id = 'IS135';
-- calling function
CALL add_return_records('RS138', 'IS135', 'Good');
-- calling function
CALL add_return_records('RS148', 'IS140', 'Good');
/*
Task 15: Branch Performance Report
Create a query that generates a performance report for each branch, showing the number of books issued, the number of books returned, and the total revenue generated from book rentals.
*/
SELECT * FROM branch;
SELECT * FROM issued_status;
SELECT * FROM employees;
SELECT * FROM books;
SELECT * FROM return_status;
CREATE TABLE branch_reports
AS
SELECT
b.branch_id,
b.manager_id,
COUNT(ist.issued_id) as number_book_issued,
COUNT(rs.return_id) as number_of_book_return,
SUM(bk.rental_price) as total_revenue
FROM issued_status as ist
JOIN
employees as e
ON e.emp_id = ist.issued_emp_id
JOIN
branch as b
ON e.branch_id = b.branch_id
LEFT JOIN
return_status as rs
ON rs.issued_id = ist.issued_id
JOIN
books as bk
ON ist.issued_book_isbn = bk.isbn
GROUP BY 1, 2;
SELECT * FROM branch_reports;
-- Task 16: CTAS: Create a Table of Active Members
-- Use the CREATE TABLE AS (CTAS) statement to create a new table active_members containing members who have issued at least one book in the last 2 months.
CREATE TABLE active_members
AS
SELECT * FROM members
WHERE member_id IN (SELECT
DISTINCT issued_member_id
FROM issued_status
WHERE
issued_date >= CURRENT_DATE - INTERVAL '2 month'
)
;
SELECT * FROM active_members;
-- Task 17: Find Employees with the Most Book Issues Processed
-- Write a query to find the top 3 employees who have processed the most book issues. Display the employee name, number of books processed, and their branch.
SELECT
e.emp_name,
b.*,
COUNT(ist.issued_id) as no_book_issued
FROM issued_status as ist
JOIN
employees as e
ON e.emp_id = ist.issued_emp_id
JOIN
branch as b
ON e.branch_id = b.branch_id
GROUP BY 1, 2;
/*
Task 19: Stored Procedure Objective:
Create a stored procedure to manage the status of books in a library system.
Description: Write a stored procedure that updates the status of a book in the library based on its issuance.
The procedure should function as follows:
The stored procedure should take the book_id as an input parameter.
The procedure should first check if the book is available (status = 'yes').
If the book is available, it should be issued, and the status in the books table should be updated to 'no'.
If the book is not available (status = 'no'), the procedure should return an error message indicating that the book is currently not available.
*/
CREATE OR REPLACE PROCEDURE issue_book(p_issued_id VARCHAR(10), p_issued_member_id VARCHAR(30), p_issued_book_isbn VARCHAR(30), p_issued_emp_id VARCHAR(10))
LANGUAGE plpgsql
AS $$
DECLARE
-- all the variabable
v_status VARCHAR(10);
BEGIN
-- all the code
-- checking if book is available 'yes'
SELECT
status
INTO
v_status
FROM books
WHERE isbn = p_issued_book_isbn;
IF v_status = 'yes' THEN
INSERT INTO issued_status(issued_id, issued_member_id, issued_date, issued_book_isbn, issued_emp_id)
VALUES
(p_issued_id, p_issued_member_id, CURRENT_DATE, p_issued_book_isbn, p_issued_emp_id);
UPDATE books
SET status = 'no'
WHERE isbn = p_issued_book_isbn;
RAISE NOTICE 'Book records added successfully for book isbn : %', p_issued_book_isbn;
ELSE
RAISE NOTICE 'Sorry to inform you the book you have requested is unavailable book_isbn: %', p_issued_book_isbn;
END IF;
END;
$$
SELECT * FROM books;
-- "978-0-553-29698-2" -- yes
-- "978-0-375-41398-8" -- no
SELECT * FROM issued_status;
CALL issue_book('IS155', 'C108', '978-0-553-29698-2', 'E104');
CALL issue_book('IS156', 'C108', '978-0-375-41398-8', 'E104');
SELECT * FROM books
WHERE isbn = '978-0-375-41398-8'