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

SQL Assignment

The document contains the SQL code to create tables and insert data for a library management system. It creates tables for members, suppliers, book details, fines, and book issues. It then inserts sample data into the tables. It also includes 16 SQL queries to retrieve information from the tables, such as members by city, unreturned books, books by a specific author, category book counts, and supplier contact details.

Uploaded by

Ashi Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views

SQL Assignment

The document contains the SQL code to create tables and insert data for a library management system. It creates tables for members, suppliers, book details, fines, and book issues. It then inserts sample data into the tables. It also includes 16 SQL queries to retrieve information from the tables, such as members by city, unreturned books, books by a specific author, category book counts, and supplier contact details.

Uploaded by

Ashi Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Create table LMS_MEMBERS

(
MEMBER_ID Varchar(10),
MEMBER_NAME Varchar(30) NOT NULL,
CITY Varchar(20),
DATE_REGISTER Date NOT NULL,
DATE_EXPIRE Date ,
MEMBERSHIP_STATUS Varchar(15)NOT NULL,
Constraint LMS_cts1 PRIMARY KEY(MEMBER_ID)
);
Create table LMS_SUPPLIERS_DETAILS
(
SUPPLIER_ID Varchar(3),
SUPPLIER_NAME Varchar(30) NOT NULL,
ADDRESS Varchar(50),
CONTACT bigint(10) NOT NULL,
EMAIL Varchar(15) NOT NULL,
Constraint LMS_cts2 PRIMARY KEY(SUPPLIER_ID)
);
Create table LMS_FINE_DETAILS
(
FINE_RANGE Varchar(3),
FINE_AMOUNT decimal(10,2) NOT NULL,
Constraint LMS_cts3 PRIMARY KEY(FINE_RANGE)
);
Create table LMS_BOOK_DETAILS
(
BOOK_CODE Varchar(10),
BOOK_TITLE Varchar(50) NOT NULL,
CATEGORY Varchar(15) NOT NULL,
AUTHOR Varchar(30) NOT NULL,
PUBLICATION Varchar(30),
PUBLISH_DATE Date,
BOOK_EDITION int(2),
PRICE decimal(8,2) NOT NULL,
RACK_NUM Varchar(3),
DATE_ARRIVAL Date NOT NULL,
SUPPLIER_ID Varchar(3) NOT NULL,
Constraint LMS_cts4 PRIMARY KEY(BOOK_CODE),
Constraint LMS_cts41 FOREIGN KEY(SUPPLIER_ID) References
LMS_SUPPLIERS_DETAILS(SUPPLIER_ID)
);
Create table LMS_BOOK_ISSUE
(
BOOK_ISSUE_NO int,
MEMBER_ID Varchar(10) NOT NULL,
BOOK_CODE Varchar(10) NOT NULL,
DATE_ISSUE Date NOT NULL,
DATE_RETURN Date NOT NULL,
DATE_RETURNED Date NULL,
FINE_RANGE Varchar(3),
Constraint LMS_cts5 PRIMARY KEY(BOOK_ISSUE_NO),
Constraint LMS_Mem FOREIGN KEY(MEMBER_ID) References
LMS_MEMBERS(MEMBER_ID),
Constraint LMS_BookDetail FOREIGN KEY(BOOK_CODE) References
LMS_BOOK_DETAILS(BOOK_CODE),
Constraint LMS_FineDetail FOREIGN KEY(FINE_RANGE) References
LMS_FINE_DETAILS(FINE_RANGE)
);
Insert into LMS_MEMBERS
Values('LM001', 'AMIT', 'CHENNAI', '2012-02-12', '2013-02-11','Temporary');
Insert into LMS_MEMBERS
Values('LM002', 'ABDHUL', 'DELHI', '2012-04-10', '2013-04-09','Temporary');
Insert into LMS_MEMBERS
Values('LM003', 'GAYAN', 'CHENNAI', '2012-05-13','2013-05-12', 'Permanent');
Insert into LMS_MEMBERS
Values('LM004', 'RADHA', 'CHENNAI', '2012-04-22', '2013-04-21', 'Temporary');
Insert into LMS_MEMBERS
Values('LM005', 'GURU', 'BANGALORE', '2012-03-30', '2013-05-16','Temporary');
Insert into LMS_MEMBERS
Values('LM006', 'MOHAN', 'CHENNAI', '2012-04-12', '2013-05-16','Temporary');

Insert into LMS_SUPPLIERS_DETAILS


Values ('S01','SINGAPORE SHOPPEE', 'CHENNAI', 9894123555,'[email protected]');
Insert into LMS_SUPPLIERS_DETAILS
Values ('S02','JK Stores', 'MUMBAI', 9940123450 ,'[email protected]');
Insert into LMS_SUPPLIERS_DETAILS
Values ('S03','ROSE BOOK STORE', 'TRIVANDRUM', 9444411222,'[email protected]');
Insert into LMS_SUPPLIERS_DETAILS
Values ('S04','KAVARI STORE', 'DELHI', 8630001452,'[email protected]');
Insert into LMS_SUPPLIERS_DETAILS
Values ('S05','EINSTEN BOOK GALLARY', 'US', 9542000001,'[email protected]');
Insert into LMS_SUPPLIERS_DETAILS
Values ('S06','AKBAR STORE', 'MUMBAI',7855623100 ,'[email protected]');

Insert into LMS_FINE_DETAILS Values('R0', 0);


Insert into LMS_FINE_DETAILS Values('R1', 20);
insert into LMS_FINE_DETAILS Values('R2', 50);
Insert into LMS_FINE_DETAILS Values('R3', 75);
Insert into LMS_FINE_DETAILS Values('R4', 100);
Insert into LMS_FINE_DETAILS Values('R5', 150);
Insert into LMS_FINE_DETAILS Values('R6', 200);

Insert into LMS_BOOK_DETAILS


Values('BL000001', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice
Hall', '1999-12-10', 6, 600.00, 'A1', '2011-05-10', 'S01');
Insert into LMS_BOOK_DETAILS
Values('BL000002', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt',
'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2011-05-10', 'S03');
Insert into LMS_BOOK_DETAILS
Values('BL000003', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice
Hall', '1999-05-10', 6, 600.00, 'A1', '2012-05-10', 'S01');
Insert into LMS_BOOK_DETAILS
Values('BL000004', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt',
'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2012-05-11', 'S01');
Insert into LMS_BOOK_DETAILS
Values('BL000005', 'Java How To Do Program', 'JAVA', 'Paul J. Deitel', 'Prentice
Hall', '1999-12-10', 6, 600.00, 'A1', '2012-05-11', 'S01');
Insert into LMS_BOOK_DETAILS
Values('BL000006', 'Java: The Complete Reference ', 'JAVA', 'Herbert Schildt',
'Tata Mcgraw Hill ', '2011-10-10', 5, 750.00, 'A1', '2012-05-12', 'S03');
Insert into LMS_BOOK_DETAILS
Values('BL000007', 'Let Us C', 'C', 'Yashavant Kanetkar ', 'BPB Publications',
'2010-12-11', 9, 500.00 , 'A3', '2010-11-03', 'S03');
Insert into LMS_BOOK_DETAILS
Values('BL000008', 'Let Us C', 'C', 'Yashavant Kanetkar ','BPB Publications',
'2010-05-12', 9, 500.00 , 'A3', '2011-08-09', 'S04');

Insert into LMS_BOOK_ISSUE


Values (001, 'LM001', 'BL000001', '2012-05-01', '2012-05-16', '2012-05-16', 'R0');
Insert into LMS_BOOK_ISSUE
Values (002, 'LM002', 'BL000002', '2012-05-01', '2012-05-06','2012-05-16', 'R2');
Insert into LMS_BOOK_ISSUE
Values (003, 'LM003', 'BL000007', '2012-04-01', '2012-04-16', '2012-04-20','R1');
Insert into LMS_BOOK_ISSUE
Values (004, 'LM004', 'BL000005', '2012-04-01', '2012-04-16','2012-04-20', 'R1');
Insert into LMS_BOOK_ISSUE
Values (005, 'LM005', 'BL000008', '2012-03-30', '2012-04-15','2012-04-20' , 'R1');
Insert into LMS_BOOK_ISSUE
Values (006, 'LM005', 'BL000008', '2012-04-20', '2012-05-05','2012-05-05' , 'R0');
Insert into LMS_BOOK_ISSUE
Values (007, 'LM003', 'BL000007', '2012-04-22', '2012-05-07','2012-05-25' , 'R4');

/*-------------------Simple qry ------------------------*/

/* Problem # 1:
Write a query to display the member id, member name, city and membership status who
are all having life time membership.
Hint: Life time membership status is “Permanent”.*/

SQL>select member_id,member_name,city,membership_status from LMS_MEMBERS where


membership_status='Permanent';

/*Problem # 2:
Write a query to display the member id, member name who have not returned the
books.
Hint: Book return status is book_issue_status ='Y' or 'N'.*/

SQL> select b.member_id, m.member_name from LMS_BOOK_ISSUE b


inner join lms_members m on m.member_id = b.member_id and book_issue_status = 'Y';

/*Problem # 3:
Write a query to display the member id, member name who have taken the book with
book code 'BL000002'.*/

SQL>select b1. member_id,b1.member_name,b2.book_code from LMS_MEMBERS


b1,LMS_BOOK_ISSUE b2 where b2.BOOK_CODE='BL000002';

/*Problem # 4:
Write a query to display the book code, book title and author of the books whose
author name begins with 'P'.*/

SQL>select book_code,book_title,author from LMS_BOOK_DETAILS where AUTHOR like 'P


%';

/*Problem # 5:
Write a query to display the total number of Java books available in library with
alias name ‘NO_OF_BOOKS’.*/

SQL>select COUNT(category)as NO_OF_BOOKS from LMS_BOOK_DETAILS where


CATEGORY='JAVA';

/*Problem # 6:
Write a query to list the category and number of books in each category with alias
name ‘NO_OF_BOOKS’.*/

SQL>select category,COUNT(category)as NO_OF_BOOKS from LMS_BOOK_DETAILS group by


CATEGORY;

/*Problem # 7:
Write a query to display the number of books published by "Prentice Hall” with the
alias name “NO_OF_BOOKS”.*/

SQL>select COUNT(publication) as NO_OF_BOOKS from LMS_BOOK_DETAILS where


PUBLICATION='Prentice Hall';

/* Problem # 8:
Write a query to display the book code, book title of the books which are issued on
the date "1st April 2012".*/

SQL>select b1.book_code,b1.DATE_ISSUE,b2.book_title from LMS_BOOK_ISSUE b1 inner


join LMS_BOOK_DETAILS b2 on b1.BOOK_CODE=b2.BOOK_CODE where b1.DATE_ISSUE='2012-04-
01';

/*Problem # 9:
Write a query to display the member id, member name, date of registration and
expiry date of the members
whose membership expiry date is before APR 2013.*/

SQL>select member_id,member_name,DATE_REGISTER,DATE_EXPIRE from LMS_MEMBERS where


DATE_EXPIRE < '2013-04-01';

/* Problem # 10:
write a query to display the member id, member name, date of registration,
membership status of
the members who registered before "March 2012" and membership status is
"Temporary"*/

SQL> select member_id,member_name,DATE_REGISTER,MEMBERSHIP_STATUS from LMS_MEMBERS


where DATE_REGISTER < '2012-03-01' and MEMBERSHIP_STATUS='Temporary';

/*Problem #11:
Write a query to display the member id, member name who’s City is CHENNAI or DELHI.
Hint:
Display the member name in title case with alias name 'Name'.*/

SQL> select MEMBER_ID,MEMBER_NAME "Name" FROM LMS_MEMBERS WHERE CITY='CHENNAI' OR


CITY='DELHI';

/*Problem #12:
Write a query to concatenate book title, author and display in the following
format.
Book_Title_is_written_by_Author
Example: Let Us C_is_written_by_Yashavant Kanetkar
Hint: display unique books. Use “BOOK_WRITTEN_BY” as alias name.*/

SQL>select unique ( book_title)||'is_written_by'||author from LMS_BOOK_DETAILS;

/*Problem #13:
Write a query to display the average price of books which is belonging to ‘JAVA’
category with alias
name “AVERAGEPRICE”.*/

SQL> select avg(price) "AVERAGEPRICE" from lms_book_details group by category


having category = 'JAVA';

/*Problem #14:
Write a query to display the supplier id, supplier name and email of the suppliers
who are all having gmail account.*/

SQL>select supplier_id,SUPPLIER_NAME,EMAIL from LMS_SUPPLIERS_DETAILS where EMAIL


like '%gmail.com';

/*Problem#15:
Write a query to display the supplier id, supplier name and contact details.
Contact details can be either phone number or email or address with alias name
“CONTACTDETAILS”.
If phone number is null then display email, even if email also null then display
the address of the supplier.
Hint: Use Coalesce function.*/

SQL>select Supplier_id,supplier_name,coalesce(contact,email,address) as
contactdetails from LMS_SUPPLIERS_DETAILS;

/*Problem#16:
Write a query to display the supplier id, supplier name and contact.
If phone number is null then display ‘No’ else display ‘Yes’ with alias name
“PHONENUMAVAILABLE”. Hint: Use NVL2.*/

SQL>select SUPPLIER_ID,SUPPLIER_NAME,case when CONTACT is NULL then 'NO' when


CONTACT is not null then 'YES' end as PHONE_NUM_AVAILABLE from
LMS_SUPPLIERS_DETAILS;

/*----------------------Average query-------------------------------------*/
/*Problem # 1:
Write a query to display the member id, member name of the members, book code and
book title of the books taken
by them.*/

SQL> select MEMBER_ID,(select MEMBER_NAME from LMS_MEMBERS where


MEMBER_ID=t1.MEMBER_ID )as MEMBER_NAME,
BOOK_CODE,(select BOOK_TITLE from LMS_BOOK_DETAILS
where BOOK_CODE=t1.BOOK_CODE)as BOOK_TITLE from LMS_BOOK_ISSUE t1;
/*Problem # 2:
Write a query to display the total number of books available in the library with
alias name “NO_OF_BOOKS_AVAILABLE”
(Which is not issued). Hint: The issued books details are available in the
LMS_BOOK_ISSUE table.*/

SQL> SELECT COUNT(t1.BOOK_CODE)AS NO_OF_BOOKS_AVAILABLE FROM LMS_BOOK_DETAILS t1


LEFT JOIN LMS_BOOK_ISSUE t2 ON t1.BOOK_CODE = t2.BOOK_CODE WHERE t2.BOOK_CODE IS
NULL;

/*Problem # 3:
Write a query to display the member id, member name, fine range and fine amount of
the members
whose fine amount is less than 100.*/

SQL> SELECT T1.MEMBER_ID,(SELECT MEMBER_NAME FROM LMS_MEMBERS WHERE


MEMBER_ID=T1.MEMBER_ID)AS
MEMBER_NAME,(SELECT T2.FINE_RANGE FROM LMS_MEMBERS
WHERE MEMBER_ID=T1.MEMBER_ID)AS FINE_RANGE,(SELECT FINE_AMOUNT FROM
LMS_FINE_DETAILS T2 WHERE FINE_RANGE= T1.FINE_RANGE)
AS FINE_AMOUNT FROM LMS_BOOK_ISSUE T1 LEFT JOIN LMS_FINE_DETAILS T2
ON T1.FINE_RANGE = T2.FINE_RANGE WHERE T2.FINE_AMOUNT < 100;

/* Problem # 4:
Write a query to display the book code, book title and availability status of the
‘JAVA’ books whose edition is "6”.
Show the availability status with alias name “AVAILABILITYSTATUS”. Hint: Book
availability status can be fetched
from “BOOK_ISSUE_STATUS” column of LMS_BOOK_ISSUE table.*/

SQL> select book_code,book_title,publication,price,book_edition,publish_date


from lms_book_details
order by book_edition,publication,publish_date ;

/*Problem # 5:
Write a query to display the book code, book title and rack number of the books
which are placed in rack 'A1'
and sort by book title in ascending order.*/

SQL> SELECT BOOK_CODE,BOOK_TITLE,RACK_NUM FROM LMS_BOOK_DETAILS where RACK_NUM='A1'


ORDER BY BOOK_TITLE ASC;

/*Problem # 6:
Write a query to display the member id, member name, due date and date returned of
the members who has returned
the books after the due date. Hint: Date_return is due date and Date_returned is
actual book return date.*/

SQL> SELECT MEMBER_ID,(SELECT MEMBER_NAME FROM LMS_MEMBERS WHERE


MEMBER_ID=T1.MEMBER_ID)AS
MEMBER_NAME,DATE_RETURN AS DUE_DATE,DATE_RETURNED FROM LMS_BOOK_ISSUE T1
WHERE DATE_RETURN < DATE_RETURNED;

/*Problem # 7:
Write a query to display the member id, member name and date of registration who
have not taken any book.*/

SQL> SELECT T1.MEMBER_ID,MEMBER_NAME,DATE_REGISTER


FROM LMS_MEMBERS T1 LEFT JOIN LMS_BOOK_ISSUE T2 ON
T1.MEMBER_ID = T2.MEMBER_ID WHERE T2.MEMBER_ID IS NULL;

/*Problem # 8:
Write a Query to display the member id and member name of the members who has not
paid any fine
in the year 2012.*/

SQL> select t1.MEMBER_ID,(select t2.MEMBER_NAME from LMS_MEMBERS t2 where


t1.MEMBER_ID=t2.MEMBER_ID)as
MEMBER_NAME from LMS_BOOK_ISSUE t1
where t1.DATE_RETURN >=t1.DATE_RETURNED and year(t1.DATE_RETURNED)=2012;

/*Problem # 9:
Write a query to display the date on which the maximum numbers of books were issued
and
the number of books issued with alias name “NOOFBOOKS”.*/

SQL> select DATE_ISSUE,count(DATE_ISSUE) as NO_OF_BOOKS from LMS_BOOK_ISSUE


group by DATE_ISSUE having COUNT(DATE_ISSUE)=(select MAX(counted) from (select
COUNT(DATE_ISSUE)
as counted from LMS_BOOK_ISSUE group by DATE_ISSUE) as t);

/*Problem # 10:
Write a query to list the book title and supplier id for the books authored by
“Herbert Schildt"
and the book edition is 5 and supplied by supplier ‘S01’.*/

SQL>select BOOK_TITLE,SUPPLIER_ID from LMS_BOOK_DETAILS where AUTHOR='Herbert


Schildt' and BOOK_EDITION='5' and SUPPLIER_ID='S01';

/*Problem # 11:
Write a query to display the rack number and the number of books in each rack with
alias
name “NOOFBOOKS” and sort by rack number in ascending order.*/

SQL> select RACK_NUM,COUNT(book_code) as NO_OF_BOOKS from LMS_BOOK_DETAILS group by


RACK_NUM order by RACK_NUM asc;

/*Problem # 12:
Write a query to display book issue number, member name, date or registration, date
of expiry,
book title, category author, price, date of issue, date of return, actual returned
date, issue status,
fine amount.*/

SQL> select bi.book_issue_no, m.member_name,m.date_register,m.date_expire,


bd.book_title, bd.category, bd.author, bd.price, bi.date_issue,
bi.date_return, bi.date_returned,bi.book_issue_status, f.fine_amount from
lms_book_issue bi inner join
lms_book_details bd on bi.book_code = bd.book_code inner join lms_members m on
bi.member_id = m.member_id inner join lms_fine_details f on bi.fine_range =
f.fine_range;

/*Problem # 13:
Write a query to display the book code, title, publish date of the books which is
been published in the month of December.*/

SQL> select book_code, book_title, publish_date from LMS_BOOK_DETAILS where


extract(month from publish_date) = '12';

/*Problem # 14:
Write a query to display the book code, book title and availability status of the
‘JAVA’ books
whose edition is "5”. Show the availability status with alias name
“AVAILABILITYSTATUS”.
Hint: Book availability status can be fetched from “BOOK_ISSUE_STATUS” column of
LMS_BOOK_ISSUE table.*/

SQL> select bd.book_code, bd.book_title, bi.book_issue_status "AVAILABILITYSTATUS"


from LMS_BOOK_DETAILS
bd inner join LMS_BOOK_ISSUE bi on bd.BOOK_CODE = bi.BOOK_CODE and bd.category =
'JAVA' and bd.book_edition = 5;

/*--------------COMPLEX QUERY---------------------------*/

/*Problem # 1:
Write a query to display the book code, book title and supplier name of the
supplier who
has supplied maximum number of books. For example, if “ABC Store” supplied 3 books,
“LM Store” has supplied 2 books and “XYZ Store” has supplied 1 book. So “ABC Store”
has supplied
maximum number of books, hence display the details as mentioned below.
Example:BOOK_CODE BOOK_TITLE SUPPLIER_NAME
BL000008 Easy Reference for Java ABC STORE
BL000001 Easy Reference for C ABC STORE
BL000003 Easy Reference for VB ABC STORE*/

SQL> select bd.book_code, bd.book_title, s.supplier_name from lms_book_details bd


inner join
lms_suppliers_details s on bd.supplier_id = s.supplier_id and bd.supplier_id =
(select supplier_id from lms_book_details
group by supplier_id having count(supplier_id) = (select max(count(supplier_id))
from lms_book_details group by supplier_id));

/*Problem # 2:
Write a query to display the member id, member name and number of remaining books
he/she
can take with “REMAININGBOOKS” as alias name. Hint: Assuming a member can take
maximum 3 books.
For example, Ramesh has already taken 2 books; he can take only one book now.
Hence display the remaining books as 1 in below format.
Example:MEMBER_ID MEMBER_NAME REMAININGBOOKS
LM001 RAMESH 1
LM002 MOHAN 3*/

SQL> select bi.member_id, m.member_name, 3 - count(bi.member_id) "REMAININGBOOKS"


from
lms_book_issue bi inner join lms_members m on bi.member_id = m.member_id group by
bi.member_id, m.member_name;

/*Problem # 3
Write a query to display the supplier id and supplier name of the supplier who has
supplied minimum number of books. For example, if “ABC Store” supplied 3 books, “LM
Store” has supplied
2 books and “XYZ Store” has supplied 1 book. So “XYZ Store” has supplied minimum
number of books,
hence display the details as mentioned below.
Example:
SUPPLIER_ID SUPPLIER_NAME
S04 XYZ STORE*/

SQL> select bd.supplier_id, s.supplier_name from lms_book_details bd inner join


lms_suppliers_details s
on bd.supplier_id = s.supplier_id and bd.supplier_id = (select supplier_id from
lms_book_details group by supplier_id having
count(supplier_id) = (select min(count(supplier_id)) from lms_book_details group
by supplier_id));

You might also like