I have been struggling for hours with trying to figure out how I can return the average GPA across all sections that a professor teaches. Here is the code that I have so far.
-- Unit 12 Step 4
-- Create a Stored Procedure named Faculty_GPA to find the AverageGPA of students across all
-- the sections that the professor teaches.
USE COLLEGE;
DROP PROCEDURE IF EXISTS Faculty_GPA ;
DELIMITER $$
CREATE PROCEDURE Faculty_GPA(IN FacultyID INT, OUT outavgGPA decimal(4, 2))
BEGIN
DECLARE theGPAInfo DECIMAL(4,2);
SET theGPAInfo=
(SELECT AVG(registration.Grade)
FROM
Registration
INNER JOIN
Student ON registration.ID= Student.ID
INNER JOIN
Section ON Student.ID= Section.ID
WHERE section.ID= 2 AND section.ID= 3);
SET outavgGPA= theGPAInfo;
END
$$
DELIMITER ;
CALL Faculty_GPA(2, @averageGPA);
SELECT @averageGPA as GPA;
--
@r937,
No, I mind return the average GPA for all the sections a professor( who has a Faculty ID of 2) teaches students in. I know if I run this select statement below it will tell me RegistrationID, StudentID, SectionID, Registration.Grade, but I am just not sure how I can run a query to output the averageGPA of the sections the professor teaches students in order to help solve my bigger problem of working with the procedure I created above. I always try to see what information is stored in each table.
Here is the joining of the two tables named Section and Registration, but I still have trouble in my query with trying to match the sections that the professor taught with what students registered for his sections. How could I do that? The code and screenshot of the result grid of what I have so far. Here you can see that the professor taught sections 2 and 3 because it corresponds to his ID, but what you don’t see is the students’ registered sections corresponding to the sections he taught. I want to know all the students he taught in his sections and what grade they received. Here is code now to achieve the desired output you see. Here is SQL text code below that.
USE COLLEGE;
SELECT
Section.ID AS 'Section ID',
Section.Name AS 'Section Name',
Section.TaughtByID AS 'Section Taught By',
Registration.StudentID AS 'Student ID',
Registration.SectionID AS 'Student Registered for this section',
Registration.Grade AS 'Grade'
FROM
Section
INNER JOIN
Registration ON Section.ID= Registration.ID
WHERE Section.TaughtByID= 2;
How can I join on the right columns? I know I would need to show the section IDs in which he taught students in and all the students that were in those same sections, but I am just not understanding as you probably can tell from the work I displayed. Can you give me a hint as to what specific columns should be in this query?
I figured out why the query was wrong, but I am still not understanding how to return the AVG GPA across all sections. Here is the code I have fixed. I know it’s still not right but I am trying. How can I use the IN parameter of FacultyID inside of my SELECT statement?
-- Create a Stored Procedure named Faculty_GPA to find the AverageGPA of students across all
-- sections that the professor teaches.
USE COLLEGE;
DROP PROCEDURE IF EXISTS Faculty_GPA ;
DELIMITER $$
CREATE PROCEDURE Faculty_GPA(IN FacultyID INT, OUT outavgGPA decimal(4, 2))
BEGIN
DECLARE theGPAInfo DECIMAL(4,2);
SET theGPAInfo=
(SELECT AVG(registration.Grade)
FROM
Registration
INNER JOIN
Section ON Registration.SectionID= Section.ID
WHERE registration.Grade > 0);
SET outavgGPA= theGPAInfo;
END $$
DELIMITER ;
CALL Faculty_GPA(2, @averageGPA);
SELECT @averageGPA as GPA;
@r937,
The code that I have does return the AVG gpa but I don’t think it’s the right avg GPA for sections he taught. What I am missing or not doing right? How can I specify in my select statement only the sections he taught students in order to get avg gpa? Basically if I get this to work I can just copy this SQL query into the stored procedure, right? Here is the code:
USE COLLEGE;
SELECT
AVG(Registration.Grade)
FROM
Registration
INNER JOIN
Section ON Registration.SectionID= Section.ID
WHERE Registration.Grade > 0;