Assignment 2: by Mrunal Patekar
Assignment 2: 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'
view 2.)
create view customer_details as
select cid , cname, caddress
from customer
a.)
select caddress
from customer_details
where cname = 'John Smith';
(c) insert into customer_details values(18123 , 'Bob Jones' , '23 Court Street, Brooklyn');
(3)(a)
(3)(b)
(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: