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

By Mrunal Patekar

Assignment 2
(1)a
The screen shots of the tables are:
Customer Table:

Movie Table:

Branch Table:

Rented Table:

Copy Table:

(1)b
(1)
select distinct cu.cname from customer cu,movie m , copy cp, rented rt
where cu.cid=rt.cid and m.mid=cp.mid and cp.copyid=rt.copyid
and cu.cname in (select distinct cu.cid from customer cu,movie m , copy cp, rented rt
where cu.cid=rt.cid and m.mid=cp.mid and cp.copyid=rt.copyid
and m.title='Terminator1'
)
and cu.cname in (select distinct cu.cid from customer cu,movie m , copy cp, rented rt
where cu.cid=rt.cid and m.mid=cp.mid and cp.copyid=rt.copyid
and m.title='Terminator2'
)
and cu.cname not in (select distinct cu.cid from customer cu,movie m , copy cp, rented rt
where cu.cid=rt.cid and m.mid=cp.mid and cp.copyid=rt.copyid
and m.title='Terminator3')

(2)
select c.cname,c.cid ,movie.title, movie.mid
from
customer c join rented r on c.cid = r.cid
join copy on copy.copyid = r.copyid
join movie on movie.mid = copy.mid
group by c.cname, movie.mid having count(movie.mid)>2;
(3)
Select distinct( c.cname)
from customer c join rented
on c.cid = rented.cid
join copy on rented.copyid = copy.copyid
join movie on
copy.mid = movie.mid
group by rented.cid , copy.mid having count(distinct copy.bid)=2;
(4)
select movie.title, count(copy.mid)
from movie join copy on movie.mid = copy.mid
join rented on copy.copyid = rented.copyid
group by movie.title having count(copy.mid)>4;
(5)
select z.title from
(select movie.mid,movie.title,count(distinct branch.bid) as no_of_branches
from customer ,movie , copy , rented , branch
where customer.cid=rented.cid and movie.mid=copy.mid and copy.copyid=rented.copyid and branch.bid=copy.bid
group by movie.mid ,movie.title
) as z
where z.no_of_branches >= (select count(bid)
from branch);
(6)
select branch.bname from
rented join copy on rented.copyid = copy.copyid
join branch on copy.bid = branch.bid
having sum(cost)>= all
(select sum(cost) as total_cost from
rented join copy on rented.copyid = copy.copyid
join branch on copy.bid = branch.bid
where year (rented.outdate='2009')
group by branch.bid);
(7)
select movie.title, movie.mid from rented
join copy on rented.copyid = copy.copyid
join movie on copy.mid = movie.mid
where rented.returndate is NULL
group by movie.mid having count(distinct copy.copyid)

in
(select count(distinct copy.copyid) from rented
join copy on copy.copyid = rented.copyid
join movie on copy.mid = movie.mid
group by movie.mid)
(c) UPDATE
(1.)
update customer c
set c.balance = balance + 5
WHERE cid IN
( SELECT DISTINCT cid
FROM rented r
WHERE cid IN
( SELECT cid
FROM rented
WHERE outdate = ('20140920' - '20141011')group by rented.cid
having count(rented.copyid > 5) ))
(2)
Update rented join customer on rented.cid = customer.cid set
Rented.returndate = curdate() , customer.balance = ((datediff(curdate(),outdate)-1) + 3) where
Rented.copyid = '76235' and customer.cid='15674'
(2) VIEWS:
View ) (a)
create view analyst1 as
select rented.copyid, branch.bid,branch.baddress ,movie.title,count(movie.mid) as no_of_movies
,count(movie.mid)*rented.cost as income_per_movie
from movie join copy on movie.mid = copy.mid
join rented on copy.copyid = rented.copyid
join branch on branch.bid = copy.bid
where rented.outdate like '2009%'
group by branch.bid having count(movie.mid);

View (a)1
select no_of_movies, title from analyst1
where title = 'Terminator2' and baddress = 'Brooklyn Heights'

(2) select baddress , max(income_per_movie) as max_income from analyst1;

view 2.)
create view customer_details as
select cid , cname, caddress
from customer
a.)
select caddress
from customer_details
where cname = 'John Smith';

(b) select customer.balance from customer , customer_details


Where customer.cid = customer_details.cid

And customer_details.cname = John Smith

(c) insert into customer_details values(18123 , 'Bob Jones' , '23 Court Street, Brooklyn');

The following is the output of the above query:

(d) RELATIONAL CALCULUS of the last 4 queries in (b)

(3)(a)

(3)(b)

(3) (c) Queries:


(1) select distinct c1.source, c2.destination from train as c1 , train as c2
where
c2.source='metrosquar ' and c1.destination = 'lois lane' group by c2.destination;
(2)
select source, destination,travel_dist,cost from train_trip
where source ='metrosquar' and destination = 'broadway'
Output of query:

(3) select count(metaid) ,sid from swipe


where (swipe_in - swipe_out) = '2014-06-02 08:00:00' - '2014-06-02 10:00:00'
group by sid having count(metaid);

(4) select metaid from


swipe, schedule
where swipe_in = (' 06:00:00 - 10:00:00');

(3)(d)
The following are the screen shots of the tables which are been used in the Metro Transit Authority(META)
Station Table:

Train Table:

Schedule Table:

Metacard Table:

Train_trip Table:

Swipe Table:

You might also like