1.列出薪金高于部門(mén)30工作的所有員工的薪金的員工姓名和薪金揪漩、部門(mén)名稱(chēng)、部門(mén)人數(shù)吏口。
確定要使用的數(shù)據(jù)表:
emp:姓名奄容、薪金
dept:部門(mén)名稱(chēng)
emp:部門(mén)人數(shù)
確定已知關(guān)鍵字:雇員和部門(mén)
e.deptno = d.deptno;
①找出部門(mén)30工作的所有員工的薪金
select sal
from emp e
where e.deptno=30;
②如果返回的結(jié)果是多行單列,應(yīng)該在where里面使用子查詢(xún)語(yǔ)句产徊,判斷詞有(in昂勒、any、all)
員工姓名和薪金
select e.ename,e.sal
from emp e
where sal>all(select sal
from emp e
where e.deptno=30);
③部門(mén)名稱(chēng)
select e.ename,e.sal,d.dname
from emp e,dept d
where sal>all(select sal
from emp e
where e.deptno=30) and e.deptno=d.deptno;
④部門(mén)人數(shù)
select deptno dno,count(empno)
from emp
group by deptno;
select e.ename,e.sal,d.dname,temp.count
from emp e,dept d,(
select deptno dno,count(empno) count
from emp
group by deptno
)temp
where sal>all(select sal
from emp e
where e.deptno=30)
and e.deptno=d.deptno
and temp.dno=d.deptno;
2.列出與scott從事相同工作的所有員工及部門(mén)名稱(chēng)舟铜,部門(mén)人數(shù)戈盈,及領(lǐng)導(dǎo)姓名。
確定相關(guān)聯(lián)的表:
emp表:?jiǎn)T工信息;
dept表:部門(mén)名稱(chēng)谆刨;
emp表:統(tǒng)計(jì)部門(mén)人數(shù)奕谭;
emp表:領(lǐng)導(dǎo)姓名
相關(guān)聯(lián)的字段:
雇員和部門(mén):e.deptno=d.deptno
雇員和領(lǐng)導(dǎo):emp.mgr = memp.empno
①列出從事scott工作的員工,此查詢(xún)返回單行單列痴荐,一般用where或having條件
select job from emp where ename='SCOTT';
②找到所有符合此要求的雇員信息
select e.ename,e.job,e.sal
from emp e
where e.job=(
select job
from emp
where ename='SCOTT'
);
③部門(mén)名稱(chēng)
select e.ename,e.job,e.sal,d.dname
from emp e,dept d
where e.job=(
select job
from emp
where ename='SCOTT'
and e.deptno=d.deptno);
④領(lǐng)導(dǎo)姓名
select e.ename,e.job,e.sal,d.dname,temp.count,
m.ename
from emp e,dept d,(
select deptno dno,count(empno) count
from emp
group by deptno)temp,emp m
where e.job=(
select job
from emp
where ename='SCOTT')
and e.deptno=d.deptno
and d.deptno=temp.dno
and e.mgr=m.empno;
⑤消除掉scott的用戶(hù)
select e.ename,e.job,e.sal,d.dname,temp.count,
m.ename
from emp e,dept d,(
select deptno dno,count(empno) count
from emp
group by deptno)temp,emp m
where e.job=(
select job
from emp
where ename='SCOTT')
and e.deptno=d.deptno
and d.deptno=temp.dno
and e.mgr=m.empno
and e.ename<>'SCOTT';
3.列出薪金比“SMITH”或“ALLEN”多的所有員工的編號(hào)血柳,姓名,部門(mén)名稱(chēng)生兆,其領(lǐng)導(dǎo)姓名难捌,部門(mén)人數(shù),平均工資鸦难、最高及最低工資根吁。
確定相關(guān)表:
emp:員工的編號(hào),姓名合蔽;
dept:部門(mén)名稱(chēng)击敌;
emp:領(lǐng)導(dǎo)信息;
emp:統(tǒng)計(jì)部門(mén)數(shù)據(jù)
相關(guān)字段:
雇員和部門(mén):e.deptno=d.deptno
雇員和領(lǐng)導(dǎo):emp.mgr = memp.empno
①列出"SMITH"或''ALLEN''
select sal
from emp
where ename in ('SMITH','ALLEN');
②以上查詢(xún)返回的是多行單列數(shù)據(jù)拴事,應(yīng)該在where字句中使用它沃斤。列出薪金比“SMITH”或“ALLEN”多的所有員工
select e.empno,e.ename,e.sal
from emp e
where e.sal>any(
select sal
from emp
where ename in('SMITH','ALLEN'))
and ename not in('SMITH','ALLEN');
③找到領(lǐng)導(dǎo)信息
select e.empno,e.ename,e.sal,m.ename
from emp e,emp m
where e.sal>any(
select sal
from emp
where ename in('SMITH','ALLEN'))
and ename not in('SMITH','ALLEN')
and e.mgr=m.empno;
④部門(mén)人數(shù)圣蝎,平均工資、最高及最低工資衡瓶。
select e.empno,e.ename,e.sal,m.ename,temp.count,
temp.avg,temp.max,temp.min,d.dname
from emp e,emp m,(
select deptno dno,count(empno)count,
avg(sal) avg,max(sal) max,min (sal) min
from emp
group by deptno) temp,dept d
where e.sal>any(
select sal
from emp
where
ename in('SMITH','ALLEN'))
and e.ename not in('SMITH','ALLEN')
and e.mgr=m.empno(+)
and temp.dno=d.deptno
and e.deptno=d.deptno;
4.列出受雇日期早于其直接上級(jí)的所有員工的編號(hào)徘公、姓名、部門(mén)名稱(chēng)哮针、部門(mén)位置关面、部門(mén)人數(shù)。
確定要使用的數(shù)據(jù)表:
emp:員工的編號(hào)十厢、姓名
dept:部門(mén)名稱(chēng)等太、部門(mén)位置
emp:部門(mén)人數(shù)
emp:找到領(lǐng)導(dǎo)雇用日期,作為自身關(guān)聯(lián)使用
確定已知的關(guān)聯(lián)字段:
雇員和領(lǐng)導(dǎo):emp.mgr = memp.empno
雇員和部門(mén):emp.deptno = dept.deptno
①列出受雇日期早于其直接上級(jí)的所有員工的編號(hào)蛮放、姓名
select e.empno,e.ename
from emp e,emp m
where e.mgr = m.empno(+) and e.hiredate
< m.hiredate;
②部門(mén)名稱(chēng)缩抡、部門(mén)位置
select e.empno,e.ename,d.dname,d.loc
from emp e,emp m,dept d
where e.mgr = m.empno(+)
and e.hiredate < m.hiredate
and e.deptno = d.deptno;
③部門(mén)人數(shù)
select e.empno,e.ename,d.dname,d.loc,temp.count
from emp e,emp m,dept d,(
select deptno dno,count(empno) count
from emp
group by deptno)temp
where e.mgr = m.empno(+)
and e.hiredate < m.hiredate
and e.deptno = d.deptno
and d.deptno = temp.dno;