oracle sql select语法格式

来源:互联网 发布:plsql如何导出表数据 编辑:程序博客网 时间:2024/06/06 18:11
语法格式
select column, group_function(column)
from table
[where condition]
[group by  group_by_expression]
[having group_condition]
[order by column];

执行过程:from--where -- group  by– having– select-- order  by

对分组查询的结果进行过滤,要使用having从句。
having从句过滤分组后的结果,它只能出现在group by从句之后,而where从句要出现在group by从句之前。
where过滤行,having过滤分组。having支持所有where操作符。

eg:

select deptno, job, avg(sal)  

from emp

group by deptno,job  

having avg(sal) > 1200  

order by deptno,job;


、SQL92的多表连接查询

语法规则

SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column1 = table2.column2;

特点:

在 WHERE 子句中写入连接条件

当多个表中有重名列时,必须在列的名字前加上表名作为前缀

连接的类型:

等值连接 -- Equijoin

非等值连接 -- Non-equijoin

外连接 -- Outer join

左外连接

右外连接

自连接 -- Self join

1.1笛卡尔积

select * from dept;//4条记录

select * from emp; ;//14条记录

select * from dept,emp; ;//4*14=56条记录

小结:

检索出的行的数目将是第一个表中的行数乘以第二个表中的行数

检索出的列的数目将是第一个表中的列数加上第二个表中的列数

应该保证所有联结都有where子句,不然数据库返回比想要的数据多得多的数据 

1.2:等值查询

select * from dept,emp where dept.deptno=emp.deptno;

select * from dept d,emp e where d.deptno=e.deptno;

select d.deptno,dname,loc,empno,ename,job from dept d,emp e where d.deptno=e.deptno;

select d.deptno,dname,loc,empno,ename,job from dept d,emp e where d.deptno=e.deptno and d.deptno=10

select d.deptno,dname,loc,empno,ename,job from dept d,emp e where d.deptno=e.deptno and loc='DALLAS';

当被连接的多个表中存在同名字段时,须在该字段前加上"表名."前缀

可使用AND 操作符增加查询条件;

使用表别名可以简化查询

使用表名(表别名)前缀可提高查询效率;

1.3:非等值查询

查询员工的工资等级

select  empno,ename,job,sal,grade 

from emp e,salgrade s

where e.sal<s.hisal and e.sal>s.losal;

select  empno,ename,job,sal,grade 

from emp e,salgrade s

where e.sal<s.hisal and e.sal>s.losal and e.job='MANAGER';

1.4:外连接

使用外连接可以看到参与连接的某一方不满足连接条件的记录,而不仅仅是满足连接条件的数据。

外连接运算符为(+)

外连接分为左外连接和右外连接两种

左外连接显示左边表的全部行

SELECT table.column, table.column

FROM table1, table2

WHERE table1.column = table2.column(+);

右外连接显示右边表的全部行

SELECT table.column, table.column

FROM table1, table2

WHERE table1.column(+) = table2.column;

1.5:自连接

将一个表当两个表使用

使用举例:查询每个员工的工号、姓名、经理姓名

select e1.empno,e1.ename,e1.job,e2.ename 

from emp e1 ,emp e2

where e1.mgr=e2.empno

order by e1.empno;

1.6  更多表的连接查询

为了连接n个表,至少需要n-1个连接条件。

SQL92的语法规则的缺点:

语句过滤条件和表连接的条件都放到了where子句中 。

当条件过多时,连接条件多,过滤条件多时,就容易造成混淆



二、SQL99的多表连接查询

SQL99修正了整个缺点,把连接条件,过滤条件分开来,包括以下新的TABLE JOIN的句法结构:

交叉连接(Cross join)

自然连接(Natural join)

使用Using子句建立连接

使用On子句建立连接

外连接( Outer join )

左外连接

右外连接

全外连接

SQL1999规范中规定的连接查询语法

select 字段列表

from table1

[cross join table2] |                                    //1:交叉连接

[natural join table2] |                                  //2:自然连接

[join table2 using (字段名)] |                      //3:using子句

[join table2 on (table1.column_name = table2.column_name)] | //4:on子句

[(left | right | full outer) join table2on (table1.column_name = table2.column_name)]; //5:左/右/满外连接

2.1:交叉连接

Cross join产生了一个笛卡尔积,其效果等同于在两个表进行连接时使用WHERE子句限定连接条件;

可以使用where条件从笛卡尔集中选出满足条件的记录。

用法举例

select dept.deptno,dname,ename,job 

from dept cross join emp; 

等价于

select dept.deptno,dname,ename,job  

from dept,emp; 

2.2:自然连接

Natural join基于两个表中的全部同名列建立连接

从两个表中选出同名列的值均对应相等的所有行

如果两个表中同名列的数据类型不同,则出错

不允许在参照列上使用表名或者别名作为前缀

自然连接的结果不保留重复的属性

举例:

select empno, ename, sal, deptno, dname

from emp natural join dept

where deptno = 10;

2.3:Using子句

如果不希望参照被连接表的所有同名列进行等值连接,自然连接将无法满足要求,可以在连接时使用USING子句来设置用于等值连接的列(参照列)名。

using子句引用的列在sql任何地方不能使用表名或者别名做前缀

举例:

select e.ename,e.ename,e.sal,deptno,d. dname

from emp e join dept d 

using(deptno)

where deptno=20

2.4:On子句

自然连接的条件是基于表中所有同名列的等值连接

为了设置任意的连接条件或者指定连接的列,需要使用ON子句

连接条件与其它的查询条件分开书写

使用ON 子句使查询语句更容易理解

select ename,dname 

from emp join  dept on   emp.deptno=dept.deptno 

where  emp.deptno=30;

select empno, ename, sal, emp.deptno, dname

from emp join dept

on (emp.deptno = dept.deptno and job='SALESMAN);

2.5:外连接

左外连接

两个表在连接过程中除返回满足连接条件的行以外,还返回左表中不满足条件的行,这种连接称为左外联接。

右外连接

两个表在连接过程中除返回满足连接条件的行以外,还返回右表中不满足条件的行,这种连接称为右外联接。

全外连接(满外连接)

两个表在连接过程中除返回满足连接条件的行以外,还返回两个表中不满足条件的所有行,这种连接称为满外联接。

内连接:在SQL99规范中,内连接只返回满足连接条件的数据。

外连接举例

左外连接

select deptno, dname,empno,ename,job

from dept left outer join emp

using(deptno);

右外连接

select deptno, dname,empno,ename,job

from dept right join emp

using(deptno);

满外连接

select deptno, dname,empno,ename,job

from dept full join emp

using(deptno);

原创粉丝点击