-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueries.sql
More file actions
80 lines (75 loc) · 1.62 KB
/
Copy pathqueries.sql
File metadata and controls
80 lines (75 loc) · 1.62 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
-- Part 3: Creating Simple Queries
-- 1. List All Books with Their Authors and Genres
SELECT
b.ISBN,
b.Title,
a.Name AS Author,
g.Title AS Genre
FROM
Books b
JOIN
BookAuthor ba ON b.ISBN = ba.ISBN
JOIN
Authors a ON ba.Author_ID = a.Author_ID
JOIN
BookGenre bg ON b.ISBN = bg.ISBN
JOIN
Genres g ON bg.Genre_ID = g.Genre_ID;
-- 2. Find All Loans for a Specific Borrower (e.g., Borrower_ID = 1)
SELECT
l.Loan_Number,
l.ISBN,
b.Title,
l.Loan_Date,
l.Due_Date,
l.Return_Date,
l.Fine_Amount,
l.Return_Status
FROM
Loans l
JOIN
Books b ON l.ISBN = b.ISBN
WHERE
l.Borrower_ID = 1;
-- 3. List All Administrators and the Books They Manage
SELECT
a.Admin_ID,
u.First_Name || ' ' || u.Last_Name AS Admin_Name,
b.ISBN,
b.Title
FROM
Administrators a
JOIN
Users u ON a.User_ID = u.User_ID
JOIN
Books b ON a.Admin_ID = b.Admin_ID;
-- 4. Retrieve Overdue Loans
SELECT
l.Loan_Number,
u.First_Name || ' ' || u.Last_Name AS Borrower_Name,
b.Title,
l.Due_Date,
l.Return_Status
FROM
Loans l
JOIN
Borrowers br ON l.Borrower_ID = br.Borrower_ID
JOIN
Users u ON br.User_ID = u.User_ID
JOIN
Books b ON l.ISBN = b.ISBN
WHERE
l.Due_Date < SYSDATE AND l.Return_Status = 'N';
-- 5. Calculate Total Fines for Each Borrower
SELECT
br.Borrower_ID,
u.First_Name || ' ' || u.Last_Name AS Borrower_Name,
SUM(l.Fine_Amount) AS Total_Fines
FROM
Loans l
JOIN
Borrowers br ON l.Borrower_ID = br.Borrower_ID
JOIN
Users u ON br.User_ID = u.User_ID
GROUP BY
br.Borrower_ID, u.First_Name, u.Last_Name;