僱員數據庫查詢

--3.9
--a.找出所有爲'small Bank Corporation'工作的僱員的名字及其居住的城市

select employee.employee_name,city
from employee join works on(employee.employee_name=works.employee_name)
where company_name='small Bank Corporation'

--b.找出所有爲'small Bank Corporation'工作且薪金超過10000美元的僱員名字、居住街道和城市

select employee.employee_name,city,street
from employee join works on(employee.employee_name=works.employee_name)
where company_name='small Bank Corporation' and salary>5000

--c.找出數據庫中所有不爲''small Bank Corporation'工作的僱員

select works.employee_name
from works
where works.employee_name not in(
    select employee.employee_name from employee join works on(employee.employee_name=works.employee_name)
    where company_name='small Bank Corporation' )

--d.找出數據庫中工資高於''small Bank Corporation'的每個僱員的所有僱員

select employee_name
from works
where salary>=(
    select max(salary) from works where company_name='small Bank Corporation'
)

--e.假設一個公司在好幾個城市有分部,找出位於'small Bank Corporation'所有所在城市的所有公司

select company_name from company as W where not exists(
    (select city from company where company_name='small Bank Corporation')
    except
    (select city from company as T where W.company_name=T.company_name)
)

--f.找出僱員最多的公司

select company_name
 from works
 group by  company_name
having count(*) >=all(
    select count(*)
    from works
 group by  company_name
)

--g.找出平均工資高於'small Bank Corporation'平均工資的那些公司
select company_name
 from works
 group by  company_name
having avg(salary) >all(
    select avg(salary)
    from works
 where company_name='small Bank Corporation'
)

--3.16
--a.找出所有爲'small Bank Corporation'工作的僱員的名字

select employee_name from works where company_name='small Bank Corporation'

--b.找出數據庫中所有居住城市和公司所在城市相同的僱員

select e.employee_name
 from employee as e,works as w,company as c
 where e.employee_name=w.employee_name and w.company_name=c.company_name and e.city=c.city

--c.找出數據庫中所有居住的街道和城市與其經理相同的僱員************************                  

select t.employee_name
from employee as t
where street in(
    select street from employee as e join manages as m on(e.employee_name=m.manages_name)  
    where t.street=street
)
and city in(
    select city from employee as e join manages as m on(e.employee_name=m.manages_name)
    where t.city=city
)

--d.找出工資高於其所在公司僱員平均工資的所有僱員

select employee_name
from works as w
where salary>=all(
    select avg(salary) from works as t
    where w.company_name=t.company_name
)

--f.找出工資總和最少的公司***********************                          

select company_name
from works
group by company_name
having sum(salary)<=all(
    select sum(salary) from works group by company_name
)

--3.17
--a.爲'small Bank Corporation'的所有僱員增長10%的工資

update works set salary=salary*1.1 where company_name='small Bank Corporation'

--b.爲'small Bank Corporation'的所有經理增長10%的工資

update works set salary=salary*1.1 where company_name='small Bank Corporation' and employee_name in(
        select manages_name
        from manages
        )

--c.刪除'small Bank Corporation'的僱員在works關係中的所有元組
delete from works where company_name='small Bank Corporation'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章