You are on page 1of 2

--Lab Week 10

--Using the Movie2 DB undertake the following

--1) Create a view called v1Movie that will show a count


-- of DVDMovies and the average, maximum and minimum awards for each movie category

go

DROP VIEW V1Movie

go

CREATE VIEW V1Movie( Category, MovieCnt, CatAvgAwds,CatMaxAwds,CatMinAwds) AS


(SELECT Category,COUNT(*), AVG(Awards),MAX(Awards),MIN(Awards)
FROM DVDMovie
GROUP BY Category)

go

SELECT * FROM V1Movie

--2) Create a view called v2Movie which will have for each actor by actor id, a count of the
movies that they have appeared in
-- and the total number of nominations and awards those movie received

go

DROP VIEW V2Movie

go

CREATE VIEW V2Movie (ActorId, MvCnt, TotNoms, TotAwrds) AS


(SELECT ActorId, COUNT(*), SUM(Nomins), SUM(Awards)
FROM Casting C INNER JOIN DVDMovie D
ON C.DVDNo = D.DVDNo
GROUP BY ActorId)

go

SELECT * FROM V2Movie

--3) Create a view called V3Movie that will show a count of lead actor roles
-- for each actor, by actor id.
go

DROP VIEW V3Movie

go

CREATE VIEW V3Movie(ActorId, LeadCnt) AS


(SELECT LeadActorId, COUNT(*) FROM DVDMovie
GROUP BY LeadActorID)

go

SELECT * FROM V3Movie


--Then using these views write the following queries

--4) List the details for the actor that has had the highest number of lead roles

SELECT * FROM Actor WHERE ActorID IN


(SELECT ActorId FROM V3Movie
WHERE LeadCnt =
(SELECT MAX (LeadCnt) FROM V3Movie))

--5) List the actor details for the actor whose movies he has appeared in have the highest total of
awards
SELECT A.* FROM V2Movie V2 INNER JOIN Actor A
on A.ActorId = V2.ActorId
WHERE TotAwrds =
(SELECT MAX(TotAwrds) FROM V2Movie)

--6) List the movie details for any movie whose category has above the average number of
awards for that same category

SELECT D.* FROM V1Movie V1 INNER JOIN DVDMovie D


ON D.Category = V1.Category
WHERE Nomins > CatAvgAwds

--7) List the details of any member renting a movie in the category with the highest count of
movies. Show each member details only once.

SELECT Distinct M.* FROM Member M INNER JOIN DVDCopy DC


ON M.MemNo = DC.MemNo INNER JOIN DVDMovie DM
ON DC.DVDNo = DM.DVDNo
WHERE Category IN
(SELECT Category FROM V1Movie
WHERE MovieCnt = (SELECT MAX(MovieCnt)FROM V1Movie))

You might also like