zoukankan      html  css  js  c++  java
  • sql语句练习

    使用的是scott用户下的emp 和dept表进行操作

    --(1) 查询20部门的所有员工信息。

    select * from emp where deptno=20;
    --(2) 查询所有工种为CLERK的员工的员工号、员工名和部门号。(注意大小写)
    select empno,ename,deptno from emp where upper(job)='CLERK'
    --(3) 查询奖金(COMM)高于工资(SAL)的员工信息。
    select * from emp where comm>sal
    --(4) 查询奖金高于工资的20%的员工信息。
    select * from emp where comm>sal*0.2
    --(5) 查询10号部门中工种为MANAGER和20部门中工种为CLERK的员工的信息。
    select * from emp where (job='MANAGER' AND DEPTNO=10)
                            OR (job='CLERK' AND DEPTNO=20)
    --(6) 查询所有工种不是MANAGER和CLERK,且工资大于或等于2000的员工的详细信息。
    select * from emp where job not in ('MANAGER','CLERK')
                           and sal >=2000
    --(7) 查询有奖金的员工的不同工种。
    select distinct job from emp where comm>0
    --(8) 查询所有员工工资与奖金的和。
    select ename,nvl(sal,0)+nvl(comm,0)  from emp
    --(9) 查询没有奖金或奖金低于100的员工信息。
    select * from emp where comm is null or comm <100
    --(10) 查询各月倒数第2天入职的员工信息。 Last_day
    select * from emp where hiredate=last_day(hiredate)-1
    (11) 查询工龄大于或等于10年的员工信息。months_between
    select * from emp where trunc(months_between(sysdate,hiredate)/12)>=10
    select * from emp where ((sysdate-hiredate)/365)>=10
    (12) 查询员工信息,要求以首字母大写其他字母小写的方式显示所有员工的姓名。initcap
    select initcap(ename) from emp
    (13) 查询员工名正好为6个字符的员工的信息。
    select * from emp where length(ename)=6
    (14) 查询员工名字中不包含字母"S"的员工。  Like 或 instr
    select * from emp where ename not like '%S%'
    select * from emp where instr(ename,'S')=0
    (15) 查询员工姓名的第二字母为"M"的员工信息。
    select * from emp where  ename like '_M%'
    (16) 查询所有员工姓名的前三个字符。
    select substr(ename,1,3) from emp
    (17) 查询所有员工的姓名,如果包含字母"s",则用"S"替换。Replace
    select replace(ename,'s','S') from emp
    (18) 查询员工的姓名和入职日期,并按入职日期从先到后进行排序。
    select ename,hiredate from emp order by hiredate asc
    (19) 显示所有员工的姓名、工种、工资和奖金,按工种降序排序,若工种相同则按工资升序排序。
    select ename,job,sal,comm from emp order by job desc,sal asc
    (20) 显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序,若月份相同则按入职的年份排序。
    select ename,to_char(hiredate,'yyyymm') from emp order by
     to_char(hiredate,'mm'),to_char(hiredate,'yyyy')
    (21) 查询在2月份入职的所有员工信息。
    select * from emp where to_char(hiredate,'mm')=2
    (22) 查询所有员工入职以来的工作期限,用"**年**月**日"的形式表示。
    select aa.ename,aa.nian||'年'||aa.yue||'月'||aa.ri||'天'  from (select ename,trunc((sysdate-hiredate)/365) as nian,trunc(mod(months_between(sysdate,hiredate),12)) as yue,trunc
    (mod((sysdate-hiredate),30)) as ri from emp) aa
    (23) 查询至少有一个员工的部门信息。
    select * from dept where deptno in (select deptno from emp)
    (24) 查询工资比SMITH员工工资高的所有员工信息。
    select * from emp where sal > (select sal from emp where ename='SMITH')
    (25) 查询所有员工的姓名及其直接上级的姓名。
    select a.ename,b.ename  from emp a,emp b where a.mgr=b.empno
    (26) 查询入职日期早于其直接上级领导的所有员工信息。
    select * from emp a where hiredate<
    (select hiredate from emp b where a.mgr=b.empno)
    (27) 查询所有部门及其员工信息,包括那些没有员工的部门。
    select d.deptno,d.dname,d.loc,e.empno,e.ename
     from dept d,emp e where d.deptno=e.deptno(+)
    (28) 查询所有员工及其部门信息,包括那些还不属于任何部门的员工。
    select d.deptno,d.dname,d.loc,e.empno,e.ename
     from dept d,emp e where d.deptno(+)=e.deptno
    (29) 查询所有工种为CLERK的员工的姓名及其部门名称。
    select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno
    and e.job='CLERK'
    (30) 查询最低工资大于2500的各种工作。
    select job,min(sal) from emp  group by job having min(sal)>2500
    (31) 查询平均工资低于2000的部门及其员工信息。
    select avg(sal),job from emp group by job having avg(sal)<2000;
    (32) 查询在SALES部门工作的员工的姓名信息。
    select ename from emp where deptno=(select deptno from dept where dname='SALES')
    select ename from emp e,dept d where e.deptno=d.deptno and d.dname='SALES'
    (33) 查询工资高于公司平均工资的所有员工信息。
    select * from emp where sal > (select avg(sal) from emp)
    (34) 查询出与SMITH员工从事相同工作的所有员工信息。
    select * from emp where job=(select job from emp where ename='SMITH')
    (35) 列出工资等于30部门中某个员工的工资的所有员工的姓名和工资。
    select ename,sal from emp where sal  in  (select sal from emp where deptno=30)
    (36) 查询工资高于30部门工作的所有员工的工资的员工姓名和工资。
    select ename,sal from emp where sal  > all (select sal from emp where deptno=30)
    select ename,sal from emp where sal  >(select max(sal) from emp where deptno=30)
    (37) 查询每个部门中的员工数量、平均工资和平均工作年限。
    select deptno,count(empno),avg(sal),avg(trunc((sysdate-hiredate)/365))
    from emp group by deptno
    (38) 查询从事同一种工作但不属于同一部门的员工信息。
    select distinct a.*  from emp a,emp b where a.job=b.job and a.deptno!=b.deptno
    (39) 查询各个部门的详细信息以及部门人数、部门平均工资。
    select d.deptno,d.dname,d.loc,count(e.empno),avg(sal) from dept d,emp e where d.deptno=e.deptno
    group  by  d.deptno,d.dname,d.loc
    (40) 查询各种工作的最低工资。
    select job,min(sal) from emp group by job
    (41) 查询各个部门中不同工种的最高工资。
    select job,max(sal) from emp group by job
    (42) 查询10号部门员工及其领导的信息。
    select a.ename,b.ename as manager from emp a,emp b where a.mgr=b.empno and a.deptno=10
    (43) 查询各个部门的人数及平均工资。
    select deptno,count(*),avg(sal) from emp group by deptno
    (44) 查询工资为某个部门平均工资的员工的信息。
    select ename,sal from emp where sal in(select avg(sal) from emp group by deptno);
    (45) 查询工资高于本部门平均工资的员工的信息。
    select e1.ename,e1.job,e1.sal,e1.deptno from emp e1 where sal>(select avg(sal) from emp e2 where e1.deptno=e2.deptno);
    (46) 查询工资高于本部门平均工资的员工的信息及其部门的平均工资。
    select * from emp,(select avg(sal) sal,deptno from emp  group by deptno)avgsal where emp.deptno=avgsal.deptno and emp.sal>avgsal.sal;
    (47) 查询工资高于20号部门某个员工工资的员工的信息。
    select * from emp where sal>(select min(sal) from emp where deptno=20)
    (48) 统计各个工种的员工人数与平均工资。
    select job,count(empno),avg(sal) from emp group by job
    (49) 统计每个部门中各工种的人数与平均工资。
    select job,count(empno),avg(sal),deptno from emp group by deptno,job;
    (50) 查询工资、奖金与10号部门某员工工资、奖金都相同的员工的信息。
    select * from emp,(select sal,comm from emp where deptno=10) a where emp.sal=a.sal and emp.comm=a.comm;
    (51) 查询部门人数大于5的部门的员工信息。
    select * from emp where deptno in (select deptno from (select deptno,count(*) from emp group by deptno having count(*)>5))
    (52) 查询所有员工工资都大于2000的部门的信息。
    select * from dept where deptno in (select deptno from (select deptno,min(sal) from emp group by deptno having min(sal)>2000))
    --(53) 查询所有员工工资都大于2000的部门的信息及其员工信息。
    select * from emp,dept,((select deptno,min(sal) from emp group by deptno having min(sal)>2000)) a where dept.deptno=a.deptno and dept.deptno=emp.deptno;
    --(54) 查询所有员工工资都在2000~3000之间的部门的信息。
        select * from dept,(select deptno,min(sal),max(sal) from emp group by deptno having min(sal)>2000 and max(sal)<3000) a where dept.deptno=a.deptno;
    --(55) 查询有工资在2000~3000之间的员工所在部门的员工信息。
    select * from emp,(select * from emp where sal>2000 and sal<3000) a where emp.deptno=a.deptno;
    --(56) 查询每个员工的领导所在部门的信息。
    select * from dept where deptno in(select e2.deptno from emp e1,emp e2 where e1.empno=e2.mgr);
    --(57) 查询人数最多的部门信息。
    select * from (select d.* from dept d,(select deptno,count(*) enum from emp group by deptno) t where d.deptno=t.deptno order by t.enum  desc) where rownum=1

    select * from dept where deptno=(select a.deptno from(select count(empno) count,deptno from emp group by deptno) a where a.count=(select max(count(empno)) from emp group by 
    deptno));


    --(58) 查询30号部门中工资排序前3名的员工信息。
    select * from (select a.*,rownum as rn from emp a where deptno=30  order by sal) where  rn<=3


    --(59) 查询所有员工中工资排序在5到10名之间的员工信息。
    select * from (select a.*,rownum rn from (select * from emp order by sal) a ) where rn<=10 and rn>=5;
    --(60)向emp表中插入一条记录,员工号为1357,员工名字为oracle,工资为2050,部门号为20,入职日期为2002年5月10日。
    insert into emp(empno,ename,sal,deptno,hiredate) values(1357,'oracle',2050,20,to_date('2002-5-10','yyyy-mm-dd'))
    --(61) 向emp表中插入一个记录,其员工名为FAN ,员工号为8000,其他信息与SMITH员工的信息相同。
    insert into emp select 8000,'FAN',job,mgr,hiredate,sal,comm,deptno from emp where ename='SMITH'
    --(62) 将各部门员工的工资修改为该员工所在部门平均工资加1000。
    update emp a set sal=1000+(select avg(sal) from emp b group by deptno having a.deptno=b.deptno )

    来着 http://blog.csdn.net/zbdba/article/details/16806937

  • 相关阅读:
    编译nginx增加fair模块
    使用CentOS8来部署php7.4
    通过PHP代码将大量数据插入到Sqlite3
    不同程序语言处理加密算法的性能对比(PHP/Golang/NodeJS)
    CentOS8更换国内YUM源
    MySQL获取上月第一天、上月最后日、本月第一天、本月最后日的方法
    GO
    Go-数据类型以及变量,常量,函数,包的使用
    GO语言介绍以及开发环境配置
    利用python代码操作git
  • 原文地址:https://www.cnblogs.com/lanliying/p/3832930.html
Copyright © 2011-2022 走看看