Exercises With Solutions - SQL (1)
Exercises With Solutions - SQL (1)
Q.1. Find the names of all instructors in the "Comp. Sci" department who have salary greater
than $70,000.
Q.2. Retrieve the names of all instructors, along with their department names and department
building names.
// OR natural join
Q.3. "Find the instructor names and the course_ids of all courses taught by the instructors of the
'Comp. Sci.' department."
Or,
Q.4. "List the names of all instructors along with the titles of the courses that they teach."
Q.5. "Find the names of all instructors whose salary is greater than at least one instructor in the
'Biology' department."
1
Ans: select distinct T.name
from instructor T, instructor S
where T.salary>S.salary and S.dept_name='Biology';
Q.6. "List the course_ids of all the courses taught in the Fall 2017 semester but not in the Spring
2018 semester".
Q.7. "Find the total no. of instructors who teach a course in the Spring 2018 semester".
Q.8. "Find the number of instructors in each department who teach a course in the Spring 2018
semester."
Q.9. "For each course section offered in 2017, find the average total credits (tot_cred) of all
students enrolled in the section, if the section has at least 2 students."
Q.10. "Find the departments that have the highest average salary."
2
having avg(salary) >= all(select avg(salary)
from instructor
group by dept_name);
Q.11. "Find the average instructor-salaries of those departments where the average salary is
greater than 42,000."
Q.12. "Delete all tuples from the instructor relation for those instructors associated with a
department located in the 'Watson' building."
Q.13. "Suppose that we want to convert (promote) each student in the 'Music' department who
has earned more than 10 credit hours, as an instructor in the 'Music' department with a salary
40,000". Write an appropriate insertion query for this.
Q.14. “Give a 5 percent salary raise to the instructors whose salary is less than average.” Write
the corresponding update query.