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

NAME: Atharva Chilwerwar

21MIS0247

Train (train_number, name, source, destination, start_time,


reach_time,traveltime, distance, class, days, type)

Ticket (PNR_no,Transactionid, from_station, To_station,


date_of_journey,class date_of_booking, total_ticket_fare,
train_number)

Passenger (PNR_no, Serial_no, Name, Age,


Reservation_status)

Train_Route(Train_no, route_no, station_code, name,


arrival_time,
depart_time, distance,day)

Train_Ticket_fare(Train_no, class, base_fare,


reservation_charge,
superfast_charge, other_charge, tatkal_charge, service_tax)
QUESTION 5:

Write Queries to

Use SET Operators

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

select train_no from train


minus
select train_number from ticket;

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

select name from train where train_no in


(select train_no from train_ticket_fare where class not like '%A%');
3. Print all the PNR numbers available in the database.

select distinct(pnr_no) from passenger


union all
select distinct(pnr_no) from ticket;

4. Find passenger names who have booked to 'Pune'.

select name from passenger, ticket where


(passenger.pnr_no=ticket.pnr_no and ticket.to_station='Pune');
Use Nested Query (in Operators)

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

select train.name from train, train_route where


train_route.name='Katpadi' and train_route.train_no=train.train_no;

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

select name from train where train_no= (select train_no from


train_ticket_fare where service_tax=0);
3. Find the Passenger name who have booked for the train that
starts from 'Chennai'.

select name from passenger where pnr_no in (select pnr_no from


ticket where from_station like 'CHENNAI%');

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

select name from train where train_no in (select train_no from


train_ticket_fare where class like '%A' and base_fare<3000 );

Use Join Query


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

select name from train join ticket on


train.train_no=ticket.train_number where ticket.to_station='Katpadi';

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

Select name from train join train_ticket_fare on train.train_no =


train_ticket_fare.train_no where train_ticket_fare.service_tax=0 and
train.type='Superfast';
3. Find the Passenger name (and train name) who have booked for
the train that starts from 'Chennai'.

Select name from passenger join ticket on


passenger.pnr_no=ticket.pnr_no;

4. Display the train names, each type of class and the total fare for
each type of class.

Select train.name ,train.class, train_ticket_fare.base_fare from train ,


train_ticket_fare where train.train_no= train_ticket_fare.train_no;
5. Display all the train details and the ticket details (if booked any).

Select * from train t, ticket ti where t.train_no = ti.train_number;


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

Select t.pnr_no, p.pnr_no from ticket t, passenger p where


t.pnr_no=p.pnr_no;

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

Select * from train t,ticket ti where t.train_no= ti.train_number;


QUESTION 6:

Write Queries to

Use Correlated (and nested) Query

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

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

Select name from train t where train_no in (select train_number from


ticket ti where t.train_no=ti.train_number and ti.train_number>10);
3. Find the passengers who do not pass through 'Mettupalam'.

Select name from passenger p where pnr_no in(select pnr_no from


ticket t where exists (select 1 from train_route r where
t.train_number=r.train_no and r.route_no in ('1','4')));

4. Find passengers who have booked for superfast trains.

Select name from passenger where pnr_no in (select pnr_no from


passenger intersect select
pnr_no from ticket where train_number in(select train_number from
ticket intersect select train_no
from train_ticket_fare where superfast_charge is NOT NULL));
Complex queries (use groupby/groupby having/join/nested)

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

Select t.train_no, t.name, q.from_station,q.to_station from train t left


outer join ticket q on
t.train_no=q.train_number;
2. List the train names and the number of sub stations it has.

Select t.name , q.sub_station from train t left outer join train_route q


on t.train_no=q.train_no;

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

Select distinct (q.to_station) from train t right outer join ticket q on


t.train_no=q.train_number;

4. List the train names that have at least four bookings.

Select t.name from train t where t.train_no in (select train_number


from ticket q where q.pnr_no in(select
pnr_no from passenger p where p.reservation_status='CNF'));
5. Create a table cancellation history (Insert values from ticket and
passenger table).

Create table cancellation_history(


DOC date,pnr_no number(5));

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

select name from passenger where pnr_no in (select pnr_no from


passenger intersect select pnr_no from ticket where train_number in
(select train_number from ticket intersect select train_no from
train_ticket_fare
where Superfast_Charge is not NULL ));

You might also like