0% found this document useful (0 votes)
11 views

Practical 10

Uploaded by

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

Practical 10

Uploaded by

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

Practical No.

10
Create schema library;
Create tables:(books,issued_book,fine,student)
Create commands:
Create table books
(bookid int primary key,
title varchar (255),
author varchar (30),
subject varchar (30),
publisher varchar (30),
edition int,
price double);
create table student
(roll_no int primary key,
name varchar (30),
branch varchar (5),
year int);

create table issued_book


(roll_no int,
bookid int,
date_of_issue date,
primary key (roll_no,bookid),
foreign key (roll_no) references student(roll_no),
foreign key(bookid) references books(bookid));
Create table fine
(roll_no int,
bookid int,
date_of_issue date,
late_days int,
fine double,
primary key(roll_no,bookid),
foreign key(bookid) references books(bookid),
foreign key(roll_no) references student(roll_no));

Insert commands:
INSERT INTO books
VALUES
('120', 'Database Management System', 'KORTH', 'Computer', 'PHI', '3', '450.50'),
('121','Compiler Design', 'SILVER', 'Computer', 'PEARSON', '1', '350.00'),
('122', 'Programming in C', 'DENNIS', 'Computer', 'PHI', '2', '345.89'),
('124', 'Automata', 'SILVER', 'Computer', 'SUDARSHAN', '2', '760.0'),
('125', 'Engineering Physics', 'SK CHAND', 'Applied', 'MCGRAW', '2', '560.0'),
('127', 'Engineering Mathematics', 'SK CHAND', 'Applied', 'SUDARSHAN', '3', '600.0'),
('131', 'Management of Human', 'Fenny', 'Management', 'MGY', '2', '1200.50');
INSERT INTO student
values
('1', 'ahaan', 'AS', '1'),
('2', 'viaan', 'AS', '2'),
('3', 'sam', 'MGT', '2'),
('4', 'john', 'EC', '1'),
('5', 'loki', 'EC', '2'),
('6', 'robert', 'CSE', '1'),
('7', 'james', 'CSE', '3');

INSERT INTO issued_book


values
('1', '127', '2023-10-14'),
('3', '124', '2023-09-8'),
('4', '120', '2023-08-17'),
('5', '131', '2023-05-30');

INSERT INTO fine


VALUES
('5', '131', '2023-05-30', '44', '220');

Queries:
1.Select all books
SELECT * FROM books;
2.Select all students
SELECT * FROM student;

3.Select all issued books


SELECT * FROM issued_book;

4.Select all fines


SELECT * FROM fine;

5. Select book details for a specific book (e.g., BookID = 120):

SELECT * FROM books WHERE BookID = 120;

6. Select student details for a specific student (e.g., Roll_No = 1):


SELECT * FROM student WHERE Roll_No = 1;

7. Select books issued by a specific student (e.g., Roll_No = 1):

SELECT * FROM issued_book WHERE Roll_No = 1;

8. Select fines for a specific student (e.g., Roll_No = 5):


SELECT * FROM fine WHERE Roll_No = 5;

9. Select the total number of books:

SELECT COUNT(*) AS TotalBooks FROM books;

10. Select the total number of students:

SELECT COUNT(*) AS TotalStudents FROM student;

11. Select the average price of books:

SELECT AVG(Price) AS AveragePrice FROM books;

12. Select books with a quantity less than 5:


SELECT * FROM books WHERE Quantity < 5;
13. Select students in the Computer Science branch:

SELECT * FROM student WHERE Branch = 'CSE';

14. Select books ordered by edition in descending order:

SELECT * FROM books ORDER BY Edition DESC;

15. Select students who borrowed books on or after '2023-09-01':

SELECT * FROM issued_book WHERE Date_of_Issue >= '2023-09-01';

16. Select books and their authors:

SELECT Title, Author FROM books;

17. Select distinct subjects from books:

SELECT DISTINCT Subject FROM books;


18. Select students who borrowed books along with book details:

SELECT s.*, ib.*, b.Title FROM student s


JOIN issued_book ib ON s.Roll_No = ib.Roll_No
JOIN books b ON ib.BookID = b.BookID;

19. Select books not yet borrowed:


SELECT * FROM books
WHERE BookID NOT IN (SELECT BookID FROM issued_book);

20. Select books and the count of times each has been borrowed:

SELECT b.*, COUNT(ib.BookID) AS BorrowCount


FROM books b
LEFT JOIN issued_book ib ON b.BookID = ib.BookID
GROUP BY b.BookID;

21. Select the student who has the maximum fine:

SELECT s.*, f.*


FROM student s
JOIN fine f ON s.Roll_No = f.Roll_No
ORDER BY f.Fine DESC
LIMIT 1;

22. Update the price of a specific book (e.g., BookID = 121):


UPDATE books SET Price = 400.00 WHERE BookID = 121;

23. Delete a specific student (e.g., Roll_No = 3):

DELETE FROM student WHERE Roll_No = 3;

24. Insert a new book:


INSERT INTO books (Title, Author, Subject, Publisher, Edition, Price)
VALUES ('New Book', 'New Author', 'New Subject', 'New Publisher', 1, 100.00);

25. Select books published by 'PHI':


SELECT * FROM books WHERE Publisher = 'PHI';

26. Select students who borrowed books with details of the book and edition:
SELECT s.*, ib.*, b.Title, b.Edition
FROM student s
JOIN issued_book ib ON s.Roll_No = ib.Roll_No
JOIN books b ON ib.BookID = b.BookID;
27. Find the books with editions greater than 2 borrowed by students:

SELECT b.*, s.*


FROM books b
JOIN issued_book ib ON b.BookID = ib.BookID
JOIN student s ON ib.Roll_No = s.Roll_No
WHERE b.Edition > 2;

28. List all books along with the fine details (if any):
SELECT b.*, f.*
FROM books b
LEFT JOIN fine f ON b.BookID = f.BookID;

29. Find the average fine amount paid by students:


SELECT AVG(f.Fine) AS AverageFine
FROM fine f;

30. List the names of students and the titles of books they have borrowed:
SELECT s.*, b.Title
FROM student s
JOIN issued_book ib ON s.Roll_No = ib.Roll_No
JOIN books b ON ib.BookID = b.BookID;

You might also like