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

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...
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
***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...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
This lab uses a Student class with the following fields: private final String firstName; private final...
This lab uses a Student class with the following fields: private final String firstName; private final String lastName; private final String major; private final int zipcode; private final String studentID; private final double gpa; A TestData class has been provided that contains a createStudents() method that returns an array of populated Student objects. Assignmen The Main method prints the list of Students sorted by last name. It uses the Arrays.sort() method and an anonymous Comparator object to sort the array...
Write the following questions as queries in SQL. Use only the operators discussed in class (no...
Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins) Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT