SQL练习(5)(2020-04-12)

鉴于我写sql语句的能力有些差,在今后的一段时间内会时常做一些sql语句的练习。先给自己定一个小目标:先将网上能找到的sql练习都做一遍。

——————————————————————————————————————————————————————

这里使用的是oracle自带的几张表,在scott用户下面。

放开scott用户方法见:https://blog.csdn.net/qq_26896085/article/details/105342212

--查询员工编号,员工姓名,经理的编号,经理的姓名
select e1.empno, e1.ename, e2.empno, e2.ename from emp e1, emp e2 where e1.mgr = e2.empno;
--查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名
select e1.empno, e1.ename, dname, e2.empno, e2.ename from emp e1, emp e2, dept where e1.mgr = e2.empno and e1.deptno = dept.deptno order by dname;
--查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称
select e1.empno, e1.ename, d1.dname, salgrade.grade, e1.mgr, e2.ename, d2.dname from emp e1, emp e2, dept d1, dept d2, salgrade 
where e1.mgr = e2.empno and e1.deptno = d1.deptno and e2.deptno = d2.deptno and e1.sal between salgrade.losal and salgrade.hisal;
--查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
select e1.empno, e1.ename, d1.dname, s1.grade, e1.mgr, e2.ename, d2.dname, s2.grade from emp e1, emp e2, dept d1, dept d2, salgrade s1, salgrade s2
where e1.mgr = e2.empno and e1.deptno = d1.deptno and e2.deptno = d2.deptno and e1.sal between s1.losal and s1.hisal and e2.sal between s2.losal and s2.hisal;
--查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的 部门名称,经理的工资等级 --将工资等级 1,2,3,4 显示成 one, two, three, four, five
select e1.empno, e1.ename, d1.dname, 
       decode(s1.grade, 1, 'one', 2, 'tow', 3, 'three', 4, 'four', 'five') "grade1",
       e1.mgr, e2.ename, d2.dname, s2.grade,
       decode(s2.grade, 1, 'one', 2, 'tow', 3, 'three', 4, 'four', 'five') "grade2"
from emp e1, emp e2, dept d1, dept d2, salgrade s1, salgrade s2
where e1.mgr = e2.empno and e1.deptno = d1.deptno and e2.deptno = d2.deptno and e1.sal between s1.losal and s1.hisal and e2.sal between s2.losal and s2.hisal;
--
select e1.empno, e1.ename, d1.dname, 
       case s1.grade
         when 1 then 'one'
         when 2 then 'two'
         when 3 then 'three'
         when 4 then 'four'
         when 5 then 'five'
       end "grade1",
       e1.mgr, e2.ename, d2.dname, s2.grade,
       case s2.grade
         when 1 then 'one'
         when 2 then 'two'
         when 3 then 'three'
         when 4 then 'four'
         when 5 then 'five'
       end "grade1"
from emp e1, emp e2, dept d1, dept d2, salgrade s1, salgrade s2
where e1.mgr = e2.empno and e1.deptno = d1.deptno and e2.deptno = d2.deptno and e1.sal between s1.losal and s1.hisal and e2.sal between s2.losal and s2.hisal;
--查询员工姓名和员工部门所处的位置
select ename, loc from emp, dept where emp.deptno = dept.deptno;
select ename, loc from emp join dept on emp.deptno = dept.deptno;

猜你喜欢

转载自blog.csdn.net/qq_26896085/article/details/105479741