Lab 1
Lab 1
BRANCH(Branchid,Branchname,HOD)
STUDENT(USN,Name,Address,Branchid,Sem)
BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)
AUTHOR(Authorid,Authorname,Country,Age)
BORROW(USN,Bookid,Borrowdate)
-----------------------------------------------------------------------------------
------------------------------------------------------------------
TABLE:BRANCH
Create table BRANCH
(Branchid varchar(10)Primary key,
Branchname varchar(10),
HOD varchar(10));
Insert into
STUDENT(USN,Name,Address,Branchid,Sem)values("4JN17MCA11","Jishi","#9,kallalli",2);
------------------------------------------------------------------------------
USN Name Address Branchid Sem
------------------------------------------------------------------------------
4JN17MCA11 Jishi #9,kallalli B01 2
4JN17MCA37 Suraksha #20,Vinobanagar B01 2
4JN17MCA08 Ananya #11,Hudco B01 1
4JN17MBA10 Namitha #5,Gopala B02 2
4JN17MBA01 Samanyu #1,Bhadravathi B02 1
-----------------------------------------------------------------------------------
------------------------------------------------------------------
TABLE:BOOK
Create table BOOK
(Bookid varchar(10)Primary key,
Bookname varchar(10),
Authorid varchar(10)references Author,
Publisher varchar(10),
Branchid varchar(10)references Branch);
Insert into
BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)values("BK01","DBMS","A01","KJF","
B01");
-------------------------------------------------------------------
Bookid Bookname Authorid Publisher Branchid
-------------------------------------------------------------------
BK01 DBMS A01 KJF B01
BK02 OOP A05 UVW B01
BK03 DMS A03 KJF B01
BK04 UNIX A04 OPQ B01
BK05 C++ A02 KJF B01
-----------------------------------------------------------------------------------
------------------------------------------------------------------
TABLE:AUTHOR
Create table AUTHOR
(Authorid varchar(10)Primary key,
Authorname varchar(10),
Country varchar(10),
Age number(2));
--------------------------------------------------------------------
Authorid Authorname Country Age
--------------------------------------------------------------------
A01 Elmasri Navathe India 44
A02 Balguru Swami India 50
A03 Girimaldi India 40
A04 Sumitabadas India 36
A05 K R Venugopal India 54
A06 Dennis Richie India 60
-----------------------------------------------------------------------------------
------------------------------------------------------------------
TABLE:BORROW
Create table BORROW
(USN varchar(10)references STUDENT,
Bookid varchar(10)references BOOK,
Borrowdate date);
---------------------------------------
USN Bookid Borrowdate
---------------------------------------
4JN17MCA11 BK01 2018-04-03
4JN17MCA11 BK03 2018-03-04
4JN17MCA37 BK02 2018-01-11
4JN17MCA08 BK04 2018-02-05
4JN17MCA37 BK01 2018-06-14
4JN17MCA11 BK05 2018-05-11
-----------------------------------------------------------------------------------
------------------------------------------------------------------
a)List the details of students who are all studying in 2nd sem MCA
select USN,Name,Address
from STUDENT S,BRANCH B
where S.Branchid=B.Branchid and Sem=2 and Branchname='MCA';
------------------------------------------
USN Name Address
------------------------------------------
4JN17MCA11 Jishi #9,kallalli
4JN17MCA37 Suraksha #11,vinobanagar
-----------------------------------------------------------------------------------
------------------------------------------------------------------
b)List the students who are not borrowed any books
select Name
from STUDENT S
where NOT EXISTS
(select * from BORROW B where B.USN=S.USN);
----------
Name
----------
Samanyu
Ananya
-----------------------------------------------------------------------------------
------------------------------------------------------------------
c)Display the USN,student name,branch name,book name,author name,books borrowed
date of 2nd sem MCA students.
select S.USN,Name,Branchname,Bookname,Authorname,Borrowdate
from BRANCH B,STUDENT S,BOOK BK,AUTHOR A,BORROW BR
where S.Branchid=B.Branchid and BR.USN=S.USN and BK.Bookid=BR.Bookid and
BK.Authorid=A.Authorid and Sem=2 and Branchname='MCA';
-----------------------------------------------------------------------------------
---------------------
USN Name Branchname Bookname Authorname Borrowdate
-----------------------------------------------------------------------------------
---------------------
4JN17MCA11 Jishi MCA DBMS Elmasri Navathe 03-MAR-18
4JN17MCA11 Jishi MCA DMS Giri maldi 06-APR-18
4JN17MCA37 Suraksha MCA OOP K R Venugopal 01-SEP-18
4JN17MCA37 Suraksha MCA DBMS Elmasri Navathe 06-APR-18
4JN17MCA11 Jishi MCA C Dennis Richie 15-MAY-18
-----------------------------------------------------------------------------------
------------------------------------------------------------------
d)Display the number of books written by each author.
select Authorname,Count(*)
from AUTHOR A,BOOK BK
where A.Authorid=BK.Bookid
GROUP BY Authorname;
-----------------------------------
Authorname Count(*)
-----------------------------------
BalguruSwami 1
Dennis Richie 1
Elmasri Navathe 2
Girimaldi 1
K R Venugopal 1
Sumitabadas 1
-----------------------------------------------------------------------------------
------------------------------------------------------------------
e)Diplay the students details who borrowed more than two books.
select *
from STUDENT S
where(selectCount(*)
from BORROW BR
where BR.USN=S.USN)>2;
-----------------------------------------------------------------
USN Name Address Branchid Sem
-----------------------------------------------------------------
4JN17MCA11 Jishi #9,kallalli B01 2
-----------------------------------------------------------------------------------
------------------------------------------------------------------
f)Display the student details who borrowed books of more than one author.
select *
from STUDENT S
where (select count(DISTINCT A.Authorid)
from BOOK BK,AUTHOR A,BORROW BR
where BK.Authorid=A.Authorid and BR.Bookid=BK.Bookid and
BR.USN=S.USN)>1;
---------------------------------------------------------------------------------
USN Name Address Branchid Sem
---------------------------------------------------------------------------------
4JN17MCA11 Jishi #9,kallalli B01 2
4JN17MCA37 Suraksha #11,vinobanagar B01 2
-----------------------------------------------------------------------------------
------------------------------------------------------------------
g)Display the book names in descending order of their names
select Bookname
from BOOK
ORDER BY Bookname DESC;
--------------
Bookname
--------------
UNIX
OOP
JAVA
DMS
DBMS
C++
C
-----------------------------------------------------------------------------------
------------------------------------------------------------------
h)List the details of students who borrowed the books which are all published by
the same publisher.
select *
from STUDENT S
where(select count(DISTINCT Publisher)
from BOOK BK,BORROW BR
where BR.Bookid=BK.Bookid and BR.USN=S.USN)=1 and USN in(select USN
from BORROW GROUP BY USN HAVING COUNT(*)>1);
-----------------------------------------------------------------
USN Name Address Branchid Sem
-----------------------------------------------------------------
4JN17MCA11 Jishi #9,kallalli B01 2