-- 34 列子查詢:
-- 34.1 查詢普通員工(非領(lǐng)導(dǎo))的工資等級(jí):empno,ename,sal,grade
use test2;
# 1,找出找出領(lǐng)導(dǎo)的員工編號(hào), mgr字段
select distinct mgr from emp where mgr is not null;
# 2,查詢?nèi)w員工工資等級(jí)
select *
from emp left join salgrade
on sal >=losal and sal <= hisal;
# 3 ,前面兩步結(jié)合
select *
from emp left join salgrade
on sal >=losal and sal <= hisal
where empno not in (select distinct mgr from emp where mgr is not null);
-- 練習(xí), 查詢領(lǐng)導(dǎo)的工資等級(jí):empno ,ename,job,mgr,sal,grade
##第1步,找出領(lǐng)導(dǎo)的員工編號(hào)
select distinct mgr from emp where mgr is not null;
## 2,查詢?nèi)w員工工資等級(jí)
select *
from emp left join salgrade
on sal >=losal and sal <= hisal;
## 3,把第一步的結(jié)果做一個(gè)where條件篩選子查詢
select empno 領(lǐng)導(dǎo)的員工編號(hào) ,ename 姓名 ,job 職位,mgr 直屬領(lǐng)導(dǎo) ,sal 工資 ,grade 工資等級(jí)
from emp left join salgrade
on sal >=losal and sal <= hisal
where empno in (select distinct mgr from emp where mgr is not null);
-- 34.2 查詢基本工資高于30號(hào)部門任意員工的其他部門員工信息 (高于其中一個(gè)即可)(不包含30號(hào)部門)
#a=4, 判斷 a >any(1,3,5) ,結(jié)果true
#a=4, 判斷 a >all (1,3,5) ,結(jié)果False
#練習(xí)
#a=7, 判斷 a >any(1,3,7,9) ,結(jié)果是
#a=12, 判斷 a >all(1,3,7,10),結(jié)果是
# 第1步 查詢30號(hào)部門任意員工的工資,作為一個(gè)集合
select sal from emp where deptno=30;
# 第2步,查詢所有員工的信息 (含工資信息)
select * from emp ;
# 第3步,前兩步合起來(lái)
select *
from emp where sal > any(查詢30號(hào)部門任意員工的工資組成的集合)
and deptno !=30;
-- 練習(xí), 查詢基本工資高于30號(hào)部門所有員工的其他部門員工信息
#思路1 ,> all(30號(hào)部門工資集合)
select * from emp where sal >all(select sal from emp where deptno=30);
#思路2,大于30號(hào)部門最高工資
#第1步,查詢30號(hào)部門最高工資
select max(sal) from emp where deptno=30;
#第2步,查詢所有員工工資信息
select * from emp;
#第3步,前兩步結(jié)合, where sal > 查詢30號(hào)部門最高工資(子查詢)
select * from emp
where sal >(select max(sal) from emp where deptno=30)
and deptno !=30;








暫無(wú)數(shù)據(jù)