Professional Documents
Culture Documents
Database Management Systems Lab Assessment-3: Name: Atharv Kugaji REG NO:19BIT0347
Database Management Systems Lab Assessment-3: Name: Atharv Kugaji REG NO:19BIT0347
Database Management Systems Lab Assessment-3: Name: Atharv Kugaji REG NO:19BIT0347
LAB ASSESSMENT-3
NAME: ATHARV KUGAJI
REG NO:19BIT0347
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:
CODE:
SQL> Select PNR_NO from ticket Union Select PNR_NO from Passenger;
OUTPUT:
4)Find passenger names who have booked to 'Pune'.
CODE:
1 row created.
1 row created.
SQL> SELECT P_NAME FROM TICKET NATURAL JOIN PASSENGER WHERE TO_STATION='Pune';
P_NAME
------------------------------
MOHAN
SQL>
OUTPUT:
CODE:
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:
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);
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:
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
------------------------------ ------------------------------
OUTPUT:
4) Display the trains names, each type of class and the total fare for each type of class.
CODE:
OUTPUT:
5)Display all the train details and the ticket details(if booked any).
CODE:
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:
Sequence created.
1 row created.
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:
FROM_STATION TO_STATION
------------------------------ ------------------------------
7898989867
Lucknow Chandigarh
9845969545
GUHATI GANGTOK
7878675678
GUHATI GANGTOK
FROM_STATION TO_STATION
------------------------------ ------------------------------
8987678987
SITAPUR BISWAN
7656789098
CHENAI KANPUR
OUTPUT:
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:
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:
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:
CODE:
SQL> select distinct p_name from passenger where pnr_no in(select pnr_no from train INNER join
ticket ON
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
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:
TNAME
COUNT(TRAIN_ROUTE.ROUTE_NAME)
------------------------------ ----------------------------
MUMBAI EXPRESS 11
BISWAN PASSENGER 1
OUTPUT:
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:
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:
2 (
3 cancel_ID varchar(10) primary key,
4 Cancel_date date,
5 pnr_no number(10),
OUTPUT:
6. Create a table for all the train numbers and class available in train_ticket_fare with
total seats.
CODE:
Table created.
Table altered.
Table altered.
OUTPUT:
7. Find the station name that has highest number of trains stopping at.
CODE:
OUTPUT: