You are on page 1of 11

CST363 Database Exam 1 Fall 2019(a)

Name Miguel Solis

Directions:

You may use your textbook, notes, course assignments, computers and internet resources.
Submit your file as a word DOCX or PDF file. Check iLearn course page for deadline date and
time.
You do not have a time limit for this exam.
Do not get help from other people or give help to other students. Sign the honor pledge
below.

I did not give or receive any unauthorized help in doing this exam. The exam is my own work.

Miguel Solis
Sign or type your name above.

The exam is 150 points out of a course total of 1000 points. So 15% of your final grade.

Each of the 20 questions is worth 7.5 points

The file exam1_schema_data.sql can be used to create the tables and load sample data.

Page 1 of 11
CST363 Database Exam 1 Fall 2019(a)

The following questions use the tables shown below.

Table: Student

StudentID LastName FirstName GPA Major TotalCredits

Table: Enrollment

StudentID CourseId Semester Grade Units

The format of Semester value is a string value such as 'Fall2016' with the name of the semester ‘Fall’
or ‘Spring’ and YYYY for year.

Table: Course

CourseId Name Section Department Instructor

Write a SQL select that can answer the question.

1. Report on all the students (LastName, FirstName) who are majoring in Biology and have a GPA
between 3.0 and 3.5 The result should be sorted by LastName.

SELECT lastname, firstname


FROM student
WHERE major = "Biology" AND gpa BETWEEN 3.0 AND 3.5
ORDER BY lastname;

2. Summarize the student data by major. For each major show the number of students with that
major and the average GPA of students by major. Change the column titles of the result set to
be “numberOfStudents” and “averageGPA”.

SELECT major, COUNT(major) AS "numberOfStudents", ROUND(AVG(gpa), 2) AS "averageGPA"


FROM student
GROUP BY major;

Page 2 of 11
CST363 Database Exam 1 Fall 2019(a)

3. List all students (LastName, FirstName) took “Discrete Math” with a grade of “B” or “A”.

SELECT s.lastname, s.firstname


FROM student s JOIN enrollment e
ON s.studentid = e.studentid JOIN course c
ON e.courseid = c.courseid
WHERE c.name = "Discrete Math" AND e.grade IN ("A", "B");

4. For each course, show CourseId, Name and count of enrolled students. If a course has no
enrollments, a count of 0 should appear. The result set should be CourseId order.

SELECT c.courseid, c.name, COUNT(e.courseid) AS enrollmentCount


FROM course c LEFT JOIN enrollment e ON c.courseid = e.courseid
GROUP BY c.courseid
ORDER BY c.courseid;

Page 3 of 11
CST363 Database Exam 1 Fall 2019(a)

5. Find students enrolled in course ’Calculus 2’ in semester 'Spring2017' who have taken and
passed with a grade of C or higher the required prerequisite course ’Calculus 1’.

SELECT s.lastname, s.firstname


FROM student s JOIN enrollment e
ON s.studentid = e.studentid JOIN course c
ON e.courseid = c.courseid
WHERE s.studentid IN (
SELECT e2.studentid
FROM enrollment e2 JOIN course c2
ON e2.courseid = c2.courseid
WHERE c2.name = "Calculus 1" AND e2.grade IN ("A", "B", "C")) AND
c.name = "Calculus 2" AND e.semester = "Spring2017";

Page 4 of 11
CST363 Database Exam 1 Fall 2019(a)

6. There are 3 professors who teach Calculus 1: Smith, Lupin and Fenwick. We want to compare
the average Calculus 2 grade of students based on their teacher in Calculus 1. If a student took
Calculus 1 but did not take Calculus 2 don’t count them. For example; the output below would
show that 10 of Smith’s calculus 1 students continued on to calculus 2 and their average grade
in calculus 2 is 2.8. Fenwick had 25 students in calculus 1 and none of them went on to calculus
2. Lupin had 30 students in his calc1 class continue on to calc2 where they are received a 3.5
average grade. The data shown below is an example. Your results may be different from what
is shown below.

InstructorCaculus1 NumberTakingCalculus2 AverageGradeCalculus2


Smith 10 2.8
Lupin 30 3.5
Fenwick 25 0

You will need to use the SQL case function to convert letter grades into numbers that can be
averaged. A letter grade of B should be 3.0 and a letter grade of C should be 2.0, etc.

Example of using case expressions can be found on pages 284-285 of the textbook.

SELECT t.instructor, COUNT(*) AS "NumberTakingCalculus2", AVG(t2.num_grade) AS


"AverageGradeCalculus2"
FROM (
SELECT e1.studentid, c1.instructor
FROM enrollment e1 JOIN course c1
ON e1.courseid = c1.courseid
WHERE c1.name = "Calculus 1"
) t JOIN (
SELECT e2.studentid,
CASE grade
WHEN "A" THEN 4.0
WHEN "B" THEN 3.0
WHEN "C" THEN 2.0
WHEN "D" THEN 1.0
WHEN "F" THEN 0.0
ELSE 0.0
END AS "num_grade"
FROM enrollment e2 JOIN course c2 ON e2.courseid = c2.courseid
WHERE c2.name = "Calculus 2"
) t2 ON t.studentid = t2.studentid
GROUP BY t.instructor;

Page 5 of 11
CST363 Database Exam 1 Fall 2019(a)

7. Write an SQL select that would return a transcript data for a student. Test your query for
student 3729. The transcript should be in chronological order and should include course id,
name, semester, grade, units. Note that the values of semester do not sort in correct order.
The correct chronological order would be: 2017 Spring, 2017 Summer, 2017 Fall , 2018 Spring,
etc. . In your answer include the SQL statement used, and a screenshot showing the transcript
list of courses and grade taken by student 3729 in the correct sequence. You will probably need
to use a case function to calculate a value that can be sorted to get data in the correct sequence.

SELECT c.courseid, c.name,t.semester, t.grade, t.units


FROM course c JOIN (
SELECT *,
CASE e.semester
WHEN "Spring2016" THEN 1
WHEN "Fall2016" THEN 2
WHEN "Spring2017" THEN 3
END AS "num_semester"
FROM enrollment e
) t ON c.courseid = t.courseid
WHERE t.studentid = 3729
ORDER BY t.num_semester;

Page 6 of 11
CST363 Database Exam 1 Fall 2019(a)

8. A student wants to enroll in Math 170 Discrete Mathematics. If the student has already taken
the course and passed (grade of 2.0 or better), or taken and failed the course 2 times, they
cannot enroll again.
a. Use your query to determine if student 3729 can enroll in the course.

i.
b. Can student 5430 enroll in the course?

i.
c. Include the sql statement you used in your answer here.

SELECT t.studentid, COUNT(c.name) AS times_taken, MAX(num_grade) AS


highest_grade,
CASE WHEN COUNT(c.name) >= 2 AND MAX(num_grade) < 2.0 THEN "No"
WHEN COUNT(c.name) > 0 AND MAX(num_grade) >= 2.0 THEN "No"
ELSE "Yes"
END AS can_enroll
FROM course c JOIN (
SELECT e.studentid, e.courseid,
CASE grade
WHEN "A" THEN 4.0
WHEN "B" THEN 3.0
WHEN "C" THEN 2.0
WHEN "D" THEN 1.0
WHEN "F" THEN 0.0
ELSE 0.0
END AS "num_grade"
FROM enrollment e) t ON c.courseid = t.courseid
WHERE t.studentid = 5430 AND c.name = "Discrete Math";

9. Write a SQL update statement that changes the grade for student 4456 who passed the course
Discrete Math in semester 2017Sp. Change the grade from C to B. For full credit you should
have a single update statement that has a where clause that verifies that the current grade is C.

UPDATE enrollment SET grade = "B" WHERE studentid = 4456 AND


semester = "Spring2017" AND grade = "C" AND courseid
IN (SELECT c.courseid FROM course c WHERE c.name = "Discrete Math");

Page 7 of 11
CST363 Database Exam 1 Fall 2019(a)

The following questions are based on the tables

Product (maker, model, type)


PC(model, speed, ram, hd, price)
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

The file exam1_schema_data.sql can be used to create the tables and load sample data.

Write SQL SELECT statements to answer the following questions. Show the result of your query and
your SQL SELECT statement. However, your SQL statement should work for any data, not just the
sample data given.

10. What PC models have a speed of at least 3.00?

SELECT model, speed FROM pc WHERE speed >= 3.0;

11. List all manufacturers that makes laptops with a hard disk of at least 100GB?

SELECT maker
FROM product p JOIN laptop l ON p.model = l.model
WHERE l.hd > 100 AND p.type = "laptop" ;

12. Find the model number and price of all products (of any type) made by manufacturer B? [hint:
this requires a union]

SELECT p.type, l.model, l.price FROM laptop l JOIN product p ON l.model = p.model WHERE
p.maker = "B" AND p.type = "laptop"
UNION
SELECT p.type, pc.model, pc.price FROM pc JOIN product p ON pc.model = p.model WHERE
p.maker = "B" AND p.type = "pc"
UNION
SELECT p.type, ptr.model, ptr.price FROM printer ptr JOIN product p ON ptr.model = p.model
WHERE p.maker = "B" AND p.type = "printer";

Page 8 of 11
CST363 Database Exam 1 Fall 2019(a)

13. Find the model numbers for all color laser printers.

SELECT model FROM printer WHERE color = 1;

14. Find those manufacturers that sell laptops but not pc’s.

SELECT DISTINCT p.maker


FROM product p JOIN laptop l ON p.model = l.model
WHERE p.type = "laptop" AND p.maker NOT IN (
SELECT p2.maker FROM product p2 JOIN pc ON p2.model = pc.model WHERE p2.type = "pc");

15. Find those hard-drive sizes that occur in two or more PC’s. [hint; use group and having]

SELECT hd FROM pc GROUP BY hd HAVING COUNT(hd) >= 2;

16. Find those pairs of PC models that have both the same speed and RAM. A pair should be listed
only once: e.g. list (f, g) but not (g,f)

SELECT DISTINCT pc.model, pc2.model


FROM pc JOIN pc pc2 ON (pc.speed = pc2.speed AND pc.ram = pc2.ram)
WHERE pc.model < pc2.model;

17. Find the maker of the color printer with the lowest price.

SELECT p.maker FROM product p JOIN printer ptr ON p.model = ptr.model


WHERE ptr.price = (
SELECT MIN(ptr.price)
FROM product p JOIN printer ptr ON p.model = ptr.model
WHERE ptr.color = 1 AND p.type = "printer");

Page 9 of 11
CST363 Database Exam 1 Fall 2019(a)

18. Find the manufacturer of PC’s with at least three different speeds.

SELECT p.maker
FROM pc JOIN product p ON pc.model = p.model
WHERE p.type = "pc"
GROUP BY p.maker
HAVING COUNT(speed) >= 3;

19. Find those manufacturers of at least two different computers (PC’s or laptops) with speeds of at
least 2.80

SELECT t.maker FROM (


SELECT p.maker, pc.speed FROM pc JOIN product p ON pc.model = p.model WHERE p.type =
"pc" AND speed >= 2.8
UNION
SELECT p.maker, l.speed FROM laptop l JOIN product p ON l.model = p.model WHERE p.type =
"laptop" AND speed >= 2.8

20. Find the manufacturer(s) of the computer (PC or laptop) with the highest speed.

SELECT t.maker
FROM (
SELECT p.maker, pc.speed FROM pc JOIN product p ON pc.model = p.model WHERE p.type =
"pc"
UNION
SELECT p.maker, l.speed FROM laptop l JOIN product p ON l.model = p.model WHERE p.type =
"laptop"
)t
WHERE t.speed = (SELECT MAX(t2.speed) FROM (
SELECT p.maker, pc.speed FROM pc JOIN product p ON pc.model = p.model WHERE p.type =
"pc"
UNION
SELECT p.maker, l.speed FROM laptop l JOIN product p ON l.model = p.model WHERE p.type =
"laptop"
) t2);

Page 10 of 11
CST363 Database Exam 1 Fall 2019(a)

Course feedback. I would very much appreciate your feedback on the course so
far. Many of the assignments are redesigned for this semester. Your feedback will
help me to keep improving the course. Your answers here do not affect your grade.

1. Are you satisfied with the help available in the course and the feedback that
you receive on assignments and quizzes from instructor, TA and fellow
students? What changes (if any) would you like to see?

Yes, I am satisfied.

2. What is your opinion of the Murach MySQL textbook. Do you think that the
book is well written, easy to understand, relevant to your career goal, worth
the cost? Did you find anything missing that you wished had been included
in the book?

Rating: 1 (lowest) – 5 (highest)

Why?

Because the book provides good examples for every topic.

3. Rate the following topics and resources as helpful or not in your learning.

Helpful Not
Helpful
Orientation X
Videos on SQL joins, grouping and subselects X
Assignment 2 on multiple table queries X
Assignment 3 on complex queries, grouping, X
subselects

4. Other comments positive or negative you have about the course so far?

No, sorry. So far so good, there is a lot to learn in such a short period of time.

Page 11 of 11

You might also like