0% found this document useful (0 votes)
11 views

Midterm Test Even

_Midterm Test Even

Uploaded by

TWICE EDIT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Midterm Test Even

_Midterm Test Even

Uploaded by

TWICE EDIT
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Midterm test (Even) – Duration 60 minutes

For each question, copy both SQL code and capture query output in images and paste image
after each question here

1. Create the following table based on ERDiagram (2 pts)

Note the primary key and foreign key constraint in your SQL Code
CREATE TABLE departments(
departmentID INT PRIMARY KEY,
departmentName VARCHAR(50) NOT NULL);

CREATE TABLE Professors(


ProfessorID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES departments(DepartmentID));
CREATE TABLE Courses(
CoursesID INT PRIMARY KEY,
CourseName VARCHAR(50),
ProfessorID INT,
FOREIGN KEY(ProfessorID) REFERENCES professors(ProfessorID));

2. Insert the following data to database (2 pts)


 Department Table
DepartmentID DepartmentName
1 Computer Science
2 Mathematics
3 Physics
INSERT INTO departments( DepartmentID, DepartmentName) VALUES
(1, 'Computer Science'),
(2, 'Mathematics'),
(3, 'Physics');

 Professors Table:
ProfessorID FirstNam LastName DepartmentID
e
1 Alice Johnson 1
2 Bob Smith 2
3 Charlie Brown 3

INSERT INTO professors( ProfessorID, FirstName, LastName, DepartmentID) VALUES


(1, 'Alice', 'Johnson', 1),
(2, 'Bob', 'Smith', 2),
(3, 'Charlie', 'Brown', 3);

 Courses Table
CourseID CourseName ProfessorID
1 Algorithms 1
2 Calculus 2
3 Quantum 3
Physics

INSERT INTO courses( coursesID, courseName, professorID) VALUES


(1, 'Algorithms', 1),
(2, 'Calculus', 2),
(3, 'Quantum', 3);
3. Write SQL Query to handle this situation (3 pts)
 Write SQL query to retrieve list of professors (Total 3 records)
SELECT * FROM `professors`

 Write an SQL query to get the list of all courses and the professors teaching them. Include a
condition to only retrieve courses in the 'Computer Science' department. Return columns
CourseID, CourseName, ProfessorFirstName, ProfessorLastName. (Total 1 record)

SELECT CoursesID, CourseName, FirstName, LastName FROM professors


INNER JOIN courses ON professors.ProfessorID = courses.ProfessorID
WHERE professors.ProfessorID = 1;
 Write an SQL query to get the list of all professors and the courses they are teaching.
Include a condition to only retrieve professors who name start with letter “A”. Return
columns ProfessorID, FirstName, LastName, CourseName. (Total 1 record)

SELECT professors.ProfessorID, FirstName, LastName, CourseName FROM professors


INNER JOIN courses ON professors.ProfessorID = courses.ProfessorID
WHERE professors.FirstName LIKE '%A%';
 Write an SQL query to get the number of courses in each department. Return
DepartmentName and CourseCount. (Total 3 records)

SELECT COUNT(courses.CoursesID) AS CourseCount, DepartmentName FROM courses


INNER JOIN departments ON courses.CoursesID = departments.departmentID
GROUP BY courses.CoursesID;
4. Write SQL Stored Procedure to handle this situation (1.5 pts)

Create a new table called CourseEnrollments which has the following attributes. EnrollmentID is
primary key and automatically increased.

CREATE TABLE CourseEnrollments (


EnrollmentID INT PRIMARY KEY AUTO_INCREMENT,
CoursesID INT,
ProfessorID INT,
EnrollmentDate DATE);

Insert dummy data of records of your choice into newly-created table, at least 4 records. You
can do this via user interface.
Write stored procedure to query which professor taught which course. Return table contains
Professor first name, Professor last name and CourseName. The ProfessorId will be input by
user as input parameter.

5. Write SQL Trigger to handle this situation (1.5 pts)

Create table EnrollmentLogs which has the following attributes. LogID is primary key and
automatically increased

CREATE TABLE EnrollmentLogs (


LogID INT PRIMARY KEY AUTO_INCREMENT,
EnrollmentID INT NOT NULL,
Action VARCHAR(50) NOT NULL,
ActionDate TIMESTAMP);

Create a trigger that logs the changes to EnrollmentLogs to log actions related to
CourseEnrollments. Whenever a new course enrolment record is created, another record is
inserted to EnrollmentLogs. The Action of EnrollmentLogs will accept “INSERT” only since we
need to care Insert action on CourseEnrollments.

You might also like