SQL(Oracle)

http://blog.csdn.net/winter13292/article/details/7011377

SQL 对大小写不敏感!

 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。

 eg:

SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Bush' OR Customer='Adams'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500

通过 SQL,您如何向 "Persons" 表插入新的记录?

您的回答:INSERT VALUES ('Jimmy', 'Jackson') INTO Persons

正确答案:INSERT INTO Persons VALUES ('Jimmy', 'Jackson')

通过 SQL,您如何向 "Persons" 表中的 "LastName" 列插入 "Wilson" ?

您的回答:INSERT INTO Persons ('Wilson') INTO LastName

正确答案:INSERT INTO Persons (LastName) VALUES ('Wilson')

Varchar2(支持各国语言)

字符串连接符:|| (注意if 和字符串连接。字符串中有单引号,则用2个单引号。)

select distinct deptno from emp;          去掉重复值

select distinct deptno,job from emp;     去掉组合重复的

不等于<>

between 800 and 1500; 包含800 1500

is (not) null;  选空值

where sal (not)in (800,1500,2000);

like             %通配符         _一个字符      转义字符

like'%$%'escape '$';              自己定义转义字符为$

order by  ...... desc ,asc

e.g. select ename,sal,deptno from emp order  by deptno asc,ename desc;        

select ename,sal*12 annual_sal from empwhere ename not like'_A%' and sal>800

order by sal desc;

function

lower() upper()

substr(name,1,3)  第一个字符开始截,截3个字符

chr(65) : A   ascii('A')  :65

round(32.233)  四舍五入  round(23.611,1):23.6

select to_char(sal,'$99,999.9999') from emp;    $2,222.0000

select to_char(sal,'l00000.0000') from emp;             0代表一位数字,该位置没有数字强制显示 02222.0000

日期转换:select to_char (hiredate,'YYYY-MM-DD HH:MI:SS') FROM emp;

select ename,hiredate from emp where hiredate >to_date('1981-2-20 12:34:56' ,'YYYY-MM-DD HH24:MI:SS');  (注意hiredate是其他的格式)

select sal from emp where sal >  to_number('$1,250.00','$9,999.99');

NVL select ename,sal*12 +nvl(comm,0) from emp;            如果comm值为null 用0替换

 组函数 max()  min() avg() count(distinct 。。)  sum()

group by  groupby deptno,job;  根据两个的组合分组

Ename 有可能很多个 max 只能有一个输出 匹配不起来.出现在select 类表里的字段 没有出现在主函数里必须穿现在group by 里

Where     对单条语句进行过滤,WHERE 关键字无法与合计函数一起使用  ,Having对分组进行限制

总结执行顺序:

select avg (sal) from emp

where sal>1200

group by deptno

having avg(sal) >1500

order byavg(sal)  desc;

等值连接

select ename,dname from emp join depton(emp.deptno=dept.deptno);      //(using deptno    不推荐)

select ename,dname from emp,dept where emp. deptno = dept.deptno;(以前版本)  注意:现在的where 只写数据过滤条件

非等值连接

select ename, grade from emp e join salgrade s on (e.sal between s.losal and s.hisal);

三表查找

select ename, dname, grade from emp e 

join dept d on(e.deptno = d.deptno) 

join salgrade s on (e.sal between s.losaland s.hisal) 

where ename not like '_A%'; 

select e1.ename,e2.ename from emp e1 leftjoin emp e2 on (e1.mgr =e2.empno);

左外连接把左边没有满足条件的数据也取出来(右  全      RIGHT FULL) 

查找雇员的经理人是谁                     自连接 为表起2个名 当成连个表用

select e1.ename,e2.ename from emp e1,emp e2where e1.mgr =e2.empno;

 面试题 不用组函数max()求薪水的最大值       自连接

select sal from emp where sal not in(selectdistinct e1.sal from emp e1 join emp e2 on(e1.sal < e2.sal));

原文地址:https://www.cnblogs.com/kydnn/p/5026731.html