SQL必须知道的基本表操作(三)查询操作

  1. 首先呢,是查询语句的格式  
  2.  --在sql中 各关键字出现的顺序  
  3.    --SELECT  
  4.    --FROM   
  5.    --WHERE  
  6.    --GROUP BY  
  7.    --HAVING  
  8.    --ORDER BY  
  9.   
  10. ---查询一张表中的所有数据  
  11. select * from emp;  
  12. --- 查询数据某几个字段的值 按照指定字段顺序来显示数据  
  13. select empno, ename from emp;  
  14. --- 去重复查询  
  15. select distinct deptno,job from emp;  
  16. ---  条件查询  查询内容区分大小写  
  17. select deptno,job,sal from emp where ename='SMITH';  
  18. -- 查询部门编号 10或者20 的所有员工  
  19. select * from emp where deptno=10 or deptno=20;  
  20. -- 查询薪水大于2000 小于2500  
  21.  select * from emp where sal >2000 and sal<2500  
  22.   
  23. --查询入职时间早于   
  24.  select * from emp where hiredate < '17-9月 -81';  
  25.   --模糊查询  %表示通配任意个字符  _表示通配一个字符  
  26. --查询名字中包含K字符的结果  
  27.   select * from emp where ename like '%K%'  
  28.  --查询第三个字母为o的员工  
  29.  select * from emp where ename like '__O%';  
  30.  --在数组里面出现过的数据都查询出来  
  31.   select * from emp where empno in (7369,7844,7521);  
  32. --显示没有上司的员工   
  33.    select * from emp where mgr is null;  
  34.  --使用别名查询 as可省略  
  35.    select empno as 编号,ename as 姓名, sal as 月薪 from emp where ename like 'S%';   
  36.  --使用别名查询 SMITH的编号 和年薪  
  37. select empno 编号,ename 姓名 ,sal*12 年薪 from emp where ename='SMITH';  
  38.   --查询工资大于500 或者工作为MANAGER 并且姓名首字母为J开头  
  39.   select * from emp where (sal>500 or  job='MANAGER'and ename like 'J%';  
  40.   --查询所有的数据,按照薪水从高到低依次排列   
  41.   select * from emp order by sal desc;  
  42.   --从低到高  
  43.    select * from emp order by sal asc;  
  44.      
  45.    --统计所有员工的编号姓名年薪(月薪+奖金)  
  46.    select empno 编号 ,ename 姓名,(sal+nvl(comm,0))*12 年薪 from emp;  
  47.    --根据带奖金的年薪 排序  
  48.    select empno 编号 ,ename 姓名,(sal)*12 年薪 from emp order by 年薪 desc;  
  49.    -- 分组函数 max min abg sum count   
  50.    --查询出工资月薪最高的员工的编号 姓名 月薪  
  51.    select empno,ename,sal from emp where sal=(select max(sal) 月薪 from emp);   
  52.     --查询出工资月薪最高和最低的员工的编号 姓名 月薪  
  53.       select empno,ename,sal from emp where sal=(select min(sal)  from emp) or sal=(select max(sal)  from emp);  
  54.    --计算有多少员工   
  55.    select count(empno) 员工数 from emp;  
  56.    --计算员工月工资薪水总数  
  57.    select sum(sal) 总工资 from emp;  
  58.    --计算员工平均工资  
  59.    select avg(sal) from emp;  
  60.      
  61.    --显示平均工资和最高工资   
  62.          select avg(sal) 平均工资,max(sal) 最高工资 from emp;  
  63.    --显示每个部门的平均工资和最高工资  
  64.    --分组的条件一定要查询出来  
  65.    select avg(sal) 平均工资 ,max (sal) 最高工资,deptno 部门 from emp group by deptno;  
  66.    --显示每个部门各种岗位的平均工资和最低工资  
  67.   select avg(sal) 平均工资 ,min (sal) 最低工资,deptno 部门,job 岗位 from emp group by deptno,job   
  68.   --显示平均工资低于2000的部门和它的平均工资o  
  69.    select avg(sal) 平均工资,deptno 部门 from emp group by deptno  having avg(sal)<2000;  
  70.    --显示平均工资高于2000的部门和它的平均工资(总裁办(部门编号10)不参与计算)  
  71.    select avg(sal) 平均工资,deptno 部门 from emp where deptno!=10 group by deptno  having avg(sal)>2000;  
  72.    ---group by 需要注意的是 在select中出现的字段 除了汇总字段以外,全部都要出现在group by里面   
  73.    --如 select avg(sal) 平均工资 ,sal 最低工资,deptno 部门,job 岗位 from emp group by deptno,job 会出现错误  
  74.    --因为 sal 出现在了select 中,确没有出现在group by 中 并且 sal不是汇总字段(使用了汇总函数的字段 ,如avg,max,min等)  
  75.   
  76. -------------------------------------多表查询---------------------------------------  
  77.    --查询部门表  
  78.    select * from dept;  
  79.    --显示雇员名,雇员工资 雇员部门名   雇员部门名在dept表中  
  80.    select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno;  
  81.    --显示雇员名 雇员工资 雇员工资级别  工资级别在salgrade表中  
  82.    select e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;  
  83.    --显示员工名,员工工资,员工工资级别 所在部门名字,并按照部门排序  
  84.    select e.ename ,e.sal,s.grade,d.dname from emp e,dept d ,salgrade s   
  85.     where e.deptno=d.deptno and( e.sal between s.losal and s.hisal) order by e.deptno ;  
  86.     
  87.    --查询比部门30所有员工工资高的员工的姓名 工资  
  88.    select ename,job,sal,deptno from emp where sal > all (select sal from emp where deptno=30);  
  89.    select ename,job,sal,deptno from emp where sal >  (select max(sal) from emp where deptno=30);  
  90. ---查询SMITH的上司   
  91.     --自连接查询   
  92.     select e1.ename,e2.ename 上司 from emp e1,emp e2 where e1.mgr=e2.empno and e1.ename='SMITH';   
  93.    --嵌套查询  
  94.    select ename from emp where empno=(select mgr from emp where ename='SMITH');   
  95.  --如果子查询返回多个结果,则应该使用in 或者 = any  
  96.   select ename,job,sal,deptno from emp where job in (select job from emp where deptno=10);  
  97.   select ename,job,sal,deptno from emp where job  = any (select job from emp where deptno=10);  
  98.      
  99.   --如何查询部分数据  
  100.   --rownum rowid  伪列  伪劣是根据查询结果动态生成的  
  101.    --分页查询 只显示前5条结果  
  102.     select rownum,ename ,job from emp where rownum <=5;  
  103.    --查询6-10条 不能用 select rownum,ename ,job from emp where rownum <=10 and rownum>=5;  
  104.    --而要    
  105.    select * from (select rownum r,ename,job from emp where rownum <=10) where r>=5;  
  106.    --合并查询   集合操作符 union   合并结果 去掉重复行  
  107.    select * from emp where job='SALESMAN'union select * from emp where sal>=1500;  
  108.    --union all 合并结果 保留重复行  
  109.    select * from emp where job='SALESMAN'union all select * from emp where sal>=1500;  
  110.    --取交集  intersect   
  111.    select * from emp where job='SALESMAN'intersect select * from emp where sal>=1500;  
  112.    --取差集 第一个集合中有第二个集合中没有的   minus  
  113.    select * from emp where job='SALESMAN' minus select * from emp where sal>=1500;  

转载于:https://my.oschina.net/huewenhua/blog/681523

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值