oracle简单创建视图
1.平均薪水的等级最低的部门的部门名称:
select dname from ( select avg_sal, deptno, grade from (select avg(sal) avg_sal,deptno from emp group by deptno) join salgrade on avg_sal between salgrade.losal and salgrade.hisal ) t1 join dept on t1.deptno=dept.deptno where t1.grade= ( select min(grade) from ( select avg_sal, deptno, grade from (select avg(sal) avg_sal,deptno from emp group by deptno) join salgrade on avg_sal between salgrade.losal and salgrade.hisal ) )
对于比较繁琐的select语句,可以通过建立一个视图作为一张表,来简化sql语句,是语句的可读性更强一些:
2.创建视图:
create view v$_dept_avg_sal_info as
select avg_sal, deptno, grade from (select avg(sal) avg_sal,deptno from emp group by deptno)
join salgrade on avg_sal between salgrade.losal and salgrade.hisal
(解释:
create view 固定语句;
v$_dept_avg_sal_info 为视图名称;
as 固定语句;
select avg_sal, deptno, grade from (select avg(sal) avg_sal,deptno from emp group by deptno)
join salgrade on avg_sal between salgrade.losal and salgrade.hisal 要创建视图的sql数据
)
3.执行--》视图创建好了,之后上面比较繁琐的sql文可以写成如下的样子了:
select dname, t1.deptno, grade, avg_sal from
v$_dept_avg_sal_info t1
join dept on (t1.deptno=dept.deptno)
where t1.grade=
(
select min(grade) from v$_dept_avg_sal_info
)