22MCAL27 DBMS Lab
22MCAL27 DBMS Lab
following queries.
i. BRANCH(Branchid,Branchname,HOD)
ii. STUDENT(USN,Name,Address,Branchid,sem)
iii. BOOK(Bookid,Bookname,Authorid,Publisher,Branchid)
iv. AUTHOR(Authorid,Authorname,Country,age)
v. BORROW(USN,Bookid,Borrowed_Date)
Queries:
1 List the details of Students who are all studying in 2ndsem MCA.
2 List the students who are not borrowed any books.
3 Display the USN, Student name, Branch name, Book name, Author name,
Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.
4 Display the number of books written by each Author.
5 Display the student details who borrowed more than two books.
6 Display the student details who borrowed books of more than one Author.
7 Display the Book names in descending order of their names.
8 List the details of students who borrowed the books which are all published by the same
Publisher.
Create Table
Insert Statement
Queries
1. List the details of Students who are all studying in 2ndsem MCA.
SELECT * FROM STUDENT WHERE USN NOT IN ( SELECT USN FROM BORROW);
Output
USN NAME ADDRESS BRANCHID SEM
1ST16EC03 ARCHANA CHANDIGARH 3 3
1ST16MCA02 RAHUK BIHAR 1 2
Output
Output
USN NAME ADDRESS BRANCHID SEM
1ST16IS01 NAYON WEST BENGAL 4 4
6) Display the student details who borrowed books of more than one Author.
SELECT *
FROM STUDENT
WHERE USN IN (SELECT USN FROM BORROW A JOIN BOOK B ON
A.BOOKID=B.BOOKID GROUP BY USN, B.AUTHORID HAVING
COUNT(B.AUTHORID)>1);
Output
USN NAME ADDRESS BRANCHID SEM
1ST16IS01 NAYON WEST BENGAL 4 4
SELECT BOOKNAME
FROM BOOK ORDER BY BOOKNAME DESC;
Output
BOOKNAME
------------------------------
MCADBMS
ISEDBMS
ECDBMS
CSDBMS
8 List the details of students who borrowed the books which are all published by the same
Publisher.
SELECT *
FROM STUDENT WHERE USN IN(SELECT A.USN FROM STUDENT A JOIN BORROW B
ON B.USN=A.USN JOIN BOOK C ON B.BOOKID=C.BOOKID GROUP BY A.USN HAVING
COUNT(C.PUBLISHER)=1);
Output
USN NAME ADDRESS BRANCHID SEM
1ST16MCA01 ANISH 49/5/1C 1 2
1ST16MCA03 DIPAK 49/8/1C 3 3
E-R DIAGRAM:
Scheme Diagram
3.Design an ER-diagram for the following scenario, Convert the same into a relational model and
then solve the following queries. Consider a Cricket Tournament “ABC CUP” organized by an
organization. In the tournament there are many teams are contesting each having a
Teamid,Team_Name, City, a coach. Each team is uniquely identified by using Teamid. A team
can have many Players and a captain. Each player is uniquely identified by Playerid, having a
Name, and multiple phone numbers,age. A player represents only one team. There are many
Stadiums to conduct matches. Each stadium is identified using Stadiumid, having a
stadium_name,Address ( involves city,area_name,pincode). A team can play many matches. Each
match played between the two teams in the scheduled date and time in the predefined Stadium.
Each match is identified uniquely by using Matchid. Each match won by any of the one team that
also wants to record in the database. For each match man_of_the match award given to a player.
Execute the following Queries:
i. Display the youngest player (in terms of age) Name, Team name, age in which he belongs of the
tournament.
ii. List the details of the stadium where the maximum number of matches were played.
iii. List the details of the player who is not a captain but got the man_of _match award at least in
two matches.
iv. Display the Team details who won the maximum matches.
v. Display the team name where all its won matches played in the same stadium.
Queries:
1 Display the youngest player (in terms of age) Name, Team name , age in which he belongs of
the tournament.
SELECT B.NAME, A.TEAMNAME, B.AGE
FROM TEAM A , PLAYER B
WHERE A.TEAMID = B.TEAMID AND B.AGE=(SELECT MIN(AGE) FROM PLAYER ;
2 List the details of the stadium where the maximum number of matches were played.
SELECT * FROM STADIUM WHERE SID IN (SELECT SID FROM MATCH GROUP BY
SID HAVING COUNT(SID) = (SELECT MAX(COUNT(SID)) FROM MATCH GROUP BY
SID)) ;
3 List the details of the player who is not a captain but got the man_of _match award at least in
two matches.
SELECT * FROM PLAYER WHERE PID NOT IN ( SELECTCAPTAIN_PID FROM TEAM
WHERE CAPTAIN_PID NOT IN (SELECT MAN_OF_MATCH FROM MATCH GROUP BY
MAN_OF_MATCH HAVING COUNT(MAN_OF_MATCH)=2));
5 Display the team name where all its won matches played in the same stadium.
SELECT TNAME FROM TEAM WHERE TID IN (SELECT WINNING_TEAM_ID FROM
MATCH GROUP BY(WINNING_TEAM_ID,SID) HAVING COUNT(*) IN (SELECT
COUNT(WINNING_TEAM_ID) FROM MATCH GROUP BY WINNING_TEAM_ID));
4.A country wants to conduct an election for the parliament. A country having many
constituencies. Each constituency is identified uniquely by Constituency_id, having the Name,
belongs to a state,Number_of_voters. A constituency can have many voters. Each voter is
uniquely identified by using Voter_id, having the Name, age, address (involves
Houseno,city,state,pincode). Each voter belongs to only one constituency. There are many
candidates contesting in the election. Each candidates are uniquely identified by using
candidate_id, having Name, phone_no, age, state. A candidate belongs to only one party.Thereare
many parties. Each party is uniquely identified by using Party_id, having
Party_Name,Party_symbol. A candidate can contest from many constituencies under a same
party. A party can have many candidates contesting from different constituencies. No
constituency having the candidates from the same party. A constituency can have many contesting
candidates belongs to different parties. Each voter votes only one candidate of his/her
constituencty.
Queries:
i. List the details of the candidates who are contesting from more than one constituencies which
are belongs to different states.
ii. Display the state name having maximum number of constituencies.
iii. Create a stored procedure to insert the tuple into the voter table by checking the voter age. If
voter’s age is at least 18 years old, then insert the tuple into the voter else display the “Not an
eligible voter msg”.
iv. Create a stored procedure to display the number_of_voters in the specified constituency.
Where the constituency name is passed as an argument to the stored procedure.
v. Create a TRIGGER to UPDATE the count of “ Number_of_voters” of the respective
constituency in “CONSTITUENCY” table , AFTER inserting a tuple into the “VOTERS” table
Queries:
1 List the details of the candidates who are contesting from more than one constituencies which
are belongs to different states.
3 Create a stored procedure to insert the tuple into the voter table by checking the voter age. If
voter’s age is at least 18 years old, then insert the tuple into the voter else display the “Not an
eligible voter msg” .
CREATE OR REPLACE PROCEDURE AGECHECKING ( ID IN NUMBER,AGE IN
NUMBER)
AS
BEGIN
IF AGE>18 THEN
INSERT INTO VOTER(VID,VAGE) VALUES (ID,AGE);
ELSE
DBMS_OUTPUT.PUT_LINE('AGE SHOULD BE HIGH');
END IF;
END AGECHECKING;
/
Procedure created.
SQL> select * from constituency;
CONS_ID CSNAME CSSTATE NO_OF_VOTERS
---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 2
222 ramnagar kerala 1
SQL> exec display_count(111);
total voters are: 2
5.Design an ER-diagram for the following scenario, Convert the same into a relational model,
normalize Relations into a suitable Normal form and then solve the following queries. A country
can have many Tourist places . Each Tourist place is identified by using tourist_place_id, having a
name, belongs to a state, Number of kilometers away from the 02.03.2021 updated 52/ 104 capital
city of that state,history. There are many Tourists visits tourist places every year. Each tourist is
identified uniquely by using Tourist_id, having a Name, age, Country and multiple emailids. A
tourist visits many Tourist places, it is also required to record the visted_date in the database. A
tourist can visit a Tourist place many times at different dates. A Tourist place can be visited by
many tourists either in the same date or at different dates.
Queries:
i. List the state name which is having maximum number of tourist places.
ii. List details of Tourist place where maximum number of tourists visited.
iii. List the details of tourists visited all tourist places of the state “KARNATAKA”.
iv. Display the details of the tourists visited at least one tourist place of the state, but visited all
states tourist places.
v. Display the details of the tourist place visited by the tourists of all country.
INSERT INTO
TOURIST_PLACE(TPID,HISTORY,KILOMETERS,STATE,TPNAME)VALUES('11','BEAUT
Y',' 160','KARNATAKA','OOTY');
INSERT INTO
TOURIST_PLACE(TPID,HISTORY,KILOMETERS,STATE,TPNAME)VALUES('12','MONU
MENT',' 260','KERLA','BELURU');
INSERT INTO
TOURIST_PLACE(TPID,HISTORY,KILOMETERS,STATE,TPNAME)VALUES('13','BEACH'
,' 360','TAMILNANDU','MARINA');
INSERT INTO
TOURIST(TID,COUNTRY,AGE,TNAME)VALUES('22','INDIA','30','ASHOK');
INSERT INTO TOURIST(TID,COUNTRY,AGE,TNAME)VALUES('23','USA','25','KIRAN');
INSERT INTO
TOURIST(TID,COUNTRY,AGE,TNAME)VALUES('24','LONDON','26','ABHI');
1.List the state name which is having maximum number of tourist places.
SELECT STATE FROM TOURIST_PLACE GROUP BY STATE HAVING
COUNT(STATE)=(SELECT MAX(COUNT(STATE)) FROM TOURIST_PLACE GROUP BY
STATE);
3List the details of tourists visited all tourist places of the state “KARNATAKA”
SELECT * FROM TOURIST T WHERE T.TID IN
(SELECT TID FROM VISITS JOIN TOURIST_PLACE ON
VISITS.TPID=TOURIST_PLACE.TPID WHERE STATE='KARNATAKA'
GROUP BY TID HAVING COUNT(STATE) IN (SELECT COUNT(STATE ) FROM
TOURIST_PLACE WHERE STATE='KARNATAKA') );
4.Display the details of the tourists visited at least one tourist place of the state, but visited all
states tourist places.
SELECT * FROM TOURIST T WHERE T.TID IN (SELECT TID FROM VISITS JOIN
TOURIST_PLACE ON VISITS.TPID=TOURIST_PLACE.TPID
GROUP BY TID HAVING COUNT(DISTINCT STATE)
IN (SELECT COUNT(DISTINCT STATE ) FROM TOURIST_PLACE) );
5 Display the details of the tourist place visited by the tourists of all country.
SELECT * FROM TOURIST_PLACE WHERE TPID IN (
SELECT TPID FROM VISITS JOIN TOURIST ON VISITS.TID=TOURIST.TID
GROUP BY TPID HAVING COUNT(DISTINCT COUNTRY)=
(SELECT COUNT(DISTINCT COUNTRY) FROM TOURIST));