Question

In: Computer Science

Write the following queries using the schema below Class (Number, Department, Term, Title) Student (Username, FirstName,...

Write the following queries using the schema below

Class (Number, Department, Term, Title)

Student (Username, FirstName, LastName, Year)

Takes (Username, Department, Number, Term, Grade)

[clarification] In Student, Year is an integer. A student can be a 2nd year student. In that case, Year value would be 2.

For the Takes relation:

·         Grade is NA for current semester students

·         Username is foreign key to Student

·         (Department, Number, Term) is foreign key to Class

a)       Write an SQL query that returns the Term when students with username “alex” and “bob” take at least 1 class together. Include the number of class that they have taken together in that Term.

b)      Write an SQL query that returns the usernames of the students who take exactly two classes in the “CSE” department in the term “Fall.2020”

c)       Write an SQL query that returns the title of all classes which have not been taken by any first-year student.

Solutions

Expert Solution

In this question there are 3 tables.

  • CLASS having the entities number, department, term and title.
  • STUDENT having the entities Username, FirstName, LastName, Year.
  • TAKES having the entities Username, Department Number, Term, Grade.

Number is the primary key of CLASS table.

Username is a primary key of STUDENT table.

Username is the primary key of TAKES table and the foreign key of TAKES table are Department, Number, Term.

Ans.a)

Select Term, Sum(Number of Class together by Bob and Alex) from(Select Username,Count(Number) AS "Number of Class together by Bob and Alex" from TAKES where Username="Alex" AND Username="Bob" AND Number>=1 GROUP BY Username);

To fetch the term which are having student name Alex and Bob that are having at least one number of class. So to fetch this data three conditions are applied in which username is Alex , username is Bob and Number>=1. So that when all three conditions are satisfied then it retrieve the term.

Another in this query is to include the total number of classes that has to be taken together in that particular first term so to fetch that particular term in which Bob and Alex have at least one class together then count function is used to count the total number of classes by both these username and then sum is depicted in a column name "Number of Class together by Bob and Alex", for this sub selective query has to be written.

Ans.b)

Select Username from TAKES where Number=2 AND Department="CSE" AND Term="Fall.2020";

To fetch the data of the username who have number of classes exactly two and having the department CSE and the term name "Fall.2020". In this only and operator are used in order to satisfy all the three conditions that class number is equals to two, department equals to CSE and term is equals to fall.2020.

By simply using AND operator username are fetched.

Ans.c)

Select Title from CLASS NATURAL JOIN TAKES NATURAL JOIN STUDENT where Year!=1;

To fetch the title of those classes where student year is not equals to 2.

For this information, first of all JOIN clause is used in order to join all three tables by their respective primary key and foreign key.

First CLASS table is join with TAKES table with Number as a foreign key and then TAKES table join with STUDENT table by the username as foreign key where year of student table is not equals to 1.


Related Solutions

Write the following queries using the schema below Class (Number, Department, Term, Title) Student (Username, FirstName,...
Write the following queries using the schema below Class (Number, Department, Term, Title) Student (Username, FirstName, LastName, Year) Takes (Username, Department, Number, Term, Grade) [clarification] In Student, Year is an integer. A student can be a 2nd year student. In that case, Year value would be 2. For the Takes relation: ·         Grade is NA for current semester students ·         Username is foreign key to Student ·         (Department, Number, Term) is foreign key to Class a)       Write an SQL query that returns the Term...
C++: Write a student class for the library, which have the following: Username, password, Maximum number...
C++: Write a student class for the library, which have the following: Username, password, Maximum number of copies that a student is allowed to keep(less than 5), maximum borrow periods(less than 30 days per book), list of copy(array) cpp and h
7. Using the provided schema of a Purchase Order Administration database, write the following queries in...
7. Using the provided schema of a Purchase Order Administration database, write the following queries in SQL. (In the schema, bold attributes are primary keys and italicized attributes are foreign keys.) SUPPLIER (SUPNR, SUPNAME, SUPADDRESS, SUPCITY, SUPSTATUS) SUPPLIES (SUPNR, PRODNR, PURCHASE_PRICE, DELIV_PERIOD) PRODUCT (PRODNR, PRODNAME, PRODTYPE, AVAILABLE_QUANTITY) PO_LINE (PONR, PRODNR, QUANTITY) PURCHASE_ORDER (PONR, PODATE, SUPNR) 7d) Write a nested SQL query to retrieve the supplier number, supplier name, and supplier status of each supplier who has a higher supplier status...
I am having trouble writing these queries in MYSQL. Using the schema listed below, please write...
I am having trouble writing these queries in MYSQL. Using the schema listed below, please write the following queries in MYSQL: 1) Find the Content and the Reviewer Name for each comment, about “ACADEMY DINOSAUR” only if the same reviewer has commented about “ACE GOLDFINGER” too. 2) Retrieve the title of all the Movies in Japanese without any comment, ordered alphabetically. 3) Find all the movie titles where an actor called “TOM” or an actor called “BEN" acted, where there...
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)....
Basic SQL Use the following schema to answer the queries below using SQL DEVICE [dno, dtype,...
Basic SQL Use the following schema to answer the queries below using SQL DEVICE [dno, dtype, price] PROVIDER [pno, pname, web] SERVICE [dno, pno, servicedate] SERVICE.dno references DEVICE.dno SERVICE.pno references PROVIDER.pno bold is underline. a) Find the dno for the most expensive device b) Find all providers that have the work fast in the name c) Find the number of different device types (dtype) d) Give all details of devices with price more than $400
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:...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The program should allow creation an array of object instances to assign firstName, lastName and mark from input user and perform and assign grade based on mark’s criteria displayed below. MARKS INTERVAL 95 - 100 90 - <95 85 - <90 80 - <85 75 - <80 70 - <75 65 - <70 60 - <65 0 - <60 LETTER GRADE A+ A B+ B...
Database Schema: Book(bookID, ISBN, title, author, publish-year, category) Member(memberID, lastname, firstname, address, phone-number, limit) CurrentLoan(memberID, bookID,...
Database Schema: Book(bookID, ISBN, title, author, publish-year, category) Member(memberID, lastname, firstname, address, phone-number, limit) CurrentLoan(memberID, bookID, loan-date, due-date) History(memberID, bookID, loan-date, return-date) Members can borrow books from the library. The number of books they can borrow is limited by the “limit” field of the Member relation (it may differ for different members). The category of a book includes fiction, non-fiction, children’s and reference. The CurrentLoan table represents the information about books that are currently checked out. When the book is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT