oracle 中SQL查询相关知识点
1 --SQL查询 2 --1,笛卡尔积 3 --多张表相乘,需要显示所有的数据 4 5 ----内连接:笛卡尔积 数据都是有关联的 6 select * from 左表 , 右表 where 左表.主键=右表.外键; 7 select * from 左表 inner join 右表 on 左表.主键=右表.外键 8 9 ----左连接:笛卡尔积 左边表中的数据都要显示 10 select * from 左表 left join 右表 on 左表.主键=右表.外键; 11 select * from 左表,右表 where 左表.主键=右表.外键(+); 12 13 ----右连接:笛卡尔积 右边表中的数据都要显示 14 select * from 左表 right join 右表 on 左表.主键=右表.外键; 15 select * from 左表,右表 where 左表.主键(+)=右表.外键; 16 17 18 --2, 19 --DDL 数据定义语言--对象操作 create,drop,alter 20 --DML 数据操作语言--数据操作 select,insert,update,delete 21 --DCL 数据控制语言--对象权限 grant..to... revoke...from ... 22 23 24 --3,语法--- 25 --3.1、排序 order by 升序 asc (默认) 降序desc 26 ----先按部门号升序来排序,再按照工资的降序来排序 27 select * from emp order by deptno asc ,sal desc ; 28 29 ----distinct 去掉重复数据 30 select distinct(deptno) from emp; 31 ----group by 分组 一般情况和聚合函数一起用 32 ------统计每个部门发了多少工资 33 select dept.dname,nvl(sum(emp.sal),0) from dept left join emp on dept.deptno=emp.deptno group by dept.dname; 34 select dept.dname,count(emp.ename) from dept left join emp on dept.deptno=emp.deptno group by dept.dname; 35 36 ------请找出 每个月工资超过10000元的部门 37 select dept.dname,nvl(sum(emp.sal),0) from dept left join emp on dept.deptno=emp.deptno group by dept.dname having nvl(sum(emp.sal),0)>10000; 38 ----聚合函数作为条件的时候不能放在where 后面,只能用 having 代替 where ,放到having 后面 39 40 --3.2、模糊查询 下划线 任意一个字符 %任意多个字符 41 --like ‘a__‘ 42 --like ‘%a%‘ 43 --like ‘_a_‘ 44 ----查询名字中有A的人 45 select * from emp where ename like ‘%A%‘; 46 ----查询名字以A开头的人 47 select * from emp where ename like ‘A%‘ 48 ----查询名字中第二个字符是A的所有员工 49 select * from emp where ename like ‘_A%‘ 50 ----查询编号是7900的员工 51 select * from emp where empno=7900; 52 select * from emp where empno like 7900; 53 ----子查询 in () 在什么区间里面 54 select * from emp where deptno in ( select deptno from dept where dname=‘RESEARCH‘ or dname=‘SALES‘ ); 55 56 --4, 57 ----4.1、exists() 小括号中如果有数据结果就执行 exists 前面的内容返回数据和结构 ,否则值返回表结构 58 select * from emp where exists (select * from emp where empno=7900) 59 60 ----4.2、not exists ()小括号中如果有数据结果就返回表结构,没有数据就返回表结构和数据 61 select * from emp where not exists (select * from emp where empno=0); 62 63 ----4.3、union ---合并数据 64 --将部门的编号和名称 与 员工编号和名称显示到一起去 65 select DEPTNO as no, DNAME as name from dept 66 union 67 select EMPNO as no , ENAME as name from emp; 68 69 --5,复制新的表和数据 70 create table new_emp as select ename,sal from emp; 71 ----只是复制新的表,加一个无数据的条件就可以了 72 create table new_emp as select ename,sal from emp where empno=0; 73 74 ----复制数据 75 insert into A (a,b) select a,b from B; 76 insert into new_emp select ename,sal from emp;
原文:http://www.cnblogs.com/bigerf/p/6497708.html