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

22MCAL27 DBMS Lab

This document describes creating tables to store data about a cricket tournament including teams, players, stadiums, and matches. It provides SQL statements to create the tables, insert sample data, and queries to return specific data. The queries return: 1) the youngest player's name, team, and age, 2) details of the stadium with the most matches, 3) players who received man of the match awards in 2+ matches but are not captains, 4) the team with the most wins, and 5) teams whose won matches were all at the same stadium.

Uploaded by

SRIKANTH BN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
327 views

22MCAL27 DBMS Lab

This document describes creating tables to store data about a cricket tournament including teams, players, stadiums, and matches. It provides SQL statements to create the tables, insert sample data, and queries to return specific data. The queries return: 1) the youngest player's name, team, and age, 2) details of the stadium with the most matches, 3) players who received man of the match awards in 2+ matches but are not captains, 4) the team with the most wins, and 5) teams whose won matches were all at the same stadium.

Uploaded by

SRIKANTH BN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1) Create the following tables with properly specifying Primary keys, foreign keys and solve the

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

CREATE TABLE BRANCH


(
BRANCHID NUMBER PRIMARY KEY,
BRANCHNAME VARCHAR(20),
HOD VARCHAR(20)
);

CREATE TABLE STUDENT


(
USN VARCHAR(30) PRIMARY KEY,
NAME VARCHAR(40),
ADDRESS VARCHAR(60),
BRANCHID NUMBER REFERENCES BRANCH,
SEM NUMBER
);
CREATE TABLE AUTHOR
(
AUTHORID NUMBER PRIMARY KEY,
AUTHORNAME VARCHAR(30),
COUNTRY VARCHAR(30),
AGE NUMBER
);

CREATE TABLE BOOK


(
BOOKID NUMBER PRIMARY KEY,
BOOKNAME VARCHAR(30),
AUTHORID NUMBER REFERENCES AUTHOR,
PUBLISHER VARCHAR(30),
BRANCHID NUMBER REFERENCES BRANCH
);

CREATE TABLE BORROW


(
USN VARCHAR(30) REFERENCES STUDENT,
BOOKID NUMBER REFERENCES BOOK,
BORROWED_DATE DATE
);

Insert Statement

INSERT INTO BRANCH VALUES (1,'MCA','SRINIVAS RAO');


INSERT INTO BRANCH VALUES (2,'BE','TIPPESWAMY');
INSERT INTO BRANCH VALUES (3,'BE_EC','RAVISHANKAR');
INSERT INTO BRANCH VALUES (4,'BE_ISE','JOHN');

INSERT INTO STUDENT VALUES ( '1ST16MCA01', 'ADARSH','KERALA',1,2);


INSERT INTO STUDENT VALUES ( '1ST16CS02', 'ANIS','KERALA',2,2);
INSERT INTO STUDENT VALUES ( '1ST16EC03', 'ARCHANA','CHANDIGARH',3,3);
INSERT INTO STUDENT VALUES ( '1ST16IS01', 'NAYON','WEST BENGAL',4,4);
IN SERT INTO STUDENT VALUES ( '1ST16MCA02', 'RAHUK','BIHAR',1,2);

INSERT INTO AUTHOR VALUES (11,'WILLIAM STALLING','AMERICA',30);


INSERT INTO AUTHOR VALUES (12,'PETERSON','CANADA', 40);
INSERT INTO AUTHOR VALUES (13,'PADMAREDDY','INDIA',35);
INSERT INTO AUTHOR VALUES (14,'NAMRATHA','INDIA',27);
INSERT INTO BOOK VALUES (111,'MCADBMS',11,'WILLIAM PUBLISHER',1);
INSERT INTO BOOK VALUES (222,'CSDBMS',12,'PETERSON PUBLISHER',2);
INSERT INTO BOOK VALUES (333,'ISEDBMS',13,'PADMAREDDY PUBLISHER',3);
INSERT INTO BOOK VALUES (444,'ECDBMS',14,'NAMRATHA PUBLISHER',4);

INSERT INTO BORROW VALUES( '1ST16MCA01',111,date '2010-11-02');


INSERT INTO BORROW VALUES( '1ST16CS02',222,date '2014-1-02');
INSERT INTO BORROW VALUES( '1ST16IS01',333,date '2014-4-05');
INSERT INTO BORROW VALUES( '1ST16IS01',333,date '2015-5-07');
INSERT INTO BORROW VALUES( '1ST16IS01',333,date '2017-4-05');

