0% found this document useful (0 votes)
6 views

Exercises With Solutions - SQL (1)

The document contains a series of SQL exercises along with their solutions, covering various queries related to instructors, courses, and departments. It includes tasks such as retrieving instructor names based on salary, listing course IDs for specific semesters, and performing updates and deletions based on certain conditions. Each question is followed by the corresponding SQL query to achieve the desired result.

Uploaded by

jeequest17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exercises With Solutions - SQL (1)

The document contains a series of SQL exercises along with their solutions, covering various queries related to instructors, courses, and departments. It includes tasks such as retrieving instructor names based on salary, listing course IDs for specific semesters, and performing updates and deletions based on certain conditions. Each question is followed by the corresponding SQL query to achieve the desired result.

Uploaded by

jeequest17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercises with Solutions - SQL

Q.1. Find the names of all instructors in the "Comp. Sci" department who have salary greater
than $70,000.

Ans: select name


from instructor
where dept_name='Comp. Sci.' and salary>70000;

Q.2. Retrieve the names of all instructors, along with their department names and department
building names.

Ans: select name, department.dept_name, building


from instructor, department
where instructor.dept_name = department.dept_name;

// 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."

Ans: select name, course_id


from instructor, teaches
where instructor.ID = teaches.ID and instructor.dept_name='Comp. Sci.';

Or,

select name, course_id


from instructor natural join teaches
where instructor.dept_name='Comp. Sci.';

Q.4. "List the names of all instructors along with the titles of the courses that they teach."

Ans: select name, title


from (instructor natural join teaches) join course
using (course_id);

NOTE: the following query would be incorrect:-

select name, title


from instructor natural join teaches natural join course;

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".

Ans: select course_id


from section
where semester = 'Fall' and year = '2017'
minus
select course_id
from section
where semester = 'Spring' and year = '2018';

Q.7. "Find the total no. of instructors who teach a course in the Spring 2018 semester".

Ans: select count(distinct ID)


from teaches
where semester = 'Spring' and year = '2018';

Q.8. "Find the number of instructors in each department who teach a course in the Spring 2018
semester."

Ans: select dept_name, count(distinct ID) as instr_count


from instructor natural join teaches
where semester = 'Spring' and year = '2018'
group by dept_name;

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."

Ans: select course_id, semester, year, sec_id, avg(tot_cred)


from takes natural join student
where year = '2017'
group by course_id, semester, year, sec_id
having count(ID) >=2;

Q.10. "Find the departments that have the highest average salary."

Ans: select dept_name


from instructor
group by dept_name

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."

Ans: select dept_name, avg_salary


from (select dept_name, avg(salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;

Q.12. "Delete all tuples from the instructor relation for those instructors associated with a
department located in the 'Watson' building."

Ans: delete from instructor


where dept_name in (select dept_name
from department
where building='Watson');

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.

Ans: insert into instructor


select ID, name, dept_name, 40000
from student
where dept_name='Music' and tot_cred > 10;

Q.14. “Give a 5 percent salary raise to the instructors whose salary is less than average.” Write
the corresponding update query.

Ans: update instructor


set salary = salary*1.05
where salary < (select avg(salary)
from instructor);

You might also like