Question

Need help writing SQL statements. The questions involved use the following database Student SID – Integer...

Need help writing SQL statements. The questions involved use the following database

Student

SID – Integer – Primary Key

SName - String

Address - String

Classes

ClassID – Integer – Primary Key

ClassName - String

Credits - Integer

Grades

SID – Integer – Primary Key

CID – Integer – Primary Key

Grade - Integer

Q1 – Write a script that creates a view to show all the students and all the classes they took and the grades they got in those classes. The view should create an output result set with the following columns

SName, ClassName, Grade.

The view should be sorted by student name.

Q2 – Update the view in Q3 to also include new students(The students who have NOT TAKEN any classes yet, and thus do not have a grade)

Q3 – Create a Stored procedure that accepts a student name and outputs the student's average grade(Average grade in all the classes the student took). (You can assume no 2 students have the same name).

If the student name is null then return a grade of -100

0 0
Add a comment Improve this question Transcribed image text
Answer #1


drop table student cascade constraints;
drop table class cascade constraints;
drop table gradeS cascade constraints;

CREATE TABLE STUDENT (SID INT PRIMARY KEY,
                       SNAME VARCHAR(20),
                       ADDRESS VARCHAR(20));
                      
INSERT INTO STUDENT VALUES(1,'John','1234 Ninth');
INSERT INTO STUDENT VALUES(2,'Joe','1234 Seventh');
INSERT INTO STUDENT VALUES(3,'Harry','1234 Second');
INSERT INTO STUDENT VALUES(4,'Peter','1234 Sixth');

CREATE TABLE CLASS(CLASSID INT PRIMARY KEY,
                   CLASSNAME VARCHAR(20),
                   CREDITS INTEGER);
                  
INSERT INTO CLASS VALUES(11,'DBMS',4);
INSERT INTO CLASS VALUES(12,'C',4);
INSERT INTO CLASS VALUES(13,'C++',4);
INSERT INTO CLASS VALUES(14,'Discrete Maths',3);

CREATE TABLE GRADES (SID INT,CID INT,GRADE INT,
                   FOREIGN KEY(SID) REFERENCES STUDENT(SID),
                   FOREIGN KEY(CID) REFERENCES CLASS(CLASSID));
                  
INSERT INTO GRADES VALUES(1,11,50);
INSERT INTO GRADES VALUES(2,13,90);
INSERT INTO GRADES VALUES(2,12,70);
INSERT INTO GRADES VALUES(3,11,58);

SELECT * FROM STUDENT;
SELECT * FROM CLASS;
SELECT * FROM GRades;

CREATE OR REPLACE VIEW SHOWALLSTUDENTS AS(SELECT SNAME,CLASSNAME,GRADE
                                           FROM STUDENT S,CLASS C, GRADES G
                                           WHERE S.SID=G.SID AND C.CLASSID=G.CID);
                                          
SELECT * FROM SHOWALLSTUDENTS;


CREATE OR REPLACE PROCEDURE STUDENTSGRADE(STUDENT_NAME VARCHAR)
AS
done Number :=1;
CURSOR C1 IS
SELECT AVG(GRADE) avggrade,SNAME,G.SID
FROM GRADES G,STUDENT S
WHERE G.SID=S.SID AND SNAME=STUDENT_NAME
GROUP BY (G.SID,SNAME);
BEGIN
FOR P IN C1
LOOP
   if (P.SNAME = STUDENT_NAME) THEN
       dbms_output.put_line('STUDENT'||' '||P.SNAME||' '||'EXISTS AND HIS AVERAGE GRADE IS '||P.avggrade);
       done:=0;
   else
   dbms_output.put_line('Student '||STUDENT_NAME||' HAS NOT TAKEN ANY CLASSES and thus do not have a grade');
   end if;
END LOOP;
if (done = 1 ) THEN
dbms_output.put_line('Student '||STUDENT_NAME||' HAS NOT TAKEN ANY CLASSES and thus do not have a grade');
end if;
end;
/

EXEC STUDENTSGRADE('Peter');
EXEC STUDENTSGRADE('Joe');

CREATE OR REPLACE PROCEDURE STUDENTS(STUDENT_NAME VARCHAR)
AS
done Number :=1;
CURSOR C1 IS
SELECT AVG(GRADE) avggrade,SNAME,G.SID
FROM GRADES G,STUDENT S
WHERE G.SID=S.SID AND SNAME=STUDENT_NAME
GROUP BY (G.SID,SNAME);
BEGIN
FOR P IN C1
LOOP
   if (P.SNAME = STUDENT_NAME) THEN
       dbms_output.put_line('STUDENT'||' '||P.SNAME||' '||'EXISTS AND HIS AVERAGE GRADE IS '||P.avggrade);
       done:=0;
   end if;
