首页 > 代码库 > Orcla 数据库复习2 --子查询和表连接

Orcla 数据库复习2 --子查询和表连接

 子查询和表连接
 ①.查询挣钱最多的人的名字
 SELECT ename,sal FROM emp
 WHERE sal=(SELECT MAX(sal) FROM emp);
 ②.查询有哪些人的工资位于所有人的平均工资之上
 SELECT ename,sal FROM emp
 WHERE sal > (SELECT AVG(sal) FROM emp);
 ③.求部门中哪些人的薪水最高:
 SELECT ename,sal FROM emp
 JOIN (SELECT MAX(sal) max_sal,deptno FROM emp GROUP BY deptno) t
 ON emp.sal =t.max_sal AND emp.deptno = t.deptno;
④.求出员工的名字和他的经理人姓名
  SELECT  e1.ename, e2.ename FROM
  emp e1 JOIN emp e2
  ON e1.mgr = e2.empno;
 ⑤.查询出每个人的薪水等级
  SELECT ename,grade FROM emp e JOIN salgrade s
  ON  (e.sal BETWEEN s.losal AND s.hisal);
 ⑥查询出每个人的部门名称和薪水等级
 SELECT ename,dname,grade FROM
 emp e JOIN dept d ON (e.deptno =d.deptno)
 JOIN salgrade s ON (e.sal BETWEEN s.losal AND s.hisal)
 ⑦(左外连接) 查询出雇员的名字和他经理人的名字
 SELECT e1.ename, e2.ename FROM
 emp e1 LEFT JOIN emp e2
 ON e1.mgr = e2.empno;
⑧(右外连接) 查询出所有雇员的部门名称
 SELECT ename ,dname FROM
 emp e RIGHT JOIN dept d
 ON e.deptno = d.deptno;
 ⑨(全外连接) 查询出所有雇员的部门名称
 SELECT ename ,dname FROM
 emp e FULL JOIN dept d
 ON e.deptno = d.deptno;
 ⑩求部门平均薪水的等级
 SELECT deptno,avg_sal,grade FROM
 (SELECT deptno,AVG(sal) avg_sal FROM emp GROUP BY deptno) t
 JOIN salgrade s
 ON t.avg_sal BETWEEN s.losal AND s.hisal;
--求部门平均的薪水等级
  SELECT deptno,AVG(grade) FROM
 (SELECT deptno,ename,grade FROM emp JOIN salgrade s ON
  emp.sal BETWEEN s.losal AND s.hisal) t
  GROUP BY deptno;
--求雇员中哪些人是经理人
 SELECT  ename FROM emp
 WHERE  empno  in(SELECT DISTINCT mgr FROM emp);
--不准用组函数,求薪水的最高值
  SELECT sal FROM emp WHERE sal NOT IN
 (SELECT DISTINCT  e1.sal FROM emp e1  JOIN  emp e2
 ON e1.sal< e2.sal);
--平均薪水最高的部门的部门编号
SELECT deptno ,avg_sal FROM
( SELECT deptno, AVG(sal) avg_sal FROM emp GROUP BY deptno);
WHERE avg_sal =
(SELECT MAX(avg_sal) FROM
( SELECT deptno, AVG(sal) avg_sal FROM emp GROUP BY deptno));
--平均薪水最高的部门的部门名称
SELECT dname FROM dept WHERE deptno =
(SELECT deptno  FROM
        ( SELECT deptno, AVG(sal) avg_sal FROM emp GROUP BY deptno)
 WHERE avg_sal =
(SELECT MAX(avg_sal) FROM
        ( SELECT deptno, AVG(sal) avg_sal FROM emp GROUP BY deptno)
  )
);

--求平均薪水的等级最低的部门的部门名称
SELECT dept.dname,t1.deptno,grade,avg_sal FROM
(
SELECT deptno,grade,avg_sal FROM
(SELECT AVG(sal) avg_sal, deptno FROM emp GROUP BY deptno) t JOIN salgrade s
ON t.avg_sal BETWEEN s.losal AND s.hisal
) t1
JOIN dept ON (t1.deptno = dept.deptno)
WHERE t1.grade =
(SELECT MIN(grade) FROM
(SELECT deptno,grade,avg_sal FROM
(SELECT AVG(sal) avg_sal, deptno FROM emp GROUP BY deptno) t JOIN salgrade s
ON t.avg_sal BETWEEN s.losal AND s.hisal))

--求部门经理人平均薪水最低的部门名称


--求比普通员工的最高薪水还要高的经理人名称
  SELECT ename  FROM emp WHERE
  empno IN (SELECT DISTINCT mgr FROM emp WHERE mgr IS NOT NULL)
  AND sal >
(SELECT max(sal) FROM  emp WHERE empno NOT  IN
(SELECT DISTINCT mgr FROM emp WHERE mgr IS NOT NULL))

--求薪水最高的前5名雇员
SELECT ename,sal
FROM (
SELECT ename,sal FROM emp ORDER BY sal DESC
)
WHERE ROWNUM <=5;
--求薪水最高的第6到第10名雇员
SELECT ename,sal FROM(
SELECT ename,sal, ROWNUM r FROM(
SELECT ename,sal FROM emp ORDER BY sal DESC)
)

WHERE r>=6 AND r<=10;


注:查询语句是自己看马士兵的视频敲出来的


Orcla 数据库复习2 --子查询和表连接