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 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)
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...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a select statement to show the invoicelineitemdescriptions that have the total invoicelineitemamount >1000 and the number of accountno is >2. 10. Write a select statement that returns the vendorid, paymentsum of each vendor, and the number of invoices of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number...
Based on this code: CREATE TABLE DEPARTMENT ( DepartmentName Char(35) NOT NULL, BudgetCode Char(30) NOT NULL,...
Based on this code: CREATE TABLE DEPARTMENT ( DepartmentName Char(35) NOT NULL, BudgetCode Char(30) NOT NULL, OfficeNumber Char(15) Not Null, DepartmentPhone Char(12) NOT NULL, CONSTRAINT DEPARTMENT_PK primary key(DepartmentName) ); CREATE TABLE EMPLOYEE( EmployeeNumber Int NOT NULL AUTO_INCREMENT, FirstName Char(25) NOT NULL, LastName Char(25) NOT NULL, Department Char(35) NOT NULL DEFAULT 'Human Resources', Position Char(35) NULL, Supervisor Int NULL, OfficePhone Char(12) NULL, EmailAddress VarChar(100) NOT NULL UNIQUE, CONSTRAINT EMPLOYEE_PK PRIMARY KEY(EmployeeNumber), CONSTRAINT EMP_DEPART_FK FOREIGN KEY(Department) REFERENCES DEPARTMENT(DepartmentName) ON UPDATE CASCADE, CONSTRAINT...
Write SQL queries below for each of the following: List the names and cities of all...
Write SQL queries below for each of the following: List the names and cities of all customers List the different states the vendors come from (unique values only, no duplicates) Find the number of customers in California List product names and category descriptions for all products supplied by vendor Proformance List names of all employees who have sold to customer Rachel Patterson
Question 1: Part 1 Write SQL statements for the following queries from the ‘EMPLOYEE’ table in...
Question 1: Part 1 Write SQL statements for the following queries from the ‘EMPLOYEE’ table in the WPC Database in MySQL: Display all records from the Employee table for employees working in the “Marketing” department. Display all records from the Employee table for employees working in the “Marketing” department OR “Finance” Department. Display the Last Names of all employees such that each last name appears only once. Display all the attributes for employees whose employee number is less than 10....
Write SQL queries for the following statements based on Employees table whose schema is given below:...
Write SQL queries for the following statements based on Employees table whose schema is given below: (Employee_ID,First_Name,Last_Name,Email,Phone_Number,Hire_Date,Job_ID,Salary, Manager_Id, Department_Id) a. Create the given table along with the following constraints: Phone_Number should not be left undefined, salary should be between 10000 and 20000, employee_id should uniquely identify rows and should not be left undefined. User-defined names should be given to constraints. b.  Display each employee’s last name, hire date and salary review date (which is the date after six months of service)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT