查询语句
1.
(1)
(2)
(3)
(4)
(5)
2.
(1)
3.
select distinct
4.
5.
6.
具体用法:select
(1)
(2)
7.
(1)
select * from 表名 where
(2)
第一种:select * from
第二种:select
8.
%表示0到多个字符
_表示1个任意字符
如何显示首字母为s的员工姓名和工资:
如何显示第三个字符为大写O的所有员工姓名和工资:
9.
如何显示enpno为123.456.789..的雇员情况:
第一种:select * from
第二种:select * from
10.
如何显示没有上级的雇员情况:
11.
查询工资高于500或是岗位为上级的雇员,同时还要满足他们的姓名收字母为大写的J:
12.
order
如何按照工资的从低到高的顺序显示雇员的信息:
13.
14.
select
select
15.
select
注意:avg不会把null进行统计,如果你希望为空值也需要计算,则应该泽阳做:
select sun(工资列)/count(*)from表名
count(*)也可以对字段进行统计cout(字段名)
16.
select
17.
18.
select deptno,avg(sal),max(sal),job from emp group by deptno,job order by deptno;
19.
(1)
(2)
(3)
1.
select emp.ename,emp.sal,dept.dname from emp,dept where emp.deptno=dept.deptno;
2.
避免出现笛卡尔集:
多表查询的条件是至少不能少于表的个数-1
3.
SQL> select dept.deptno,dept.dname,emp.ename,emp.sal from dept,emp where emp.deptno=dept.deptno and dept.deptno=10;
4.
SQL> select emp.ename,emp.sal,grade from emp,salgrade where emp.sal between salg
and salgrade.hisal;
5.
如果两个表的列同名,则需要加表名区分,否则可以不加
注意:建议在多表查询的时候,使用别名:
SQL> select e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.lo
isal;
6.
SQL> select ename,emp.sal,dept.dname from emp,dept where emp.deptno=dept.deptno
pt.dname;
7.
显示个个员工的姓名和他的上级领导姓名
解题思路:把emp表看做两张表,一张是员工表,一张是上级表:
select worker.ename,boss.mgr from emp worker,emp boss where worker.empno=boss.mgr;
如果希望把没有上级的员工显示出来,则需要外连接
8.
如何显示工资比部门30的任意一个工资高的员工的姓名、工资和部门号
select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=30);
第二种方式:select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);
9.
查询出与smith的部门和岗位完全相同的所有雇员
select
10.
select distinct dept.dname
11.
select ename,(sal+nvl(comm,0))*13 年收入 from emp;
12.
先查询出各个部门的平均工资:select deptno,avg(sal) from emp group by deptno;
把上面的查询结果当成一个临时表:
SQL> select emp.deptno,emp.ename from emp,(select deptno mydp,avg(sal) myavg from emp by deptno) where emp.sal>myavg and emp.deptno=mydp order by emp.deptno;
13.
SQL> select* from emp,(select deptno dpt,max(sal) height from emp group by deptno) where emp.sal=height and emp.deptno=dpt;
14.
SQL>select dept.deptno,dept.dname,mycount from dept,(select deptno,count(*)mycount from emp group by deptno) mytable where dept.deptno=mytable.deptno(+);+号表示外连
分页查询
上面的sql语句相当于一个分页模板:6代表取到第几条;4代表从第几条开始去取
如果西需要针对不同的情况分页,请在最内层进行处理,包括多表
查看分页查询效率:
模拟10万的一个表-》测试
create table mytest as select empno,ename,sal,comm,deptno from emp;
自我复制:
insert into mytest(empno,ename,sal,comm,depto)select empno,ename,sal,comm,depot from mytest;
本语法:select
举例:显示出员工信息和部门名称:
select emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno;
等价于:
select emp.ename,dept.dname from emp inner join dept on emp.deptno=dept.deptno;
外连接
外连接分为3中:左外连,右外连,完全外连。
左外连:select stu.id,stu.name,score.grade from stu left join score on stu.id=score.id;
如何判断一张表是左边:如果在left join左边,就是左表
另外一种写法:select stu.id,stu.name,score.grade from stu ,score where stu.id=score.id(+);
右外连:
显示所有成绩,如果没有名字与id也把成绩显示出来:
select stu.id,stu.name,score.grade from stu right join score on stu.id=score.id;
也可以这样写:
select stu.id,score.grade from stu,score where stu.id(+)=score.id;
说明:右外连,指的是右边的表如果没有和左边的表任何一条记录匹配,也要被选中;
全外连:不管有没有匹配上,都显示出来:
select stu.id,stu.name,score.grade from stu full outer join score on stu.id=score.id;
列出部门名称和这些部门的员工信息,并且同时列出那些没有员工的部门:
select dept.dname,emp.ename,emp.job,emp.mgr,emp.deptno from emp,dept where emp.deptno(+)=dept.deptno;
列出薪资大于1500的各种工作:
select distinct emp.job from emp,(select min(sal) mymin,job from emp group by job)where mymin>1500;
查询出与scott从事相同工作的员工信息:
select emp.ename,emp.job from emp,(select job scottjob from emp where ename='SCOTT')where emp.job=scottjob;