Queries
1. List the details of Students who are all studying in 2ndsem MCA.

SELECT * FROM STUDENT WHERE SEM=2;


Output
USN NAME ADDRESS BRANCHID SEM
1ST16MCA01 ADARSH KERALA 1 2
1ST16CS02 ANIS KERALA 2 2
1ST16MCA02 RAHUK BIHAR 1 2

2) List the students who are not borrowed any books.

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

3) Display the USN, Student name, Branch_name, Book_name, Author_name,


Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.
SELECT DISTINCT
A1.USN,A1.NAME,C.BOOKNAME,D.AUTHORNAME,A2.BORROWED_DATE
FROM STUDENT A1 JOIN BRANCH BR ON A1.BRANCHID=BR.BRANCHID JOIN
BORROW A2 ON A1.USN=A2.USN JOIN BOOK C ON A2.BOOKID=C.BOOKID JOIN
AUTHOR D ON C.AUTHORID=D.AUTHORID
WHERE A1.SEM=2 AND BR.BRANCHNAME='MCA';

Output

USN NAME BOOKNAME AUTHORNAME BORROWED_


1ST16MCA01 ADARSH MCADBMS WILLIAM STALLING 02-NOV-10

4) Display the number of books written by each Author


SELECT AUTHORNAME, COUNT(*)
FROM AUTHOR, BOOK
WHERE AUTHOR.AUTHORID=BOOK.AUTHORID
GROUP BY AUTHOR.AUTHORNAME;
Output
AUTHORNAME COUNT(*)
------------------------------ ----------
PETERSON 1
WILLIAM STALLING 1
PADMAREDDY 1
NAMRATHA 1
5) Display the student details who borrowed more than two books.
SELECT *
FROM STUDENT
WHERE USN IN (SELECT USN FROM BORROW GROUP BY USN HAVING
COUNT(*)>2);

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

7) Display the Book names in descending order of their names.

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.

CREATE TABLE TEAM


(TID INT PRIMARY KEY,
TNAMEVARCHAR(20),
COACH VARCHAR (20),
CAPTAIN_PIDINT, CITY VARCHAR(20));

CREATE TABLE PLAYER


(PIDINT PRIMARY KEY,
PNAMEVARCHAR(2),
AGE INT, TIDINT REFERENCES TEAM(TID));

CREATE TABLE STADIUM


(SID INT PRIMARY KEY,
SNAMEVARCHAR(20),
PICODE NUMBER(8),
CITY VARCHAR(20),
AREA VARCHAR(20));

CREATE TABLE MATCH


(MID INT PRIMARY KEY,
MDATE DATE, TIME VARCHAR(6),
SID INT REFERENCES STADIUM(SID),
TEAM1_ID INT REFERENCES TEAM(TID),
TEAM2_ID INT REFERENCES TEAM(TID),
WINNING_TEAM_IDINT REFERENCES TEAM(TID),
MAN_OF_MATCHINT REFERENCES PLAYER(PID));
CREATE TABLE PLAYER_PHONE
( PIDINT REFERENCES PLAYER(PID),
PHONE INT ,
PRIMARY KEY(PID,PHONE));

INSERT INTO TEAM VALUES(1,'INDIA','VIRAT',11,'DELHI');


INSERT INTO TEAM VALUES(2,'AUSTRALIA','SMITH',22,'SYDNEY');
INSERT INTO TEAM VALUES(3,'WESTINDIES','SAMMY',33,'KINGSTOWN');
INSERT INTO TEAM VALUES(4,'SRILANKA','SANGAKARA',44,'COLOMBO');

INSERT INTO PLAYER VALUES(101,'MS',35,1);


INSERT INTO PLAYER VALUES(102,'PS',40,2);
INSERT INTO PLAYER VALUES(103,'MM',26,3);
INSERT INTO PLAYER VALUES(104,'NO',36,4);

INSERT INTO STADIUM


VALUES(201,'CHINNASWAMY',560010,'BANGALORE','KARNATAKA');
INSERT INTO STADIUM
VALUES(202,'EADENGARDEN',200010,'KOLKATA','WESTBENGAL');
INSERT INTO STADIUM
VALUES(203,'MELBOURNE',122010,'MELBOURNE','AUSTRALIA');

