Question

In: Computer Science

Write the following queries using relational algebra. SHOW ALL STEPS CREATE TABLE MovieStar (msid char(3), msname...

Write the following queries using relational algebra. SHOW ALL STEPS

CREATE TABLE MovieStar (msid char(3), msname char(10), title char(4), PRIMARY KEY(msid)); msid – movie star ID, msname – star’s name, title – denotes gender (“Mr.’, “Mrs.”, “Ms.”)

CREATE TABLE Stars_in (msid char(3), mid char(3), contract number(9,2), duration number(2), PRIMARY KEY (msid, mid), FOREIGN KEY(msid) REFERENCES MovieStar, FOREIGN KEY(mid) REFERENCES Movie); contract – the star’s salary for the movie, duration – contract duration in months.

CREATE TABLE Movie (mid char(3), mname char(10), year number(4), length number(4,2), PRIMARY KEY (mid)); mid – movie ID, mname – movie title, year – the year when the movie was created, length – movie duration in hours.

1. Find the names of male stars who had contracts for less than 3 months.

2. Find titles of movies with the stars’ contracts over $1,000,000.00.

3. Find stars’ names who starred in movies with duration over 2 hours.

4. Find stars’ names who starred in at least one movie.

5. Find stars’ names, who starred in at least two movies.

6. Find the stars’ names who starred in movies created in 2005 and 2015.

Please elaborate on the answer.

Solutions

Expert Solution

A sample DB for given schema:

  1. SELECT msname FROM MovieStar WHERE title LIKE 'Mr.' AND msid in (SELECT msid FROM Stars_in WHERE duration < 3)

    Explanation: The inner query finds all stars ids which have starred in some movie with contract duration < 3, then in outer query, WHERE condition, we ensure that title is Mr. and msid belongs to the msids selected above.
    The output of the query for given db:

  2. SELECT mname FROM Movie WHERE mid in (SELECT mid FROM Stars_in WHERE contract > 1000000.00)

    Explanation: The inner query finds all movie ids which have given a contract > 1000000, then in outer query, WHERE condition, we ensure that mid comes from this set of ids
    The output of the query for given db:

  3. SELECT msname FROM MovieStar ms JOIN Stars_in si ON ms.msid = si.msid JOIN Movie mv ON mv.mid = si.mid WHERE length > 2

    Here, we join tables MoivieStar, Stars_in on basis of the star id, then join this combined table with Movie on basis of movie id. This gives us a table with info about movie, star, and movie_star combined so now just select star which have movie length > 2 hours
    Sample output:

  4. SELECT msname FROM MovieStar WHERE msid in (SELECT msid FROM Stars_in)

    Here, we firstly find the stars id present in Stars_in , it gives us ids of all the stars which have starred in some movie. Now just select names of those stars from outer query.
    Sample output:

  5. SELECT msname FROM MovieStar WHERE msid in (SELECT msid FROM Stars_in GROUP BY msid HAVING count(msid) >= 2)

    In the inner query, we find all stars' id which have starred in more than 2 movies, by combining all rows by movie star id (group by) and checking count. After that in outer query we get names from those ids.
    Sample output:

  6. SELECT msname FROM MovieStar ms JOIN Stars_in si ON ms.msid = si.msid JOIN Movie mv ON mv.mid = si.mid WHERE mv.year BETWEEN 2005 AND 2015

    Similar to 3rd, we combine all three tables and pick up stars names where movie year was between 2005 and 2015
    Sample Output:


Related Solutions

Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it...
Write the following questions as queries in Relational Algebra. Use only the operators discussed in class...
Write the following questions as queries in Relational Algebra. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming —in particular, no outer joins or aggregations). Type your answers. If you can’t find Greek letters in your word processor, you can simply write out the operator, all uppercase (i.e. ’SELECT’). Please use renaming consistently, as indicated in the handout. Before starting, make sure you understand the schema of the database. If you...
) Write queries for the following. Include screenshots of the queries and the outputs. Create a...
) Write queries for the following. Include screenshots of the queries and the outputs. Create a procedure named DisplayInfo which takes customer name as a parameter and displays information of that customer. (database – sql_store)
Given the following relational schema, write queries in SQL to answer the English questions. There is...
Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...
Given the following relational schema, write queries in SQL to answer the English questions. There is...
Given the following relational schema, write queries in SQL to answer the English questions. There is a shipment database on the MySQL server. You can also use the DDL for MySQL. You must only submit the SQL for your answers but you can include the query output as well to help the TA with marking. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid: integer, shipdate: Date/Time) ShippedProduct(sid:...
This is t a relational database please write SQL queries to solve the listed questions. The...
This is t a relational database please write SQL queries to solve the listed questions. The database is a variation of the “Movie Database” . There are several differences in it, so look it over carefully before writing your SQL queries Notes: TheaterNum, MovieNum, and ActorNum are numeric primary key fields in their respective tables. Movie and actor names are not assumed to be unique unless specified otherwise in a question. In the THEATER table, Capacity is the number of...
Write the following SQL queries and show the corresponding output of the DBMS: 1) Write an...
Write the following SQL queries and show the corresponding output of the DBMS: 1) Write an SQL statement to display all the information of all Nobel Laureate winners. 2) Write an SQL statement to display the string "Hello, World!". 3) Write an SQL query to display the result of the following expression: 2 * 14 +76. 4) Write an SQL statement to display the winner and category of all Laureate winners. 5) Write an SQL query to find the winner(s)...
Multiply two binary numbers1101 and 1101 using a serial multiplier. Show all steps in a table.
Multiply two binary numbers1101 and 1101 using a serial multiplier. Show all steps in a table.
Create an ANOVA and regresion problem, show all steps and result.
Create an ANOVA and regresion problem, show all steps and result.
Use a single SQL statement to create a relational table and to load into the table...
Use a single SQL statement to create a relational table and to load into the table department name, subject code, year of running and session of running that offered by the departments. Note that a running subject offered by a department means a lecturer of the department has been assigned to teach the subject. Next, enforce the appropriate consistency constraints on the new table.    When ready use SELECT statement to list the contents of the relational table created and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT