a) Display name, ID, and department of students.
SQL query:
SELECT name, id, dept_name
FROM student;
b) Display name, ID, department and salaries of instructors.
SQL query:
SELECT name, id, dept_name, salary FROM instructor;
c) Display the average salaries of instructors in each department.
SQL query:
SELECT dept_name, AVG (salary) AS avg_salary FROM instructor GROUP BY dept_name;
d) Display students and the courses they took in each year.
SQL query:
SELECT s.name AS student_name, t.course_id, t.year
FROM takes t
JOIN student s ON t.id = s.id
ORDER BY t.year, s.name;
e) Display instructors and the courses they taught in each year.
SQL query:
SELECT i.name AS instructor_name, t.course_id, t.year
FROM teaches t
JOIN instructor i ON t.id = i.id
ORDER BY t.year, i.name;
f) Display the number of students each instructor is advising.
SQL query:
SELECT i.name AS instructor_name, COUNT (a.s_id) AS student_count
FROM advisor a
JOIN instructor i ON a.i_id = i.id
GROUP BY i.name;
g) Display the name of student and name of instructor who advised student with ID 00128
SQL query:
SELECT s.name AS student_name, i.name AS instructor_name
FROM advisor a
JOIN student s ON a.s_id = s.id
JOIN instructor i ON a.i_id = i.id
WHERE s.id = '00128';
h) Display the instructors, the courses they taught and the venues (building and room number)
where the lectures were conducted.
SQL query:
SELECT i.name AS instructor_name, t.course_id, c.building, c.room_number
FROM teaches t
JOIN instructor i ON t.id = i.id
JOIN section s ON t.course_id = s.course_id AND t.sec_id = s.sec_id AND t.semester =
s.semester AND t.year = s.year
JOIN classroom c ON s.building = c.building AND s.room_number = c.room_number;
i) Display each student and the number of courses s/he took.
SQL query:
SELECT s.name AS student_name, COUNT(t.course_id) AS courses_taken
FROM student s
JOIN takes t ON s.id = t.id
GROUP BY s.name;
j) Find the IDs of all students who were taught by an instructor named Einstein; make sure there
are no duplicates in the result.
SQL query:
SELECT DISTINCT t.id
FROM takes t
JOIN teaches te ON t.course_id = te.course_id AND t.sec_id = te.sec_id AND t.semester =
te.semester AND t.year = te.year
JOIN instructor i ON te.id = i.id
WHERE i.name = 'Einstein';
k) Find courses taught by instructor with ID 10101 and students who attended such courses in each
year.
SQL query:
SELECT t.course_id, t.year, s.name AS student_name
FROM teaches te
JOIN takes t ON te.course_id = t.course_id AND te.sec_id = t.sec_id AND te.semester =
t.semester AND te.year = t.year
JOIN student s ON t.id = s.id
WHERE te.id = '10101'
ORDER BY t.year, t.course_id, s.name;
l) Find the room numbers used by the instructor Gold in teaching his courses in each year
SQL query:
SELECT DISTINCT c.room_number, t.year
FROM teaches te
JOIN section s ON te.course_id = s.course_id AND te.sec_id = s.sec_id AND te.semester =
s.semester AND te.year = s.year
JOIN classroom c ON s.building = c.building AND s.room_number = c.room_number
JOIN instructor i ON te.id = i.id
WHERE i.name = 'Gold';