END LOOP;
if (done = 1 ) THEN
dbms_output.put_line('Student NAME IS null hence grade is -100');
end if;
end;
/

EXEC STUDENTS('Peter');
EXEC STUDENTS('Joe');

Add a comment
Know the answer?
Add Answer to:
Need help writing SQL statements. The questions involved use the following database Student SID – Integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • EXERCISE 1 (SQL Queries) Consider the following schema: SUPPLIERS (SID : integer, SNAME : string, CITY...

    EXERCISE 1 (SQL Queries) Consider the following schema: SUPPLIERS (SID : integer, SNAME : string, CITY : string) PARTS (PID : integer, PNAME : string, COLOR : string) CATALOG (SID : integer, PID : integer, COST : real) The key fields are underlined, and the domain of each field is listed after the field name. Thus, SID is the key for SUPPLIERS, PID is the key for PARTS, and SID and PID together form the key for CATALOG. The CATALOG...

  • 4. The exams database: The following database contains multiple choice exam questions tables Questions and On...

    4. The exams database: The following database contains multiple choice exam questions tables Questions and On Hons). Each question has a question Tert and each one of the options has and optid, which is an integer representing options A) (optid=1), B) (optid=2), C) (optid=3), etc. as well as the text of the option. The field correct is either Oor 1 and indicates which is the correct option for that particular question. courseThe exams have numbers 1, 2, 3, etc. Each...

  • Put all of your SQL code in a file named grades.sql and submit it below. Download...

    Put all of your SQL code in a file named grades.sql and submit it below. Download the starter code, which contains import_grades.sql and the grades.csv file. Using he import_grades, sql file, create your database and table. - 0 eded. 1 T Une Modify the LOAD DATA INFILE to correct the path to load the grades.csv file, and add/remove LOCAL he only modification you may make to the import_grades.sql or the grades.csv files. The data represents grades for assignments in a...

  • Someone please answer all of these. I need these badly. The submission date is knocking at...

    Someone please answer all of these. I need these badly. The submission date is knocking at the door. Experiment 1: SQL data definition and data insertion 46 hours) 1. CREATE TABLE. The database schema consists of the three relations, whose schemas are: S (Spa, Sname. Sgender, Sage, Sdert? // students(SID, name, gender, age, department) SC (Spa, Cne. Grade) //Course(SID, CID, grade) C (One Cname Crno. Ceredit) l/courses (CID, course name, prerequisite courses, credit) 2. DROP TABLE, ALTER TABLE, CREATE INDEX,...

  • Consider the following schema: SUPPLIERS (SID: integer, SNAME: string, STREET: string, CITY: string, ZIP: string) PARTS...

    Consider the following schema: SUPPLIERS (SID: integer, SNAME: string, STREET: string, CITY: string, ZIP: string) PARTS (PID: integer, PNAME: string, COLOR: string) CATALOG (SID: integer, PID: integer, COST: real) The primary key attributes are underlined, and the domain of each attribute is listed after the attribute name. Thus, SID is the primary key for SUPPLIERS, PID is the primary key for PARTS, and SID and PID together form the primary key for CATALOG. Attribute SID in CATALOG is a foreign...

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • Consider the below schema of the university database (keys are in bold and underline): Students(stuID: Integer,...

    Consider the below schema of the university database (keys are in bold and underline): Students(stuID: Integer, stuName: String, gender: String, age: Integer, gpa: Float) Departments(deptName: String, numPhDs: Integer) ProfessorWorks(profID: Integer, profName: String, deptName: String) CoursesOffer(cNo: String, cTitle: String, deptName: String) Majors(deptName:  String, stuID: Integer, degreeProgram: String; attendYear: String, attendSemester: String) Sections(cNo: String, academicYear: String, semester: String, sectNo: Integer, profID: Integer) Enrolls(stuID: Integer, cNo: String, academicYear: String, semester: String, sectNo: Integer, grade: String) Write the following queries in Relational algebra. Print...

  • Given the following relational schema, write queries in SQL to answer the English questions. The Access...

    Given the following relational schema, write queries in SQL to answer the English questions. The Access Database for the schema is available, as is a DDL file. It is also available on the MySQL server. You must only submit the SQL for your answers. You can get your answers without using a DBMS or by using Access or MySQL. Customer(cid: integer, cname: string, address: string, city: string, state: string) Product(pid: integer, pname: string, price: currency, inventory: integer) Shipment(sid: integer, cid:...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT