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

    --1、使用基本查询语句.
    --(1)查询DEPT表显示所有部门名称.
    select * from dept;
    --(2)查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行,并指定列别名为"年收入"。(NVL(comm,0) comm取空值时用0替代)
    select ename,12*(sal+nvl(comm,0)) "年收入" from emp;
    --(3)查询显示不存在雇员的所有部门号。
    select d.deptno from dept d where d.deptno not in (select distinct deptno from emp)

    --2、限制查询数据
    --(1)查询EMP表显示工资超过2850的雇员姓名和工资。
    select ename,sal from emp where sal>2850;
    --(2)查询EMP表显示工资不在1500~2850之间的所有雇员及工资。
    select ename,sal from emp where sal not between 1500 and 2850;
    --(3)查询EMP表显示代码为7566的雇员姓名及所在部门代码。
    select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno and empno=7566 ;
    select * from emp e,dept d where e.deptno=d.deptno;
    --(4)查询EMP表显示部门10和30中工资超过1500的雇员名及工资。
    select ename,sal from emp where deptno in(10,30) and sal>1500;
    --(5)查询EMP表显示第2个字符为"A"的所有雇员名其工资。
    select ename,sal from emp where ename like '_A%';
    --(6)查询EMP表显示补助非空的所有雇员名及其补助。
    select ename,comm from emp where comm is not null;
    --3、排序数据
    --(1)查询EMP表显示所有雇员名、工资、雇佣日期,并以雇员名的升序进行排序。
    select ename,sal,hiredate from emp order by ename asc;
    --(2)查询EMP表显示在1981年2月1日到1981年5月1日之间雇佣的雇员名、岗位及雇佣日期,并以雇佣日期进行排序。
    select ename, job, hiredate
    from emp
    where hiredate between to_date('1981/2/1', 'yyyy/mm/dd') and
    to_date('1981/5/1', 'yyyy/mm/dd')
    order by hiredate asc;
    --(3)查询EMP表显示获得补助的所有雇员名、工资及补助,并以工资升序和补助降序排序。
    select ename,sal,comm from emp where comm is not null order by sal asc,comm desc;

    --第二部分:
    --1.列出至少有一个雇员的所有部门。
    select d.dname from (select distinct deptno from emp) e,dept d where e.deptno = d.deptno;
    --2.列出薪金比“SMITH”多的所有雇员。
    select ename from emp where sal > (select sal from emp where ename='SMITH');
    --3.列出所有雇员的姓名及其上级的姓名。
    select e1.ename,e2.ename from emp e1,emp e2 where e1.mgr = e2.empno;
    --4.列出入职日期早于其直接上级的所有雇员。
    select e1.ename,e1.hiredate,e2.hiredate from emp e1,emp e2 where e1.mgr=e2.empno and e1.hiredate < e2.hiredate;
    --5.列出部门名称和这些部门的雇员,同时列出那些没有雇员的部门。
    select d.dname,e.ename from emp e right outer join dept d on e.deptno = d.deptno;
    --6.列出所有“CLERK”(办事员)的姓名及其部门名称。
    select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno and e.job = 'CLERK';
    --7.列出各种岗位的最低薪金,并显示最低薪金大于1500所有工作岗位及其最低薪资。
    select e.job,min(sal) as msal from emp e group by job having min(sal) > 1500;
    --8.列出从事“SALES”(销售)工作的雇员的姓名,假定不知道销售部的部门编号。
    select ename from emp where deptno in (select deptno from dept where dname='SALES');
    --select e.ename,d.dname,d.deptno from emp e,dept d where e.deptno=d.deptno and d.dname='SALES'
    --9.列出薪金高于公司平均的所有雇员。
    select ename,sal from emp where sal > (select avg(sal) from emp);
    --10.列出与“SCOTT”从事相同工作的所有雇员。
    select ename from emp where job in(select job from emp where ename='SCOTT');
    --11.列出薪金等于在部门30工作的所有雇员的薪金的雇员的姓名和薪金。
    select ename,sal from emp where sal in (select sal from emp where deptno = 30);
    --12.列出薪金高于在部门30工作的所有雇员的薪金的雇员的姓名和薪金。
    select ename,sal from emp where sal > (select max(sal) from emp where deptno=30);
    select e.ename,e.sal from emp e,(select deptno,max(sal) msal from emp group by deptno having deptno=30) d where e.deptno!=d.deptno and e.sal>d.msal;

  • 相关阅读:
    使用Hugo框架搭建博客的过程
    使用Hugo框架搭建博客的过程
    使用Hugo框架搭建博客的过程
    Windows软件包管理工具:Scoop
    Centos8 安装ifconfig(net-tools.x86_64)
    Centos8 重启网卡方法
    使用Visual Studio 2019--调试汇编32位代码的详细步骤
    linux 三剑客之awk总结
    linux 三剑客之sed常用总结
    mysql数据库的笔记
  • 原文地址:https://www.cnblogs.com/cqming/p/10738209.html
Copyright © 2011-2022 走看看