INSERT INTO MATCH VALUES(301,DATE'2010-5-10','10AM',201,1,2,1,101);


INSERT INTO MATCH VALUES(302,DATE'2016-3-15','11AM',202,2,3,3,103);

INSERT INTO PLAYER_PHONE VALUES(101,100000);


INSERT INTO PLAYER_PHONE VALUES(102,2100000);
INSERT INTO PLAYER_PHONE VALUES(103,23330000);

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));

4 Display the Team details who won the maximum matches.


SELECT * FROM TEAM WHERE TID IN (SELECT WINNING_TEAM_ID FROM MATCH
GROUP BY WINNING_TEAM_ID HAVING COUNT(WINNING_TEAM_ID)=(SELECT
MAX(COUNT(WINNING_TEAM_ID))FROM MATCH GROUP BY WINNING_TEAM_ID));

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

CREATE TABLE CONSTITUENCY


(CONS_ID NUMBER(20) PRIMARY KEY,
CSNAME VARCHAR(20),
CSSTATE VARCHAR(20),
NO_OF_VOTERS NUMBER(10));

CREATE TABLE PARTY


(PID NUMBER(20) PRIMARY KEY,
PNAME VARCHAR(20),
PSYMBOL VARCHAR(10));

CREATE TABLE CANDIDATES


(CAND_ID NUMBER(12) PRIMARY KEY,
PHONE_NO NUMBER(10),
AGE NUMBER(2),
STATE VARCHAR(20),
NAME VARCHAR(20),
PID INT REFERENCES PARTY(PID));

CREATE TABLE CONTEST


(CONS_ID NUMBER(20) REFERENCES CONSTITUENCY(CONS_ID),
CAND_ID NUMBER(12) REFERENCES CANDIDATES(CAND_ID),
PRIMARY KEY(CONS_ID,CAND_ID));

CREATE TABLE VOTER


(VID NUMBER(20) PRIMARY KEY,
VNAME VARCHAR(20),
VAGE NUMBER(5),
VADDR VARCHAR(20),
CONS_ID NUMBER(20) REFERENCES CONSTITUENCY(CONS_ID),
CAND_ID NUMBER(12) REFERENCES CANDIDATES(CAND_ID));

INSERT INTO CONSTITUENCY VALUES(111,'rajajinagar','karnataka',4);


INSERT INTO CONSTITUENCY VALUES(222, 'ramnagar','kerala', 1);

INSERT INTO PARTY VALUES(876, 'bjp', 'lotus');


INSERT INTO PARTY VALUES(877, 'congress', 'hand');

INSERT INTO CANDIDATES VALUES(121, 9538904626, 23, 'kerala', 'raksha', 876);


INSERT INTO CANDIDATES VALUES(122, 9740777502, 24, 'karnataka', 'veena', 877);

INSERT INTO CONTEST VALUES(111, 122);


INSERT INTO CONTEST VALUES(222, 121);
INSERT INTO CONTEST VALUES(222, 122);

INSERT INTO VOTER VALUES(345, 'prashanth', 21,' kanakpura', 222, 122);


INSERT INTO VOTER VALUES(346, 'prakash', 23, 'ramnagar', 111 ,121);
INSERT INTO VOTER VALUES(348, 'nagesh' ,30,' mandya', 111, 121);
INSERT INTO VOTER VALUES(349, 'nagesh', '30', 'mandya', 111, 121);

Queries:
1 List the details of the candidates who are contesting from more than one constituencies which
are belongs to different states.

SELECT * FROM CANDIDATES WHERE CAND_ID IN (SELECT CAND_ID FROM


CONTEST JOIN CONSTITUENCY ON CONTEST.CONS_ID=CONSTITUENCY.CONS_ID
GROUP BY CAND_ID HAVING COUNT(DISTINCT(CSSTATE))>1);
2 Display the state name having maximum number of constituencies.

SELECT CSSTATE FROM CONSTITUENCY GROUP BY CSSTATE HAVING


COUNT(CSSTATE) IN (SELECT MAX(COUNT(CSSTATE)) FROM CONSTITUENCY
GROUP BY CSSTATE);

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;
/

SQL> SET SERVEROUTPUT ON;


SQL> EXEC AGECHECKING (25,21);
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED. // ROW INSERTED
SQL> EXEC AGECHECKING (20,15);
AGE SHOULD BE HIGH //MESSAGE DISPLAYED AS AGE IS LESS THAN OR EQUAL TO
18
PL/SQL PROCEDURE SUCCESSFULLY COMPLETED.

4 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.
CREATE OR REPLACE PROCEDURE DISPLAY_COUNT
(
CONST_ID NUMBER
)
AS
VID CONSTITUENCY.CONS_ID % TYPE;
BEGIN
SELECT NO_OF_VOTERS INTO VID FROM CONSTITUENCY WHERE CONS_ID =
CONST_ID
AND ROWNUM = 1;
DBMS_OUTPUT.PUT_LINE ( 'TOTAL VOTERS ARE: ' || VID);
END;
/

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 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.

CREATE OR REPLACE TRIGGER COUNT


AFTER INSERT ON VOTER
FOR EACH ROW
BEGIN
UPDATE CONSTITUENCY
SET NO_OF_VOTERS = NO_OF_VOTERS + 1
WHERE CONS_ID=:NEW.CONS_ID;
END COUNT;
/

SQL> set serveroutput on;


SQL> select * from constituency;
CONS_ID CSNAME CSSTATE NO_OF_VOTERS
---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 2
222 ramnagar kerala 1
SQL> insert into voter values(348,'nagesh',30,'mandya',111,121);
1 row created.
After insertion into voter table , the constituency table is
automatically updated.
SQL> select * from constituency;
CONS_ID CSNAME CSSTATE NO_OF_VOTERS
---------- -------------------- -------------------- ------------
111 rajajinagar karnataka 3
222 ramnagar kerala 1

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.

CREATE TABLE TOURIST_PLACE


(TPID NUMBER PRIMARY KEY,
HISTORY VARCHAR(20),
KILOMETERS NUMBER(3),
STATE VARCHAR(20),
TPNAME VARCHAR(20));

CREATE TABLE TOURIST(TID NUMBER PRIMARY KEY,


COUNTRY VARCHAR(20),
AGE NUMBER,
TNAME VARCHAR(20));

CREATE TABLE VISITS


(TPID NUMBER(3) REFERENCES TOURIST_PLACE(TPID),
TID NUMBER REFERENCES TOURIST(TID),
VDATE DATE,
PRIMARY KEY(TPID,TID));

CREATE TABLE EMAIL


(TID NUMBER REFERENCES TOURIST(TID),
EMAIL VARCHAR(20),PRIMARY KEY(TID,EMAIL));

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');

INSERT INTO VISITS VALUES('12','23','13-NOV-2014');


INSERT INTO VISITS VALUES('11','24','24-JUN-2012');
INSERT INTO VISITS VALUES('13','22','24-SEP-2011') ;
INSERT INTO VISITS VALUES('11','23','13-FEB-2010');
INSERT INTO VISITS VALUES('13','23','12-JAN-2010');

INSERT INTO EMAIL VALUES('23','[email protected]');


INSERT INTO EMAIL VALUES('22','[email protected]');
INSERT INTO EMAIL VALUES('24','[email protected]');

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);

2.List details of Tourist place where maximum number of tourists visited.


SELECT * FROM TOURIST_PLACE WHERE TPID IN (SELECT TPID FROM VISITS
GROUP
BY TPID HAVING COUNT(TPID)= (SELECT MAX(COUNT(TPID)) FROM VISITS GROUP
BY TPID));

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));

create table Student(USN varchar(5) primary key, Name varchar(30),DOB varchar(30),Branch


varchar(10), Mark1 integer(4), Mark2 integer(4), Mark3 integer(4), Total integer(3), GPA
integer(5));
desc Student;
insert into Student (USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values ("ca101","Asha","21-
05-2020","MCA",65,85,90);
insert into Student(USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values ("ca102","Arun","21-
05-2021","MCA",67,86,92);
insert into Student(USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values ("ca103","Shilpa","21-
05-2020","MCA",85,75,60);
insert into Student(USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values
("ca104","Sukumar","21-05-2019","MBA",65,55,55);
insert into Student (USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values ("ca105","Asha","21-
05-2020","MCA",65,85,90);
insert into Student (USN,Name,DOB,Branch,Mark1,Mark2,Mark3) values ("1001","Vinu","21-
05-2018","MCA",95,75,52);
select * from Student;
update Student set Total=Mark1+Mark2+Mark3;
select * from Student;
update Student set GPA=Mark1+Mark2+Mark3;
select * from Student;

You might also like