![Page 1: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/1.jpg)
©Silberschatz, Korth and Sudarshan 3.1
SQL: PART II
![Page 2: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/2.jpg)
©Silberschatz, Korth and Sudarshan 3.2
Example: Employee Database
EMPLOYEE
emp_id name dept_id mgr_id salary
57 Alex 1 - 84000
23 Jen 1 57 72000
85 Max 3 19 51000
44 Tim 2 37 63000
DEPARTMENT
dept_id name
1 Sales
2 Accounting
3 HR
![Page 3: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/3.jpg)
©Silberschatz, Korth and Sudarshan 3.3
Query #1
Find all employees in the Sales and HR departments.
![Page 4: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/4.jpg)
©Silberschatz, Korth and Sudarshan 3.4
Query #1
Find all employees in the Sales and HR departments.
select e.name
from employee as e, department as d
where e.dept_id = d.dept_id
and (d.name = 'Sales'
or d.name = 'HR')
OR
select e.name
from employee as e
join department as d
on d.dept_id = e.dept_id
where d.name = 'Sales'
or d.name = 'HR'
![Page 5: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/5.jpg)
©Silberschatz, Korth and Sudarshan 3.5
Query #1
Find all employees in the Sales and HR departments.
select e.name
from employee as e
where e.dept_id in (
select d.dept_id
from department as d
where d.name = 'Sales'
or d.name = 'HR')
IN + SUBQUERY
(WHERE CLAUSE)
![Page 6: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/6.jpg)
©Silberschatz, Korth and Sudarshan 3.6
Query #2
Find all employees with the maximum salary.
![Page 7: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/7.jpg)
©Silberschatz, Korth and Sudarshan 3.7
Query #2
Find all employees with the maximum salary.
select e.name
from employee as e
where e.salary = (
select max(e2.salary)
from employee as e2)
SCALAR SUBQUERY
(WHERE CLAUSE)
![Page 8: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/8.jpg)
©Silberschatz, Korth and Sudarshan 3.8
Query #2
Find all employees with the maximum salary.
select e.name
from employee as e
join (
select max(e2.salary) as maxsal
from employee as e2)
on e.salary = maxsal
SUBQUERY
(FROM CLAUSE)
![Page 9: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/9.jpg)
©Silberschatz, Korth and Sudarshan 3.9
Query #3
Find the average salary for each department.
![Page 10: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/10.jpg)
©Silberschatz, Korth and Sudarshan 3.10
Query #3
Find the average salary for each department.
select d.name,
avg(e.salary) as avgsal
from department as d
join employee as e
on e.dept_id = d.dept_id
group by d.name
![Page 11: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/11.jpg)
©Silberschatz, Korth and Sudarshan 3.11
Query #3
Find the average salary for each department.
select d.name,
(select avg(e.salary)
from employee e
where e.dept_id = d.dept_id)
as avgsal
from department as d
SCALAR SUBQUERY
(SELECT CLAUSE)
![Page 12: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/12.jpg)
©Silberschatz, Korth and Sudarshan 3.12
Query #4
Find the departments with at most one manager.
![Page 13: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/13.jpg)
©Silberschatz, Korth and Sudarshan 3.13
Query #4
Find the departments with at most one manager.
select d.name
from department as d
join employee as e
on e.dept_id = d.dept_id
where e.mgr_id is null
group by d.name
having count(e.emp_id) <= 1
![Page 14: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/14.jpg)
©Silberschatz, Korth and Sudarshan 3.14
Query #4
Find the departments with at most one manager.
select d.name
from department as d
where unique (
select e.dept_id
from employee as e
where e.dept_id = d.dept_id
and e.mgr_id is null)
UNIQUE
![Page 15: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/15.jpg)
©Silberschatz, Korth and Sudarshan 3.15
Query #5
Find all managers who supervise at least one employee.
![Page 16: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/16.jpg)
©Silberschatz, Korth and Sudarshan 3.16
Query #5
Find all managers who supervise at least one employee.
select e.name
from employee as e
where exists (
select *
from employee as e2
where e2.mgr_id = e.emp_id)
EXISTS
![Page 17: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/17.jpg)
©Silberschatz, Korth and Sudarshan 3.17
Query #6
Find the names of all employees and their manager.
![Page 18: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/18.jpg)
©Silberschatz, Korth and Sudarshan 3.18
Query #6
Find the names of all employees and their manager.
select e.name as emp,
m.name as mgr
from employee as e
join employee as m
on m.emp_id = e.mgr_id
PROBLEM?
![Page 19: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/19.jpg)
©Silberschatz, Korth and Sudarshan 3.19
Query #6
Find the names of all employees and their manager.
EMPLOYEE
emp_id name dept_id mgr_id salary
57 Alex 1 - 84000
23 Jen 1 57 72000
85 Max 3 19 51000
44 Tim 2 37 63000
![Page 20: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/20.jpg)
©Silberschatz, Korth and Sudarshan 3.20
Query #6
Find the names of all employees and their manager.
select e.name as emp,
m.name as mgr
from employee as e
join employee as m
on m.emp_id = e.mgr_id
union
select e.name,
'n/a'
from employee as e
where e.mgr_id is null
![Page 21: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/21.jpg)
©Silberschatz, Korth and Sudarshan 3.21
Query #6
Find the names of all employees and their manager.
select e.name as emp,
m.name as mgr
from employee as e
left outer join employee as m
on m.emp_id = e.mgr_id
LEFT OUTER JOIN
![Page 22: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/22.jpg)
©Silberschatz, Korth and Sudarshan 3.22
Query #7
Find all departments where the total salary is greater than the average total salary for all departments.
![Page 23: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/23.jpg)
©Silberschatz, Korth and Sudarshan 3.23
Query #7
Find all departments where the total salary is greater than the average total salary for all departments.
with dept_ttl as (
select dept_id, sum(salary) as dttl
from employee group by dept_id),
with dept_avg as (
select avg(dttl) as davg
from dept_ttl)
select dept_id
from dept_ttl as t1,dept_avg as t2
where t1.dttl > t2.davg
WITH CLAUSE
![Page 24: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/24.jpg)
©Silberschatz, Korth and Sudarshan 3.24
Modifying a Table
insert – add new records
delete – remove some (or all) records
update – change some (or all) existing records
![Page 25: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/25.jpg)
©Silberschatz, Korth and Sudarshan 3.25
Insert
Add a new record to the EMPLOYEE table:
insert into
employee(id,name,dept_id,mgr_id,salary)
values(99,'Amy',2,16,95000)
![Page 26: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/26.jpg)
©Silberschatz, Korth and Sudarshan 3.26
Insert
Add a new record to the EMPLOYEE table:
insert into
employee
values(99,'Amy',2,16,95000)
![Page 27: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/27.jpg)
©Silberschatz, Korth and Sudarshan 3.27
Insert
Add a new record to the EMPLOYEE table:
insert into
employee
values(99,'Amy',2,null,95000)
![Page 28: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/28.jpg)
©Silberschatz, Korth and Sudarshan 3.28
Insert
Add all managers to the new MANAGER table:
insert into manager
select *
from employee
where mgr_id is null
![Page 29: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/29.jpg)
©Silberschatz, Korth and Sudarshan 3.29
Delete
Delete all employees from the EMPLOYEE table:
delete from employee
![Page 30: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/30.jpg)
©Silberschatz, Korth and Sudarshan 3.30
Delete
Delete employee #44:
delete from employee
where emp_id = 44
![Page 31: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/31.jpg)
©Silberschatz, Korth and Sudarshan 3.31
Delete
Delete all employees in the Sales department:
delete from employee
where dept_id = (
select dept_id
from department
where name = 'Sales')
![Page 32: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/32.jpg)
©Silberschatz, Korth and Sudarshan 3.32
Delete
Delete all employees whose salary is greater than the
average salary:
delete from employee
where salary > (
select avg(salary)
from employee)
PROBLEM?
![Page 33: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/33.jpg)
©Silberschatz, Korth and Sudarshan 3.33
Update
Make employee #23 a manager:
update employee
set mgr_id = null
where emp_id = 23
![Page 34: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/34.jpg)
©Silberschatz, Korth and Sudarshan 3.34
Update
Give all employees a 5% raise:
update employee
set salary = salary * 1.05
![Page 35: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/35.jpg)
©Silberschatz, Korth and Sudarshan 3.35
Update
Give all employees with a salary greater than
$100,000 a 3% raise and all others a 5% raise:
update employee
set salary = salary * 1.05
where salary <= 100000
update employee
set salary = salary * 1.03
where salary > 100000
PROBLEM?
![Page 36: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/36.jpg)
©Silberschatz, Korth and Sudarshan 3.36
Update
Give all employees with a salary greater than
$100,000 a 3% raise and all others a 5% raise:
update employee
set salary = case
when salary > 100000
then salary * 1.03
else
salary * 1.05
end
CASE STATEMENT
![Page 37: SQL: PART II · 3.4 ©Silberschatz, Korth and Sudarshan Query #1 Find all employees in the Sales and HR departments. select e.name from employee as e, department as d](https://reader030.vdocument.in/reader030/viewer/2022040800/5e362687eeed2e2167012f00/html5/thumbnails/37.jpg)
©Silberschatz, Korth and Sudarshan 3.37
If you disliked the lecture, please
forward all complaints to: