Database Management Systems Lab Assessment-3: Name: Atharv Kugaji REG NO:19BIT0347

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

DATABASE MANAGEMENT SYSTEMS

LAB ASSESSMENT-3
NAME: ATHARV KUGAJI
REG NO:19BIT0347

5)Write Queries to.


Use SET Operators

1)Find the train numbers for which reservation have not yet been made.

CODE:

SQL> Select tno from train Minus Select train_number from ticket;

OUTPUT:

2)Find the train names that do not have a first AC class coach.

CODE

SQL> select tname from train minus Select tname from train,table(train.class) where
column_value='AC1';

OUTPUT:

3) Print all the PNR nos available in the database.

CODE:

SQL> Select PNR_NO from ticket Union Select PNR_NO from Passenger;

OUTPUT:
4)Find passenger names who have booked to 'Pune'.

CODE:

SQL> insert into ticket values(1234565345,183748473847434,'Kanpur','Pune','24-


Aug2021',400,12345,'3A');

1 row created.

SQL> insert into passenger values(1234565345,6,'MOHAN',35,'CONFIRM');

1 row created.

SQL> SELECT P_NAME FROM TICKET NATURAL JOIN PASSENGER WHERE TO_STATION='Pune';

P_NAME

------------------------------

MOHAN

SQL>

OUTPUT:

Use Nested Query(in Operators)

1)Find the train names that stop in 'Katpadi'.

CODE:

SQL> insert into train_route(train_no,route_no,station_code,route_name,distance)


values(89898,87097,'KPD','Katpadi',80.9);
1 row created.

SQL> select tname from train where tno=(select train_no from train_route where
route_name='Katpadi');

TNAME

------------------------------

CHENAI SUPERFAST

OUTPUT:

2)Find the train names that are superfast and the service tax is zero.

CODE:

SQL> insert into train_ticket_fare(train_number,class,service_tax) values(89898,'3A',0);

1 row created.

SQL> select tname from train where tno=(select train_number from train_ticket_fare where
service_tax=0) and train_type='SUPERFAST';

TNAME

------------------------------

CHENAI SUPERFAST

OUTPUT:

NOTE:- Before tname we can also add the word “distinct” as a preventive measure because the inside
nested query my repeat the tno again.
3)Find the Passenger name who have booked for the train that starts from 'Chennai'.

CODE:

SQL> select distinct p_name from passenger where pnr_no in (select pnr_no from ticket where
train_number in(select tno from train where source='CHENAI'));

P_NAME

------------------------------

Rahul

OUTPUT:

4.Find the trains names that have all the AC coaches and the base fare is less than 3000 for each
case.

CODE:

select name from train where train_number in (select train_number from train_ticket_fare where
class like '%A' and base_fare<3000);

Use Join Query

1)Find the train names that stop in 'Katpadi'.

CODE:

SQL> select tname from train natural join train_route where route_name='Katpadi';

TNAME

------------------------------

BISWAN PASSENGER

OUTPUT:
2)Find the train names that are superfast and the service tax is zero.

CODE:

SQL> select tname from train inner join train_ticket_fare on

train_ticket_fare.train_number=train.tno where train_type= 'SUPERFAST' AND

SERVICE_TAX=0;

TNAME

------------------------------

CHENAI SUPERFAST

OUTPUT:

3) Find the Passenger name (and train name) who have booked for the train that starts from

'Chennai'.

CODE:

SQL> select p_name ,tname from passenger natural join (ticket inner join train on
train.tno=ticket.train_number) where source='CHENAI';

P_NAME TNAME

------------------------------ ------------------------------

MOHAN CHENAI SUPERFAST

Rahul CHENAI SUPERFAST

OUTPUT:

4) Display the trains names, each type of class and the total fare for each type of class.
CODE:

SQL>select train.tname,train_ticket_fare.class,nvl(base_fare,0)+ nvl(reservation_charge,0)+


nvl(SUPERFAST_charge,0)+nvl( OTHER_CHARGE,0)+nvl(TATKAL_CHARGE,0)+nvl(

SERVICE_TAX,0) "Total fare" from train inner join train_ticket_fare on


train.tno=train_ticket_fare.train_number;

OUTPUT:

5)Display all the train details and the ticket details(if booked any).

CODE:

select pnr_no,date_of_journey,tno,tname from ticket natural join train;

PNR_NO DATE_OF_J TNO TNAME

---------- --------- ---------- ------------------------------

7898989867 01-DEC-20 12345 Satbhavna Express

7898989867 01-DEC-20 89898 CHENAI SUPERFAST

7898989867 01-DEC-20 78787 MUMBAI EXPRESS

7898989867 01-DEC-20 23435 BISWAN PASSENGER

7898989867 01-DEC-20 34543 GANGTOK PASSENGER

9845969545 30-NOV-20 12345 Satbhavna Express

9845969545 30-NOV-20 89898 CHENAI SUPERFAST

9845969545 30-NOV-20 78787 MUMBAI EXPRESS

9845969545 30-NOV-20 23435 BISWAN PASSENGER

9845969545 30-NOV-20 34543 GANGTOK PASSENGER

7878675678 30-NOV-20 12345 Satbhavna Express

PNR_NO DATE_OF_J TNO TNAME

---------- --------- ---------- ------------------------------

7878675678 30-NOV-20 89898 CHENAI SUPERFAST

7878675678 30-NOV-20 78787 MUMBAI EXPRESS

7878675678 30-NOV-20 23435 BISWAN PASSENGER


7878675678 30-NOV-20 34543 GANGTOK PASSENGER

8987678987 06-OCT-20 12345 Satbhavna Express

8987678987 06-OCT-20 89898 CHENAI SUPERFAST

