You are on page 1of 6

Q1.

)Consider the following database consisting of the following tables:


bus(busno,source,destination)
ticket(ticketno,departure_time,date_of_journey,age,gender,destination,source,pnr_number)
passenger(name,pnr_number,address,gender,age,ppno)
reservation(address,status,bus_no,journey_date,no_of_seats,contact_number)
cancellation(address,pnr_number,status,journey_date,no_of_seats,contact_number)
a. Display the bus details.
b. Display unique pnr_no of all the passengers.
c. Display the ticket numbers and the names of all the passengers.
d. Find the ticket numbers of the passengers whose name starts with 'r' and ends
with 'h'.
e. create procedure on ticket table to display ticketno,source,destination when
given the ticketno.

Ans)
a. mysql>select * from bus;
b. mysql>select pnr_number from passenger;
c. mysql>select t.ticketnumber, p.name from ticket t, passenger p where
t.pnr_number=p.pnr_number;
d. mysql>select t.ticketno from ticket t, passenger p where p.name like 'r%';
e. mysql>delimiter //
mysql>create procedure p1(ticno int)
->begin
->select ticketno,source,destination from ticket where ticketno=ticno;
->end //
------------------------------------------------------------------------------------------------
Q2.)Consider the following database consisting of the following tables:
bus(busno,source,destination)
ticket(ticketno,departure_time,date_of_journey,age,gender,destination,source,pnr_number)
passenger(name,pnr_number,address,gender,age,ppno)
reservation(address,status,bus_no,journey_date,no_of_seats,contact_number)
cancellation(address,pnr_number,status,journey_date,no_of_seats,contact_number)
a. Find the names of passengers whose age is between 30 and 45.
b. Display all the passengers names beginning with 'A'.
c. Display the sorted list of passsenger names.
d. Implements queries aggregate functions(COUNT,SUM,AVG,and MAX and MIN),GROUP BY.

Ans)
a.mysql>select p.name from passenger p where 30<p.age<45;
b.mysql>select p.name from passenger p where p.name like 'A%';
c.mysql>select name from passenger order by name;
d.mysql>select count(*) from bus;
mysql>select sum(no_of_seats) from reservation;
mysql>select avg(no_of_seats) from reservation;
mysql>select max(no_of_seats) from reservation;
mysql>select min(no_of_seats) from reservation;
mysql>select count(no_of_seats) from reservation group by busno;

------------------------------------------------------------------------------------------------
Q3.)Consider the following database consisting of the following tables:
bus(busno,source,destination)
ticket(ticketno,departure_time,date_of_journey,age,gender,destination,source,pnr_number)
passenger(name,pnr_number,address,gender,age,ppno)
reservation(address,status,bus_no,journey_date,no_of_seats,contact_number)
cancellation(address,pnr_number,status,journey_date,no_of_seats,contact_number)
a. Write a query to display the infomrmation present in the passenger and cancellation
tables(union)
b. Find the number of booked tickets for each pnr_no using group by clause.
c. Find the total number of cancellation seats.
d. Create a trigger on cancel table before deleting a row from cancel table the same row
should be added to reserve table.

Ans)
a.mysql>select * from passenger p, cancellation c where p.pnr_number=c.pnr_number;
b.mysql>select count(ticketno) from ticket group by pnr_number;
c.mysql>select sum(no_of_seats) from cancellation;
d.mysql>delimiter //
mysql>create trigger t1 before delete on cancellation
->for each row
->begin
->insert into reservation values(old.address,old.pnr_number,old.status,
old.journey_date,old.no_of_seats,old.contact_number);
->end//
------------------------------------------------------------------------------------------------
Q4.)Consider the folowing database consisting of the following tables:
customer(cust_id,cust_name)
item(item_id,item_name,price)
sale(bill_no,bill_date,cust_id,item_id,qty_sold)
a. create the tables with the appropriate integrity constraints.
b. insert around 5 records in each of the tables
c. list all the bills for the current date with the customer names and item numbers
d. list all the total bill details with the quantity sold,price of the item and the final
amount
e. list the details of the customer who have bought a product which has price >200
f. Give a count of how many products have been bought by each customer
g. create a view which lists out the bill_no,bill_date,cust_id,item_id,price,qty_sold ,amount.
Ans)
c.mysql> select c.cust_name, i.item_id from customer c ,sales s, item i where
s.bill_date=CURDATE() and c.cust_id=s.cust_id and i.item_id=s.item_id;
d.mysql> select s.bill_no, s.bill_date, s.qty_sold, i.price, s.qty_sold*i.price as total from
sale s, item i, where i.item_id=s.item_id;
e.mysql> select s.cust_id,c.cust_name from sale s,customer c,item i where s.item_id=i.item_id
and s.cust_id=c.cust_id and i.price>200;
f.mysql> select cust_id,count(*) as no_of_products from sale group by cust_id;
g.mysql>create view view1 as select
s.bill_no,s.bill_date,s.cust_id,s.item_id,i.price,s.qty_sold,(i.price*qty_sold) as amount from sale
s,item i where s.item_id=i.item_id;
-------------------------------------------------------------------------------------------------
Q5.)Consider the folowing database consisting of the following tables:
student(stud_no,stud_name)
membership(mem_no,stud_no)
book(book_no,book_name,author)
iss_rec(iss_no,iss_date,mem_no,book_no)
For the above schema,perform the following-
a.Create the tables withe the appropriate integrity constraints
b.insert around 5 records in each of the tables
c.list of all the student names with their membership numbers
d.give a count of how many books have been bought by each student
e.give a list of books taken by student with stud_no 5
f.create a view which lists aout the iss_no,iss_date,stud_name,book name

Ans) c.mysql>select m.mem_no,s.stud_name from membership m,student s where


