Oracle中面试题的查询语句

本文深入探讨了SQL语言的应用技巧,包括如何通过多种查询方式获取特定数据,例如使用子查询、自关联、分组和聚合函数等高级特性来解决复杂的数据分析需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 01. 查询员工表所有数据,并说明使用*的缺点  
  2.   
  3. select * from emp;  
  4.   
  5. 01.查询职(job)为'PRESIDENT'的员工的工资  
  6.   
  7. select sal from emp where job='PRESIDENT';  
  8.   
  9. 02.查询佣金为0或为null的员工的信息  
  10.   
  11. select * from emp where comm is null or comm=1;  
  12.   
  13. 03.查询入职日期在1981-5-1到1981-12-31之间的所有员工的信息  
  14.   
  15. select * from emp where hiredate between to_date(‘1-5月-81’) and to_date(‘31-12月-81’)  
  16.   
  17. 04..查询所有名字长度为4的员工的员工的编号,姓名  
  18.   
  19. select empno,ename from emp where length(ename)=4  
  20.   
  21. 05.显示10号部门饿所有经理和20号部门的所有员工    
  22.   
  23. select * from emp where job='MANAGER' and deptno=10 or job='CLERK' and deptno=20;  
  24.   
  25. 06.显示姓名没有'L'字的员工的详细信息或含有'SM'字的员工信息  
  26.   
  27. select * from emp where ename not like '%L%' or ename like '%SM%';  
  28.   
  29. 07.显示各个部门经理的工资  
  30.   
  31. select deptno,sal from emp where job='MANAGER';  
  32.   
  33. 08.显示佣金收入比工资高的员工的详细信息  
  34.   
  35. select * from emp where comm>sal;  
  36.   
  37.    
  38.   
  39. 10.把hiredate列看做是员工的生日,求本月过生日的员工:  
  40.   
  41. SQL> select * from emp   
  42.   
  43. where to_char(hiredate,'mm')=to_char(sysdate,'mm');  
  44.   
  45. 11.把hiredate列看做是员工的生日,求下月过生日的员工  
  46.   
  47.       SQL> select * from emp   
  48.   
  49. where to_char(hiredate,'mm')=to_char(add_months(sysdate,1  
  50.   
  51. ),'mm');  
  52.   
  53. 12.求1982年入职的员工  
  54.   
  55.       SQL> select * from emp where to_char(hiredate,'yyyy')='1982';  
  56.   
  57.       hiredate是date类型的,1982是字符串类型的,类型匹配才可以  
  58.   
  59. 13.求1981年下半年入职的员工  
  60.   
  61.       select * from emp   
  62.   
  63. where hiredate between to_date('1981-7-1','yyyy-mm-dd')  
  64.   
  65. and to_date('1982-1-1','yyyy-mm-dd')-1;  
  66.   
  67.       注:to_char()函数和to_date()函数  
  68.   
  69.             to_char()函数是把日期的类型转换为指定的格式  
  70.   
  71.             to_date()是把字符串转换为日期类型  
  72.   
  73.             -1的原因是更精确  
  74.   
  75. 14.求1981年各个月入职的员工个数  
  76.   
  77.       SQL> select to_char(hiredate,'mm'),count(*) from emp where to_char(hiredate,'yyy  
  78.   
  79. y')='1981' group by to_char(hiredate,'mm') order by to_char(hiredate,'mm');  
  80.   
  81.    
  82.   
  83. 或  
  84.   
  85. select to_char(hiredate,'mm'),count(*) from emp where to_char(hiredate,'yyyy')='1981' group by to_char(hiredate,'mm'order by to_char(hiredate,'mm');  
  86.   
  87.    
  88.   
  89. PartII  
  90.   
  91. 01  .查询各个部门的平均工资  
  92.   
  93. SQL> select deptno,avg(nvl(sal,0)) from emp group by deptno;  
  94.   
  95. 02.显示各种职位的最低工资  
  96.   
  97.       SQL> select job,min(sal) from emp group by job;  
  98.   
  99. 03.按照入职日期由新到旧排列员工信息  
  100.   
  101.       SQL> select hiredate from emp order by hiredate desc;  
  102.   
  103. 04.查询员工的基本信息,附加其上级的姓名(自关联)  
  104.   
  105.       SQL> select e.*,e1.ename from emp e,emp  e1 where e.mgr=e1.empno;  
  106.   
  107. 05.显示工资比’ALLEN’高的所有员工的姓名和工作  
  108.   
  109.       SQL> select ename,sal from emp where sal>(  
  110.   
  111. select sal from emp where ename='ALLEN');  
  112.   
  113. 06.显示与scott从事相同工作的员工的信息(子查询)  
  114.   
  115.       SQL> select * from emp where job=(  
  116.   
  117.       select job from  emp where ename='SCOTT');  
  118.   
  119. 07.显示销售部(‘SALES’)员工的姓名  
  120.   
  121. SQL> select e.ename from emp e inner join dept d on e.deptno=d.deptno where d.dname='SALES';  
  122.   
  123. 08.显示与30号门’MARTIN’员工工资相同的员工的姓名和工资  
  124.   
  125. SQL> select ename,sal from emp where sal=(select sal from emp where deptno=30 and ename='MARTIN');  
  126.   
  127. 09.查询所有工资高于平均工资(包括所有员工)的销售人员  
  128.   
  129. SQL> select * from emp where sal>(  
  130.   
  131.       select avg(sal) from emp) and job='SALESMAN';  
  132.   
  133.       或  
  134.   
  135.       SQL> select * from emp where job='SALESMAN' and sal>(  
  136.   
  137.     select avg(sal) from emp);  
  138.   
  139. 10.显示所有职员的姓名及其所在部门的名称和工资(表连接)  
  140.   
  141. SQL> select e.*,e.sal,d.dname from emp e inner join dept d on e.deptno=d.deptno;  
  142.   
  143. 11.查询在研发部(RESEARCH)工作人员的编号,姓名,工作部门,工作所在地  
  144.   
  145.     SQL> select e.empno,e.ename,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno where dname='RESEARCH';  
  146.   
  147. 12.查询各个部门的名称和员工人数  
  148.   
  149. select e.deptno,d.dname,count(*) from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname  
  150.   
  151.    
  152.   
  153. 分析:  
  154.   
  155. SQL> select d.dname from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;  
  156.   
  157.    
  158.   
  159. DNAME  
  160.   
  161. --------------  
  162.   
  163. ACCOUNTING  
  164.   
  165. RESEARCH  
  166.   
  167. SALES  
  168.   
  169.    
  170.   
  171. SQL> select e.deptno,d.dname from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;  
  172.   
  173.    
  174.   
  175. DEPTNO DNAME  
  176.   
  177. ------ --------------  
  178.   
  179.     10 ACCOUNTING  
  180.   
  181.     20 RESEARCH  
  182.   
  183.     30 SALES  
  184.   
  185.    
  186.   
  187. SQL> select e.deptno,d.dname,count(*) from emp e inner join dept d on e.deptno=d.deptno group by e.deptno,d.dname;  
  188.   
  189.    
  190.   
  191. DEPTNO DNAME            COUNT(*)  
  192.   
  193. ------ -------------- ----------  
  194.   
  195.     10 ACCOUNTING              3  
  196.   
  197.     20 RESEARCH                5  
  198.   
  199. 30 SALES                   6  
  200.   
  201.    
  202.   
  203.    
  204.   
  205. 13.查询各个部门员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位(子查询)  
  206.   
  207.         
  208.   
  209. SQL> select count(*),job from emp where sal>(select avg(sal) from emp) group by  job;  
  210.   
  211. 14.查询工资相同的员工的工资和姓名(子查询)  
  212.   
  213. SQL> select sal,ename from emp e where(select count(*) from emp where sal=e.sal group by sal)>1;  
  214.   
  215. 或  
  216.   
  217. SQL> select e.sal,e.ename from emp e,emp e1 where e.sal=e1.sal and e.ename<>e1.ename;  
  218.   
  219. 15.查询工资最高的3名员工信息(排序)  
  220.   
  221.       SQL>  select rownum,e1.* from (  
  222.   
  223. select e.* from emp e order by sal desc) e1    
  224.   
  225. where rownum<=3;  
  226.   
  227. rownum是伪列,记性排序的  
  228.   
  229. 16.按工资进行排名:排名从1开始,工资相同排名相同(如果两个并列第1则没有地2名,从第三名继续排)  
  230.   
  231.     SQL> select e.*,(select count(*) from emp where sal>e.sal)+1 rank from emp e order by rank;  
  232.   
  233.       或 select sal,rank() over(order by sal descfrom emp;  
  234.   
  235. 17.求入职日期相同的(年月日相同)的员工  
  236.   
  237.     SQL> select * from emp e where(select count(*) from emp where e.hiredate=hiredate)>1;  
  238.   
  239.     或  
  240.   
  241.     SQL> select * from emp e,emp e1 where e.hiredate=e1.hiredate and e.empno<>e1.empno;  
  242.   
  243.    
  244.   
  245. 18.查询每个部门的最高工资  
  246.   
  247.     SQL> select max(sal),deptno from emp group by deptno order by deptno;  
  248.   
  249. 19.查询每个部门,每个职位的最高工资  
  250.   
  251.     SQL> select deptno,job,max(sal) from emp  group by deptno,job order by deptno;  
  252.   
  253. 20.查询每个员工的信息及工资级别,用到表(Salgrade)  
  254.   
  255.     SQL> select e.ename,s.grade from emp e,salgrade s where sal between losal and hisal;  
  256.   
  257. 或  
  258.   
  259. select e.*,s.grade from emp e inner join salgrade s on e.sal between s.losal and s.hisal  
  260.   
  261. 21.查询工资最高的第6-10名员工  
  262.   
  263.     SQL>  select * from (select rownum as r,e.* from (select * from emp order by sal desc) e) e1 where e1.r>=6 and e1.r<=10;  
  264.   
  265. 或  
  266.   
  267.     SQL> select * from(  
  268.   
  269.     select rownum as r,e.* from emp e order by sal desc) e1  
  270.   
  271.     where e1.r>=6 and e1.r<=10;  
  272.   
  273.     或  
  274.   
  275.     select * from (select rownum r,e1.* from  (select e.* from emp e order by sal desc) e1 where rownum<10) e2 where e2.r>=6  
  276.   
  277.    
  278.   
  279.     结果:  
  280.   
  281.          R   SAL  
  282.   
  283. ---------- -----  
  284.   
  285.          6  2450  
  286.   
  287.          7  1600  
  288.   
  289.          8  1500  
  290.   
  291.          9  1300  
  292.   
  293.          10  1250  
  294.   
  295.    
  296.   
  297.     Order by不能和rownum合用,  
  298.   
  299.       
  300.   
  301.    
  302.   
  303.     可以做分页啦!!  
  304.   
  305. 22.查询各个部门工资最高的员工信息  
  306.   
  307.     SQL> select * from emp e where e.sal=(  
  308.   
  309.     select max(sal) from emp where (deptno=e.deptno));  
  310.   
  311.     或  
  312.   
  313.     select * from emp e,(select deptno,max(sal) maxSal from emp group by deptno) e1 where e.sal=maxSal and e.deptno=e1.deptno  
  314.   
  315.     或  
  316.   
  317.     select * from emp e inner join (select deptno,max(sal) maxSal from emp group by deptno) e1 on e.sal=maxSal and e.deptno=e1.deptno  
  318.   
  319.    
  320.   
  321. 23.查询每个部门工资最高的前2名员工  
  322.   
  323.     SQL> select * from emp e where (  
  324.   
  325.     select count(*) from emp where sal>e.sal and e.deptno=deptno)<2  
  326.   
  327.     order by deptno,sal desc;  
  328.   
  329.    
  330.   
  331. 24.查询出有3个以上下属的员工信息(自关联)  
  332.   
  333.     select * from emp e where (   
  334.   
  335. select count(*) from emp where e.empno=mgr)>2  
  336.   
  337. 25.查询所有大于本部平均工资的员工信息  
  338.   
  339.     SQL> select * from emp e where sal>(  
  340.   
  341.     select avg(sal) from emp where e.deptno=deptno);  
  342.   
  343.     或  
  344.   
  345.     select * from emp e,(select deptno,avg(sal) avgSal from emp group by deptno) e1 where e.sal>avgSal and e.deptno=e1.deptno;  
  346.   
  347. 26.查询平均工资最高的部门信息  
  348.   
  349.     select d.* from dept d,(select deptno,avg(sal) av from emp group by deptno) where av=(select max(avg(sal)) from emp group by deptno) and e.deptno=d.deptno  
  350.   
  351.    
  352.   
  353.     或  
  354.   
  355.     select d.* from dept d where deptno=(select e.deptno from (select max(avgSal) maxSal from (select deptno,avg(nvl(sal,0)) avgSal from emp group by deptno)) e1 inner join (select deptno,avg(nvl(sal,0)) avgSal from emp group by deptno) e on e.avgSal=e1.maxSal)  
  356.   
  357.    
  358.   
  359.     注:如果在当前行的字段在查询的时候不能用别名:  
  360.   
  361. select dname,deptno dp from dept where dp=10  
  362.   
  363.    
  364.   
  365. ORA-00904: "DP": 标识符无效  
  366.   
  367. SQL> select dname,deptno dp from dept where deptno=10;  
  368.   
  369.    
  370.   
  371. DNAME           DP  
  372.   
  373. -------------- ---  
  374.   
  375. ACCOUNTING      10  
  376.   
  377.    
  378.   
  379. 也不能使用在inner join on条件中,可以使用在order by中,  
  380.   
  381. 别名放在from子句(select 。。。。别名  使用from)之后,这个别名可以在任何位置  
  382.   
  383.    
  384.   
  385. having中也不能使用别名  
  386.   
  387.    
  388.   
  389.    
  390.   
  391. 27.查询大于各个部门总工资的平均值的部门信息  
  392.   
  393. select d.*,sumsal from dept d,(select sum(nvl(sal,0)) sumsal,deptno from emp group by deptno) sumS where sumSal>(select avg(sum(nvl(sal,0))) from emp group by deptno) and sumS.deptno=d.deptno  
  394.   
  395.    
  396.   
  397. 28.查询大于各个部门总工资平均值的部门下的员工信息  
  398.   
  399.       select e.* from emp e,(select sum(nvl(sal,0)) sumSal,deptno from emp group by deptno) e1 where  
  400.   
  401. sumSal>(select avg(sum(nvl(sal,0))) from emp group by deptno) and e1.deptno=e.deptno  
  402.   
  403. 29.查询么有员工的部门信息  
  404.   
  405. select * from dept where deptno not in(select deptno from emp group by deptno);  
### ArcGIS Pro 中面要素裁切操作 在 ArcGIS Pro 中,可以通过 **“剪切”工具** 对面要素进行裁切操作。以下是关于该操作的相关说明以及实现方法: #### 使用“剪切”工具对面要素进行裁切 ArcGIS Pro 提供了一个名为“剪切”的地理处理工具,用于通过一个覆盖层来裁剪输入要素类中的区域[^1]。此工具适用于线和面数据。 要执行面要素的裁切操作,需遵循以下逻辑结构并调用相应功能模块: ```python import arcpy # 设置工作环境 arcpy.env.workspace = r"C:\path\to\your\geodatabase.gdb" # 输入待裁切的面要素类路径 input_features = "InputPolygonFeatureClass" # 裁切所使用的面要素类路径 clip_feature = "ClipPolygonFeatureClass" # 输出裁切后的结果要素类路径 output_feature_class = "OutputClippedFeatureClass" # 执行剪切操作 arcpy.analysis.Clip(input_features, clip_feature, output_feature_class) ``` 上述代码片段展示了如何利用 Python 脚本调用 `arcpy` 库完成自动化裁切任务。其中,“输入要素类”表示需要被裁减的目标面;而“裁切要素类”则定义了保留部分的空间范围[^2]。 #### 参数解释 - **输入要素 (in_features)**: 待裁切的目标面要素。 - **裁切要素 (clip_features)**: 用来限定目标面要素边界的新形状。 - **输出要素类 (out_feature_class)**: 存储裁切后结果的位置及名称。 #### 用户界面操作指南 如果偏好图形化用户界面而非编程,则可通过如下途径访问“剪切”工具: 1. 在 ArcGIS Pro 主窗口中导航至 **Analysis** 标签页下的 **Tools** 下拉菜单; 2. 搜索框内键入关键词 “Clip”,找到对应的地理处理工具; 3. 双击打开对话框,依次指定输入文件、裁切依据文件以及期望存储成果之处即可运行分析过程[^1]。 #### 注意事项 当尝试对大型数据集实施复杂几何运算时,请注意计算机性能瓶颈可能带来的影响。此外,在正式作业前建议先备份原始资料以防意外丢失重要信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值