Question 1
Using the university schema, write an SQL query to find the names of those departments whose budget is higher than that of Physics. List them in alphabetical order.
select X.dept_namefrom department as X, department as Hwhere H.dept_name = 'Physics' and X.budget > H.budgetorder by X.dept_name
Question 2
Using the university schema, use SQL to do the following:
For each student who has retaken a course at least twice
(i.e., the student has taken the course at least three times),
show the course ID and the student’s ID.
Please display your results in order of course ID and do not display duplicate rows.
select distinct course_id, IDfrom takesgroup by ID, course_idhaving count(*) > 2order by course_id
Question 3
Using the university schema, write an SQL query to find the IDs of those students who have retaken at least three distinct courses at least once
(i.e, the student has taken the course at least two times).
select distinct IDfrom (-- At lease one time--select course_id, IDfrom takesgroup by ID, course_idhaving count(*) > 1)group by ID-- Have at lease three distinct course, count the number inside ID --having count(course_id) > 2
Question 4
Using the university schema, write an SQL query to find the ID and name of each instructor who has never given an A grade in any course she or he has taught.
(Instructors who have never taught a course trivially satisfy this condition.)
select ID, namefrom instructorexcept (select distinct instructor.ID, instructor.namefrom ((instructor join teaches using (ID))join takes using (course_id, year, semster, sec_id)where takes.grade = "A")
Question 5
Using the university schema, write an SQL query to find the number of students in each section. The result columns should appear in the order “courseid, secid, year, semester, num”. You do not need to output sections with 0 students.
select course_id, sec_id, year, semester, count(*) as numfrom takesgroup by course_id,sec_id,year,semester
