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

Chapter 03 Part4 Database 3CS

Uploaded by

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

Chapter 03 Part4 Database 3CS

Uploaded by

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

Computer Science

Level #3

Chapter #3
Part #4

Database
2020
Q1. What is Nested Subqueries?
 Is a select-from-where expression that is nested within another query.

Q2. Using the next schema :

1) Find courses offered in Fall 2009 and in spring 2020


Select distinct course_id
from section
where semester = 'Fall' and year= 2009 and course_id in (select course_id
from section where semester = 'Spring' and year= 2010);
--Another solution using exists:
Select course_id from section as S
where semester = 'Fall' and year= 2009 and exists
(select * from section as T where semester = 'Spring' and
year= 2010 and S.course_id= T.course_id);
2) Find courses offered in Fall 2009 but not in Spring 2010
Select distinct course_id
from section
where semester = 'Fall' and year= 2009 and course_id not in (select course_id
from section where semester = 'Spring' and year= 2010);
--Can use not exists

1 DataBase 2020
3) Find the total number of (distinct) students who have taken course sections taught
by the instructor with ID 10101
Select count (distinct ID )
from takes
where (course_id, sec_id , semester , year ) in (select course_id , sec_id ,
semester , year
from teaches
where teaches .ID = 10101 );
4) Find names of instructors with salary greater than that of some (at least one)
instructor in the Biology department.
Select distinct T.name
from instructor as T, instructor as S
where T.salary> S.salary and S.dept_name = 'Biology';
Other Resolution
Select name
from instructor
where salary > some(select salary
from instructor
where dept_name = 'Biology');
5) Find the names of all instructors whose salary is greater than the salary of all
instructors in the Biology department.
Select name
from instructor
where salary > all (select salary
from instructor
where dept_name= 'Biology');
6) True Or False:

2 DataBase 2020
7) Delete all instructors from the Finance department
Delete from instructor where dept_name= 'Finance';

8) Delete all tuples in the instructor relation for those instructors associated with a
department located in the Watson building.
Delete from instructor

where dept_name in (select dept_name

from department where building = 'Watson');

9) Add a new tuple to course with two way.


Insert into coursevalues ('CS-437', 'Database Systems', 'Comp. Sci.', 4);

Insert into course (course_id , title , dept_name ,credits )values ('CS -437',

'Database Systems', 'Comp. Sci.', 4);

10) Add a new tuple to student with tot_creds set to null


Insert into studentvalues ('3003', 'Green', 'Finance', null);

11) Add all instructors to the student relation with tot_creds set to 0
Insert into student select ID, name, dept_name, 0 from instructor

12) Increase salaries of instructors whose salary is over $100,000 by 3%, and all
others receive a 5% raise
 Soluion1: Write two update statements:
Update instructor set salary = salary * 1.03

where salary > 100000;

Update instructorset salary = salary * 1.05

where salary <= 100000;

 Soluion2: Using with case.


Update instructor set salary = case

when salary <= 100000 then salary * 1.05

else salary*1.03

end

3 DataBase 2020

You might also like