99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
2023-08-11 閱讀量: 2390
CDA數(shù)據(jù)分析一級教材SQL語句 13

4.4子查詢


-- 查詢各部門員工人數(shù)占比

-- 查詢整體員工數(shù)量

select count(*) from emp;


將整體員工數(shù)量作為子查詢來查詢各部門員工人數(shù)占比

select deptno,count(x) 員工人數(shù)

count(*)/(select count(x) from emp) 員工人數(shù)占比

from emp group by deptno;


-- 查詢各部門最高工資

select deptno,max(sal) from emp group by deptno;


將各部門最高工資作為子查詢,找出所有部門內(nèi)部員工工資等于最高工資的員工

select empno,ename,sal,emp.deptno

from emp

left join (select deptno,max(sal) as最高工資from emp group by deptno) as t

on emp.deptno=t.deptno

where sal= 最高工資 ;


--where 本身只是查找所有工資等于最大值的記錄,返回記錄順序是隨機(jī)的如果需要有序返回,可以使用 order by 子句

-- 也可以直接使用開窗函數(shù)對工資進(jìn)行排名,查詢排名為 1的員工

select empno,ename,sal,deptno

from(select *,dense rank() over(partition by deptno order by sal desc) 工資排名 from emp) t

where 工資排名 =1;


-- 查詢公司所有員工的平均工資

select avg(sal) from emp;


-- 使用比較操作字符將員工工資與子查詢返回的平均工資進(jìn)行比較

select * from emp where sal>(select avg(sal) from emp);


-- 查詢 smith 的部門和職位

select deptno,job from emp where ename='smith';


使用比較操作字符進(jìn)行子查詢-

select empno,ename,job,deptno

from emp

where (deptno,job)=(select deptno,job from emp where ename='smith') and ename<>'smith';


查詢各部門最高工資的員工

select empno,ename,sal,deptno

from emp

where (deptno,sal) in (select deptno,max(sal) from emp group by deptno);


查詢哪些人不是普通員工,即領(lǐng)導(dǎo)的工號

select distinct mgr from emp where mgr is not null;

上述代碼的查詢結(jié)果如圖3-103所示

將第一步作為子查詢來篩選數(shù)據(jù),具體整合后的代碼如下

-- 將領(lǐng)導(dǎo)的工號作為子查詢來篩選數(shù)據(jù)

select empno,ename,sal

from emp

where empno not in (select distinct mgr from emp where mgr is not null);


-- 查詢公司部門情況,若公司有 20 的部門,則返回該部門的員工情況

select *

from emp

where exists (select *from dept where deptno=20) and deptno =20;


--exists 后接的子查詢并不對數(shù)據(jù)進(jìn)行篩選,只決定外查詢執(zhí)不執(zhí)行-

select *

from emp

where exists (select * from dept where deptno=20);


- 查詢 30 號部門所有員工的基本工資

select sal from emp where deptno=30;


-- 將 30 號部門所有員工的基本工資作為子查詢對記錄進(jìn)行篩選

Select *

from emp

where sal>any(select sal from emp where deptno=30) and deptno<>30;


--查詢基本工資高于 30 號部門所有員工的員工信息

--將any 改為 all

select *

from emp

where sal>all(select sal from emp where deptno=30);


0.0000
0
關(guān)注作者
收藏
評論(0)

發(fā)表評論

暫無數(shù)據(jù)
推薦帖子