替代空函数:nvl(列,替换值)
易错点:容易把替换值给忘了写。多数值判断函数:decode(数值|列,判断值1,显示值1,判断值2,显示值2,判断值3,显示值3……,默认值)
易错点:容易在默认值前面加上default,实际上不要加,直接写默认值就行了。『求出男生人数和女生人数:select sum(decode(性别,男,1,0)) 男,sum(decode(性别,女,1,0)) 女 from ***』
题目:以年月日的方式显示所有员工的服务年限。
select trunc(months_between(sysdate,hiredate)/12) year,trunc(mod(months_between(sysdate,hiredate),12)) month,
trunc(sysdate-add_months(hiredate,trunc(months_between(sysdate,hiredate)))) day
from emp;
sql表连接:
1,cross join 产生笛卡儿积
2,natural join 自动匹配关联条件,消除笛卡儿积
3,join……using字段,自己指定一个消除笛卡儿积的关联字段
4,join……using(条件) 自己指定一个消除笛卡尔积的关联条件
外连接:
左外连接left outer join……using()
右外连接right outer join……using()
全外连接full outer join……using()
any和all的区别:
ang表示只要满足条件里面的任意一个就行了,而all表示要满足条件里面的所有。
『例子:
select * from emp
where sal>all(select sal from emp where deptno=30);
select * from emp
where sal>any(select sal from emp where deptno=30);
』
分页查询语句:
注意点:子查询中的rownum一定要替换名称,不然在主查询语句中依然被当作rownum使用
select*from(
select rownum rn,ename,job,sal
from emp
where rownum<=currentPage*linesize)
where rn>(currentPage-1)*linesize;