Assignment 2

download Assignment 2

of 3

description

aa

Transcript of Assignment 2

  • Assignment-2

    Question 1:

    You've started a new book rating website, and you've been collecting data on ratings of various books. Here's the schema:

    Book ( bID, title, year, author )There is a book with ID number bID, a title, a publication year, and an author.

    Person ( pID, name )The person who gives the rating with ID number pID has a certain name.

    Rating ( pID, bID, stars, ratingDate )The person pID gave the book bID a number of stars rating from 1 to 5 on a certain ratingDate.

    Write the SQL commands for the following queries:1. Find all years that have a book that received a rating of 4 or 5, and sort them in

    increasing order.2. Find the titles of all books that have no ratings and sort them in increasing order.3. For all cases where the same person rated the same book more than once and

    gave it a higher rating the second time, return the person's name and the title of thebook.

    4. Some authors wrote more than one book. For all such authors, return the titles of allbooks written by them, along with the author name. Sort by author name, then booktitle.

    5. For each author, return the author's name together with the title(s) of the book(s)they wrote that received the highest rating among all of their books, and the value ofthat rating. Ignore books whose author is NULL. Sort by author name, then booktitle.

    6. For each book that has at least one rating, find the highest number of stars thatbook received. Return the book title and number of stars. Sort by book title.

    7. Find the book(s) with the highest average rating. Return the book title(s) andaverage rating. Sort by book title if multiple are present.

    8. For each book, return the title and the 'spread of rating' which is defined as thedifference between highest and lowest ratings given to that book. Sort by ratingspread from highest to lowest, then by book title in ascending order. (The columnneeds to be named 'spread'.)

    9. Find the absolute difference between the average rating of books released before2010 and the average rating of books released after 2010. (Make sure to calculatethe average rating for each book, then the average of those averages for booksbefore 2010 and books after. Don't just calculate the overall average rating beforeand after 2010. Also, the average rating of books do not include 2010.)

    10.For every distinct pair of persons such that both persons gave a rating to the samebook, return the names of both persons. Eliminate duplicates, don't pair personswith themselves, and include each pair only once. For each pair, return the namesin the pair in alphabetical order.

  • Question 2:

    Chickoo has great interest in cricket. So,he came up with database of some cricketers. He found peer relationships and liking/admirations of cricketers for each others.Relational Schema of his database 'ChickooInfo' is as follows:

    Cricketer ( ChickooInfoID, name, ChickooInfoRating )

    Friend ( ChickooInfoID1, ChickooInfoID2 )Cricketer with ChickooInfoID1 is friend with ChickooInfoID2. Friendship is mutual.eg. If record(1, 2) is present then record(2, 1) will also be present.

    Likes ( ChickooInfoID1 , ChickooInfoID2 )Cricketer with ChickooInfoID1 likes/admires cricketer ChickooInfoID2. Liking is not mutual. In somesense ChickooInfoID2 is like an idol cricketer of ChickooInfoID1.

    Mahi is also a big cricket fan , and he asks Chickoo many queries regarding liking, friendships and ratings of different cricketers. Chickoo being busy person dont have enough time to lookup throughwhole database and he does not know mysql. So, he asks you to write sql commands for Mahi's following queries:

    1. Find the names of all cricketers who are friends with someone named Mathew.2. For every cricketer who likes/admires someone two or more rating lesser than themselves,

    return that cricketer's name and rating, and the name and rating of the cricketer they like.3. Find the name and rating of all cricketers who are liked by more than one other cricketer.4. Find the difference between the number of cricketers in the database and the number of

    different names.5. For every situation where cricketer A likes cricketer B, but we have no information about

    whom B likes, return A and B's names and ratings.6. Find all cricketers who do not appear in the Likes table (as a cricketer who likes or is liked)

    and return their names and ratings. Sort by rating, then by name within each rating range.7. For every pair of cricketers who both like each other, return the name and ratings of both

    cricketers. Include each pair only once, with the two names in alphabetical order.8. Find names and ratings of cricketers who only have friends having same rating. Return the

    result sorted by rating, then by name within each rating range.9. For each cricketer A who likes a cricketer B where the two are not friends, find if they have

    a friend C in common . For all such trios, return the name and rating of A, B, and C.

    ** Sample schema and data for both the questions will be uploaded on courses portal along with the expected output for each query.

    Upload Format :

    You should submit 2 sql files , one for each question containing only sql commands for queries asked in the given question.

    Only a single sql command should be written for each query. Each command should be on single line and should end with ; (semi-colon). If you are unable to come up with command for some query you should simply put ;

    for that command. You should maintain order of commands for queries as per given in the question. Create a folder with name Assignment2_ Put 2 sql files named _1.sql (for 1st question ) and _2.sql (for

    2nd question) in the folder. Compress the folder and upload Assignment2_.tar.gz.

  • Instructions : Any kind of plagiarism won't be tolerated. It will lead to Zero marks in all the

    assignments. Strictly follow the upload format . Failure to do so will fetch you zero marks. Deadline : 7 th October 2015 11:55 PM