Professional Documents
Culture Documents
Assignment 2
Assignment 2
(a) Write an SQL statement to display the breed, type and DOB for all pets having the type
Dog and the breed Rich.
(b) Write an SQL statement to display the pet ID, breed and type for all pets having a five
character name starting with T.
(c) Write an SQL statement to display the average weight per pet breed. Consider breeds for
which two or more pets are included in the database, but do not consider any pet having
the breed of Unknown.
(d) Find the Owner’s address of pet id 3.
(e) Find the youngest of pet in dog.
Q2. Write the following queries in SQL, using the university schema.
classroom(building, room_number, capacity)
department(dept_name, building, budget)
course(course_id, title, dept_name, credits)
instructor(ID, name, dept_name, salary)
section(course_id, sec_id, semester, year, building, room number, time slot id)
teaches(ID, course_id, sec_id, semester, year)
student(ID, name, dept_name, tot cred)
takes(ID, course_id, sec_id, semester, year, grade)
advisor(s_ID, i_ID)
time slot(time_slot_id, day, start_time, end_time)
prereq(course_id, prereq_id)
(a) Find the ID and name of each student who has not taken any course offered before 2017.
(b) Find the ID and name of each student who has taken at least one Comp.Sci. course, make
sure there are no duplicate names in the result.
(c) For each department, find the maximum salary of instructors in that department. You may
assume that every department has at least one instructor.
(d) Increase the salary of each instructor in the Comp. Sci. department by 15%.
(e) Enroll every student in the Comp. Sci. department.
Answers
1. (a) SELECT Pbreed, Ptype, PDOB FROM pet WHERE Ptype = "Dog" AND Pbreed =
"Rich";
(b) SELECT Pet_ID, Pbreed, Ptype FROM pet WHERE Petname LIKE "%T____";
(c) SELECT Pbreed , AVG(Pweight) FROM pet GROUP BY Pbreed HAVING Pbreed <>
‘Unknown’ AND Count(*) >= 2;
(b) SELECT DISTINCT student.ID, student.name FROM student INNER JOIN takes ON
takes.ID = student.ID INNER JOIN course ON course.course_id = takes.course_id
WHERE course.dept_name = ‘Comp. Sci.’;
(d) UPDATE instructor SET salary = salary * 1.15 WHERE dept_name = ‘Comp. Sci.’;