• MovieSupplier
  • Homework 3 cse444




    Download 26 Kb.
    Sana24.12.2019
    Hajmi26 Kb.
    #4779

    Homework 3

    CSE444
    This homework will give you practice writing SQL statements and using the query facility of SQL Server. The queries that you'll implement in SQL Server are based on a movies database. The schema for the movie database is as follows:
    Customers

    CustID

    LastName

    FirstName


    Inventory

    TapeID

    MovieID


    Movies

    MovieID

    MovieName


    MovieSupplier

    SupplierID

    MovieID

    Price


    Orders

    OrderID

    SupplierID

    MovieID

    Copies


    Rentals

    CustomerID

    TapeID

    CkoutDate

    Duration


    Suppliers

    SupplierID

    SupplierName

    Write out SQL statements for the following 10 queries about the movie database. Enter the SQL code in the ISQL_w facility of SQL Server (as talked about in class last Friday). Select the 'cse444sql' database in the DB combo box of the ISQL_w window, (that's the movie database described above despite the undescriptive name). Enter your query in the query tab and check your results in the result tab.


    To turn in: Please turn in a print out of your SQL queries and the number of tuples returned in the resulting tables for each one. (The tables themselves may be long, so don't bother printing those out.)
    Please note that these questions may be interpreted in different ways. Just state your interpretations of them if you feel there is any ambiguity.


    1. Which movies are supplied by "Joe's House of Video" or "Video Warehouse"?

    SELECT MovieName

    FROM Movies, MovieSupplier, Suppliers

    WHERE Suppliers.SupplierName = "Joe's House of Video" and Movies.MovieID = MovieSupplier.MovieID and Suppliers.SupplierID = MovieSupplier.SupplierID

    UNION

    SELECT MovieName



    FROM Movies, MovieSupplier, Suppliers

    WHERE Suppliers.SupplierName = "Video Warehouse" and Movies.MovieID = MovieSupplier.MovieID and Suppliers.SupplierID = MovieSupplier.SupplierID





    1. Which movie was rented for the longest duration (by any customer)?

    SELECT Movies.MovieName

    FROM Rentals, Movies, Inventory

    WHERE Movies.MovieID = Inventory.MovieID and Inventory.TapeID = Rentals.TapeID and Rentals.Duration >= ALL (SELECT Duration FROM Rentals)





    1. Which suppliers supply all the movies in the inventory? (Hint: first get a list of the movie suppliers and all the movies in the inventory using the cross product. Then find out which of these tuples are invalid.)

    SELECT Suppliers.SupplierName

    FROM Suppliers

    WHERE Supplier.SupplierID NOT IN

    (SELECT MovieSupplier.SupplierID,

    FROM MovieSupplier AS MS, Inventory AS I

    WHERE NOT EXISTS

    (SELECT *

    FROM Inventory, MovieSupplier

    WHERE MovieSupplier.MovieID = Inventory.MovieID and

    MovieSupplier.SupplierID = MS.SupplierID and

    Inventory.MovieID = I.MovieID) )





    1. How many movies in the inventory does each movie supplier supply? That is, for each movie supplier, calculate the number of movies it supplies that also happen to be movies in the inventory.

    SELECT Suppliers.SupplierName, COUNT( DISTINCT MovieID)

    FROM Suppliers, MovieSupplier, Movies

    WHERE Suppliers.SupplierID = MovieSupplier.SupplierID and MovieSupplier.MovieID = Movies.MovieID

    GROUP BY Suppliers.SupplierName



    1. For which movies have more than 4 copies been ordered?

    SELECT Movies.MovieName

    FROM Movies, Orders

    WHERE Orders.MovieID = Movies.MovieID

    GROUP BY Movies.MovieName

    HAVING SUM(Copies) > 4





    1. Which customers rented "Fatal Attraction 1987" or rented a movie supplied by "VWS Video"?

    SELECT LastName

    FROM Customers, Rentals, Inventory, Movies

    WHERE Customers.CustID = Rentals.CustID and Rentals.TapeID = Inventory.TapeID and Inventory.MovieID = Movies.MovieID and Movies.MovieName LIKE "%Fatal Attraction 1987%"

    UNION

    SELECT LastName



    FROM Customers, Rentals, Inventory, Movies, MovieSupplier, Suppliers

    WHERE Customers.CustID = Rentals.CustID and Rentals.TapeID = Inventory.TapeID and Inventory.MovieID = Movies.MovieID and

    Movies.MovieID = MovieSupplier.MovieID and MovieSupplier.SupplierID = Suppliers.SupplierID and Suppliers.SupplierName = "VWS Video"


    1. For which movies are there more than 1 copy in our inventory? (Note that the TapeID in inventory is different for different copies of the same MovieID)

    SELECT Movies.MovieName

    FROM Inventory AS I1, Inventory AS I2, Movies

    WHERE I1.MovieID = I2.MovieID and I1.TapeID <> I2.TapeID

    and I1.MovieID = Movies.MovieID


    1. Which customers rented movies for 5 days or more?

    SELECT DISTINCT LastName

    FROM Customers, Rentals

    WHERE Customers.CustID = Rentals.CustID and Duration >= 5




    1. Which supplier has the cheapest price for the movie "Almost Angels 1962"?

    SELECT Suppliers.SupplierName

    FROM Suppliers, MovieSuppliers, Movies

    WHERE Suppliers.SupplierName = MovieSuppliers.SupplierName and MovieSuppliers.MovieID = Movies.MovieID and Movies.MovieName LIKE "% Almost Angels 1962%"

    and price <= ALL

    (SELECT price

    FROM MovieSuppliers, Movies

    WHERE Movies.MovieID = MovieSupplier.MovieID and

    Movies.MovieName LIKE "% Almost Angels 1962%")



    1. Which movies aren't in the inventory?

    SELECT Movies.MovieName

    FROM Movies

    WHERE MovieID NOT IN



    (SELECT MovieID

    FROM Inventory)
    Download 26 Kb.




    Download 26 Kb.