ORACLE初学第四篇

一、分组函数

select job,count(ename) as c_e from emp group by job;
/*分组函数一般是联合聚合函数count来用的,上面的意思是:统计出emp下面不同工作的人数,显示出不同的工作以及其统计数*/

二、过滤函数

/*在上面的例子中加上过滤条件*/
select job,count(ename) as c_e from emp group by job having count(ename)>2;

三、排序函数

/*在上面的例子中在加上排序*/
select job,count(ename) as c_e from emp group by job having count(ename)>2 order by c_e desc;
//默认是asc进行排序的

四、子查询

select sum(sal) 
from emp e 
where sal>(select avg(sal) from emp);

select count(sal) 
from emp e 
where 
sal>(select avg(sal) from emp);

select job,count(sal) 
from emp e 
where sal>(select avg(sal) from emp) group by job;

select job,count(sal) as cou_e 
from emp 
where sal>(select avg(sal) from emp) 
group by job 
having count(ename)>=2 
order by cou_e desc;

select job,count(sal) as cou_e 
from emp e 
where sal>(select avg(sal) 
          from emp 
          sal>(select avg(sal) from emp where job='SALESMAN')) 
group by job 
having count(ename)>=2 
order by cou_e desc;
 //打印结果
JOB            COU_E
--------- ----------
ANALYST           2
MANAGER           2
//看起来上面的语句有些复杂,可能到时候我自己都不明白,所以我进行selectfromwhere每段进行换行的分开

五、联合查询

//去掉重复元素:并集
SQL> select * from a1
  2  union
  3  select * from a2;
//union all不去重复

//交集:intersect
select * from a1 intersect select * from a2;

//差集:minus(结果查找到的是只在第一个集合中二不在第二个集合中的元素)
select * from a1 minus select * from a2;

//表关联查询(查询到的是每个表中的dept都不为空的数据)
select * from emp e,dept d where e.deptno=d.deptno;
//在ORACLE中效率更高的语句
select * from emp e inner join dept d on e.deptno=d.deptno;
//左链接left join(以左边的表数据为准,在右边有关联的数据链接起来,没有的就为空)
select * from emp e left join dept d on e.deptno=d.deptno;
//右关联
select * from emp e right join dept d on e.deptno=d.deptno;
/*注意:实际开发中用左外联接*/

六、EXISTS函数

select * from T_USER t where not exists(select * from t_user where t.id>=3);
//exists是用来判断是否存在的,当exists(查询)中的查询存在结果时则返回真,否则返回假。not exists则相反。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值