oracle query 10 questions

- 1, query the Employees table employee with the highest salary employee number, employee name, salary and department number.

select empno,ename,sal,deptno from emp where sal=(select max(sal) from emp);

--2, salary greater than 1200 employees, grouped by department number, grouped average salary must be greater than 1500, the average level of each query packets, arranged in reverse order of wages.

select deptno,avg(sal) from emp where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;

--3, query each employee in the department and their names, sorted by employee name, value required to display the third record Article

--     rownum是伪列,rownum>2,没有查询到任何记录。
--    因为rownum总是从1开始的,第一条不满足去掉的话,第二条的rownum 又成了1。依此类推,所以永远没有满足条件的记录。
--  可以这样理解:rownum是一个序列,是Oracle数据库从数据文件或缓冲区中读取数据的顺序
select * from
(select emp.ename,dname,rownum rn from emp,dept where emp.deptno=dept.deptno and rownum<=6 order by emp.ename )--必须是内小于外大于
where rn>=3;
-- 内连接 只返回两个表中连接字段相等的行。
select ename,dname from emp inner join dept on emp.deptno = dept.deptno;
-- 第二种方式
select * from
(select emp.ename,dname,rownum rn from emp,dept where emp.deptno=dept.deptno order by emp.ename )
where rn between 3 and 6;

-- 第三种
select * from(
       select t.*, rownum rn from(
             select ename,dname from emp inner join dept on emp.deptno = dept.deptno order by emp.ename
       ) t where rownum<=6
) where rn>=3;

--4, the query level for each employee names and salaries is located.

--第一种
select ename,sal,grade from emp,salgrade where sal between losal and hisal;

--第二种 内连接
select ename,sal,grade from emp inner join salgrade on sal between losal and hisal;

--5, the name of the employee name query letter is not a second employee, the group name is located, where the level of wages

--第一种
--用substr分割截取字符串
select ename,dname,grade from emp,dept,salgrade
 where sal between losal and hisal and emp.deptno=dept.deptno and substr(ename,2,1) !='a' and substr(ename,2,1) !='A';

 --第二种
 --内连接 模糊查询,第一个下划线代表第一个字符
 select ename,dname,grade 
 from emp inner join dept on dept.deptno=emp.deptno
 inner join salgrade on sal between losal and hisal
 where ename not like '_A%';

--6, query the name of each employee and their manager

--第一种 使用自连接
select e1.ename empname,e2.ename mgrname from emp e1,emp e2
where e1.mgr=e2.empno;
--第二种 自连接与内连接
select e1.ename empname,e2.ename mgrname
from emp e1 inner join emp e2 
on e1.mgr=e2.empno;

--7, query each employee and the names of their managers (including the boss itself (no manager above him));

--左连接,左表中所有记录()以及右表中连接字段相等的记录
select e1.ename empname,e2.ename mgrname from emp e1 left join emp e2 on e1.mgr=e2.empno;

--8 query department where the department name and the name of each employee (not including employees of the department)

--右连接,油表中所有记录()以及右表中连接字段相等的记录
select ename,dname from emp right join dept on emp.deptno=dept.deptno;

--9, query each department of the wages in the highest name, salary and department number.

--把查询变成一个子表
--group by要包含所有选择的字段(除了函数的),所以先查询出每个部门最高工资,然后和原始表关联,就可以查出部门最高工资者的所有信息
select ename,sal,emp.deptno from emp
inner join
(select deptno,max(sal) msal from emp group by deptno) maxsalresult
on maxsalresult.deptno=emp.deptno and sal=maxsalresult.msal;

--10 query level for each sector where the average wage

select deptno,grade,avgsalresult.avgsal avragesal 
from salgrade inner join 
(select avg(sal) avgsal,deptno from emp group by deptno) avgsalresult
on avgsal between losal and hisal;

Guess you like

Origin www.cnblogs.com/Jotal/p/11356202.html