m.stud_no=s.stud_no;
d.select mem_no,count(*) as no_of_books from iss_rec group by mem_no;
e.select b.book_name from book b,iss_rec i,memebership m where i.mem_no=m.mem_no
and m.stu_no=5;
f.create view view2 as select i.iss_no,i.iss_date,s.stu_name,b.book_name from iss_rec
i,book b,student s,membership m where i.mem_no=m.mem_no and i.book_no=b.book_no and
m.stud_no=s.stud_no;

-------------------------------------------------------------------------------------------------
Q6.)Consider the folowing database consisting of the following tables:
student(stud_no,stud_name,class)
class(class,descrip)
lab(mach_no,lab_no,description)
allotment(stud_no,mach_no,day_of_week)
For the above schema,perform of the following
a.create the tables with the appropriate integrity constraints
b.insert around 5 records in each the tables
c.list all the machine allotments with the student names,lab and machine numbers
d.give a count of how many machines have been allocated to the 'cse' class
e.give a machine allotment details of the stud_no 5 with his personal and class details
f.count for how many machines have been allocated in lab_no 1 for the day of the week as
"Monday"
g.create a view which lists the machine allotment details for "Thursday"

Ans)
c.mysql> select a.mach_no,a.day_of_week,s.stud_name,l.lab_no from allotment a, student s,
lab l, where a.stud_no=s.stud_no and a.mach_no=l.mach_no;
d.mysql> select count(DISTINCT a.mach_no) from allotment a, student s where s.class='CSE'
and s.stud_no=a.stud_no;
e.mysql> select s.stud_no,s.stud_name,s.class,a.day_of_week,a.mach_no from allotment a,
student s,
where s.stud_no=a.stud_no and s.stud_no=5;
f.mysql> select count(mach_no) from allotment a,lab l where day_of_week="Monday" and
a.mach_no=l.mach_no and l.lab_no=1;
g.mysql> create view view3 as select a.stud_no,a.mach_no,a.day_of_week,l.lab_no from
allotment a,lab l where a.mach_no=l.mach_no;

-------------------------------------------------------------------------------------------------
Q7.)Consider the following database consisting of the following tables:
employee(eid,ename,address,phone,salary,dept_no,designation)
department(dept_no,dept_name,location)
a.create the tables with appropriate integrity constraints
b.insert around 5 records in each of the tables
c.list for each clerk in employee table,retrieve the name,deptno,deptname and address.
d.list all the employees who are working at a specific location HYD in the department
e.find the difference between min and max salary.
f.list all the employees details whose name starts with 'J' or 'T'
g.create a trigger before insert on employee table based on salary (>9000).

Ans)
c.mysql> select e.ename,e.dept_no,d.dept_name,e.address from employee e, department d
where
e.dept_no=d.dept_no;
d.mysql> select e.name from employee e, department d where e.dept_no=d.dept_no and
d.location='HYD';
e.mysql> select max(salary)-min(salary) from employee;
f.mysql> select * from employee where ename like '%J' or ename like '%T';
g.delimiter //
create trigger trig_ins before insert on employee
for each row
begin
if new.salary>9000 then
set new.salary=new.salary+1000;
end if;
end//

--------------------------------------------------------------------------------------------------
Q8)Consider the following database consisting of the following tables:
sailor(sid,sname,age,rating)
boat(bid,bname,color)
reserves(sid,bid,day)
a.find the names of the sailor who reserved red boat
b.find the name of the sailor who reserved red but not green
c.find all sid's of the sailors who have rating of 10

Ans)
a.mysql>select s.sname from sailor s,boat b ,reserves r where r.rid=s.sid and b.color='red';
b.mysql>select s.sname from sailor s,boat b, reserve r where s.sid=r.sid and r.bid=bid
and b.color='red' NOT IN(select s.sname from sailor s,boat b, reserve r where
s.sid=r.sid and r.bid=bid and b.color='red' and b.color='green');
c.mysql>select s.sid from sailor s where s.rating=10;

-------------------------------------------------------------------------------------------------
Q9)Consider the following database consisting of the following tables:
sailor(sid,sname,age,rating)
boat(bid,bname,color)
reserves(sid,bid,day)
a.find the name and age of the oldest sailors
b.count the number of different sailor name
c.find the age of the youngest sailor for each rating level
d.create procedure to display sid,sname,bid,bname when bid is given

Ans)
a.mysql>select s.name ,s.age from sailor s where s.age=(select max(s.age) from sailors s);
b.mysql>select count(distinct s.sname) from sailor s;
c.mysql>select min(s.age) from sailor s GROUPBY s.rating;
d.mysql>delimiter //
mysql>create procedure p5(in b integer)
begin
select s.sid,s.sname,b.bid,b.bname from sailors s,reserves r,boats b where s.sid=r.sid and
r.bid=b.bid and b.bid=b;
end//

-------------------------------------------------------------------------------------------------
Q10)Consider the following database consisting of the following tables:
sailor(sid,sname,age,rating)
boat(bid,bname,color)
reserves(sid,bid,day)
a.find the names of the sailor who are older than the oldest sailor rating 10
b.find the average age of sailors for each rating level that is greater than any sailor
c.find the min age of the sailor whose rating is lesser than all other sailors
d.create a procedure to display sid,sname from sailor when rating is given
Ans)
a.mysql>select s.sname,s.age from sailor s where s.age>(select max(s.age)from sailor s
where
s.rating=10 );
b.NOT SURE
c.mysql>select min(age) from sailor s where s.rating=(select min(s.rating) from sailor s);
d.mysql>delimiter //
create procedure p3(in rat integer)
begin
select sid,sname from sailors where rating=rat;
end//

You might also like