8987678987 06-OCT-20 78787 MUMBAI EXPRESS

8987678987 06-OCT-20 23435 BISWAN PASSENGER

8987678987 06-OCT-20 34543 GANGTOK PASSENGER

7656789098 24-AUG-20 12345 Satbhavna Express

7656789098 24-AUG-20 89898 CHENAI SUPERFAST

PNR_NO DATE_OF_J TNO TNAME

---------- --------- ---------- ------------------------------

7656789098 24-AUG-20 78787 MUMBAI EXPRESS

7656789098 24-AUG-20 23435 BISWAN PASSENGER 7656789098 24-AUG-20 34543 GANGTOK


PASSENGER

25 rows selected.

NOTE:- In order to display and show the output we have placed the names of some fields to display
all details replace * with the name of the fields.

OUTPUT:
6)Create a sequence to provide values for the PNR no.

CODE:

SQL> create sequence p;

Sequence created.

SQL> insert into ticket(pnr_no) values(p.nextval);

1 row created.

SQL> select pnr_no from ticket;

PNR_NO

--------- 1

7656789098

7878675678

7898989867

8987678987

9845969545

6 rows selected
OUTPUT:

7. Write a query for full outer join using any of the tables above.

CODE:

SQL> select ticket.pnr_no,passenger.pnr_no,p_name,from_station,to_station from ticket full outer


join passenger on ticket.pnr_no=passenger.pnr_no;

PNR_NO PNR_NO P_NAME

---------- ---------- ------------------------------

FROM_STATION TO_STATION

------------------------------ ------------------------------

7898989867

Lucknow Chandigarh

9845969545

GUHATI GANGTOK

7878675678

GUHATI GANGTOK

PNR_NO PNR_NO P_NAME

---------- ---------- ------------------------------

FROM_STATION TO_STATION

------------------------------ ------------------------------

8987678987

SITAPUR BISWAN

7656789098
CHENAI KANPUR

OUTPUT:

6) Write Queries to.


Use Coorelated (and nested) Query

1)Find the train names for which ten tickets have been reserved.

CODE:

SQL> select tname from train inner join ticket on ticket.train_number=train.tno group by tname
having count(pnr_no)=10;

TNAME

------------------------------

Satbhavna Express

OUTPUT:

2)Find the trains that have more than ten substations.


CODE:

SQL> select tname from train where tno in(select tno from train_route,train where
train.tno=train_route.train_no group by tno having count(station_code)>10);

TNAME

------------------------------

MUMBAI EXPRESS

OUTPUT:

3. Find the passengers who do not pass through 'Mettupalam'.

CODE:

SQL> select p_name from passenger where exists(select * from train_route,ticket where
train_route.train_no=ticket.train_number and passenger.pnr_no=ticket.pnr_no and
train_route.route_name<>'Mettupalam');

P_NAME

------------------------------

Rahul

MOHAN

ROHAN

OUTPUT:

4. Find passengers who have booked for super fast trains.

CODE:

SQL> select distinct p_name from passenger where pnr_no in(select pnr_no from train INNER join
ticket ON

train.tno=ticket.train_number where train_type='SUPERFAST');

P_NAME

------------------------------

LAJPAT

OUTPUT:
Complex queries(use groupby/groupby having/join/nested)

1)Take the start station code and end station code and display the train details.

CODE:

SQL> select * from train where source in(select route_name from train_route where
station_code=&station_code) and destination in

(select route_name from train_route where

station_code=&station_code); Enter value for station_code: 'LKO' Enter value for station_code: 'CNB'
old 1: select * from train where source in(select route_name from train_route where
station_code=&station_code) and destination in

(select route_name from train_route where station_code=&station_code) new 1: select * from train
where source in(select route_name from train_route where station_code='LKO') and destination in
(select route_name from train_route where station_code='CNB') no rows selected

OUTPUT:

2. List the train names and the number of sub stations it has.

CODE:

select train.tname,count(train_route.route_name) from train,train_route where


train.tno=train_route.train_no group by train.tname;

TNAME

COUNT(TRAIN_ROUTE.ROUTE_NAME)

------------------------------ ----------------------------

MUMBAI EXPRESS 11

BISWAN PASSENGER 1
OUTPUT:

3. List the stations where all types of trains stop.

CODE:

SQL> select train_route.route_name from train_route where not exists(select train_type from train
minus select train_type from train where train.tno=train_route.train_no);

ROUTE_NAME

--------------------

Lucknow

OUTPUT:

4. List the trains names that has atleast four bookings.

CODE:

SQL> select tname from train join ticket on train.tno=ticket.train_number group by tname having
count(pnr_no)>=4;

TNAME

------------------------------

Satbhavna Express

OUTPUT:

5. Create a table cancellation history(Insert values from ticket and passenger table).

CODE:

SQL> create table cancellation_history

2 (
3 cancel_ID varchar(10) primary key,

4 Cancel_date date,

5 pnr_no number(10),

6 constraint fk_tp foreign key(pnr_no) references ticket(pnr_no),

7 tno number(5), constraint fk_tn foreign key(tno) references train(tno));

OUTPUT:

6. Create a table for all the train numbers and class available in train_ticket_fare with

total seats.

CODE:

SQL> create table seats as select train_number,class from train_ticket_fare;

Table created.

SQL> alter table seats add total_seats number(2);

Table altered.

SQL> alter table seats add primary key(train_number,class);

Table altered.

OUTPUT:
7. Find the station name that has highest number of trains stopping at.

CODE:

SQL> select route_name from train_route group by route_name having count(train_no)=max(select


count(train_no) from train_route group by route_name);

OUTPUT:

You might also like