|
在面试的时候 考到有关数据库的东西 最多的就是复杂的查询了
SELECT * FROM DEPT
SELECT * FROM EMP
--查询各个部门中员工的平均工资;
SELECT T.DEPTNO,DNAME,AVG
FROM(
SELECT DEPTNO,ROUND(AVG(SAL),2) AS AVG
FROM EMP
GROUP BY DEPTNO) T,DEPT
WHERE T.DEPTNO=DEPT.DEPTNO;
--显示部门员工平均工资大于2000的部门;
SELECT T.DEPTNO,DNAME,AVG
FROM (
SELECT ROUND(AVG(SAL),2)AS AVG,DEPTNO FROM EMP
GROUP BY DEPTNO) T,DEPT
WHERE T.DEPTNO=DEPT.DEPTNO;
--求部门平均薪水的等级 部门编号 平均工资 工资等级
SELECT * FROM SALGRADE
SELECT * FROM EMP
SELECT DEPTNO,AVG_SAL,GRADE
FROM
(
SELECT ROUND(AVG(SAL),2)AS AVG_SAL,DEPTNO FROM EMP GROUP BY DEPTNO
) T INNER JOIN SALGRADE S ON T.AVG_SAL BETWEEN S.LOSAL AND S.HISAL
--显示每个部门的平均工资和最高工资最低工资部门名称;
SELECT T.DEPTNO,DNAME,AVG,MAX,MIN
FROM(
SELECT DEPTNO,ROUND(AVG(SAL),2)AS AVG,MAX(SAL)AS MAX,MIN(SAL)AS MIN
FROM EMP
GROUP BY DEPTNO) T,DEPT
WHERE T.DEPTNO=DEPT.DEPTNO
--显示每个部门的雇员总数,平均工资;工资总计;
SELECT T.DEPTNO,DNAME,COUNT,AVG,SUM
FROM
(
SELECT DEPTNO,COUNT(ENAME)AS COUNT,ROUND(AVG(SAL),2)AS AVG,SUM(SAL)AS SUM
FROM EMP GROUP BY(DEPTNO)
) T,DEPT
WHERE T.DEPTNO=DEPT.DEPTNO
--方案一:显示所有雇员的姓名 工资 所在部门;
SELECT ENAME,SAL,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
--方案二:显示所有雇员的姓名 工资 所在部门
SELECT ENAME,SAL,DNAME
FROM EMP INNER JOIN DEPT ON EMP.DEPTNO=DEPT.DEPTNO
--显示10号部门 的雇员的姓名工资 所在部门的名称;
--方案一:
SELECT ENAME,SAL,DNAME
FROM
(
SELECT ENAME,SAL FROM EMP
WHERE DEPTNO=10
) T ,DEPT
WHERE DEPTNO=10
--方案二:
SELECT ENAME,SAL,DNAME
FROM EMP INNER JOIN DEPT ON DEPT.DEPTNO=10 AND EMP.DEPTNO=10
--查询部门编号为20的所有雇员姓名及雇员所在部门名称;
--方案一:
SELECT * FROM EMP
SELECT DEPT.DEPTNO,DNAME,ENAME FROM
(
SELECT ENAME,DEPTNO FROM EMP WHERE DEPTNO=20
) T,DEPT WHERE DEPT.DEPTNO=20
--方案二:
SELECT DEPT.DEPTNO,DNAME,ENAME
FROM DEPT INNER JOIN EMP ON DEPT.DEPTNO=20 AND EMP.DEPTNO=20
--显示所有部门的名称 及部门编号为20的所有雇员的名称;
SELECT * FROM DEPT
--方案一:
SELECT T.DEPTNO,DNAME,ENAME
FROM (SELECT DEPTNO,DNAME FROM DEPT) T
LEFT OUTER JOIN (SELECT DEPTNO,ENAME FROM EMP WHERE DEPTNO=20) D
ON T.DEPTNO=D.DEPTNO
--显示所有雇员的姓名及部门编号为20的部门的名称;
SELECT ENAME,D.DEPTNO,DNAME
FROM (SELECT DEPTNO,ENAME FROM EMP) T LEFT OUTER JOIN
(SELECT DEPTNO,DNAME FROM DEPT WHERE DEPTNO=20) D ON T.DEPTNO=D.DEPTNO
--显示BLAKE所在部门的所有雇员的 姓名及部门编号
--方案一:
SELECT ENAME,DEPTNO
FROM EMP
WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='BLAKE')
--方案二:
--显示比部门编号为20的 所有的工资都高的雇员的信息
--方案一:
SELECT * FROM EMP WHERE SAL>ALL(
SELECT SAL FROM EMP WHERE DEPTNO=20)
--方案二:
SELECT * FROM EMP WHERE SAL>(
SELECT MAX(SAL) FROM EMP WHERE DEPTNO=20)
--显示比部门编号为10的人以员工工资都高的员工的信息
--方案一:
SELECT * FROM EMP WHERE SAL>(
SELECT MIN(SAL) FROM EMP WHERE DEPTNO=10)
--方案二:
SELECT * FROM EMP WHERE SAL>ANY(
SELECT SAL FROM EMP WHERE DEPTNO=10)
--显示与SMITH同一部门同一职务的员工的信息
SELECT * FROM EMP WHERE(DEPTNO,JOB)IN(
SELECT DEPTNO,JOB FROM EMP WHERE ENAME='SMITH')
--列出至少有一个员工的部门的名称;
SELECT * FROM EMP
SELECT * FROM DEPT
SELECT DNAME FROM DEPT WHERE DEPTNO IN
(
SELECT DEPTNO FROM EMP
)
--列出薪水比SMITH 高的所有的员工
SELECT * FROM EMP WHERE SAL>
(
SELECT SAL FROM EMP WHERE ENAME='SMITH'
)
--列出所有员工的姓名及其直接上级的姓名。(采用自连接的方式)
SELECT T.员工,ENAME AS 领导
FROM
(
SELECT E1.ENAME AS 员工,E2.MGR AS 领导
FROM EMP E1 ,EMP E2
WHERE E1.EMPNO=E2.EMPNO
) T ,EMP
WHERE T.领导=EMPNO
--列出受雇日期早于其直接上级的所有员工。(采用的是自连接)
SELECT T1.ENAME ,T1.RQ
FROM
(
SELECT EMPNO,ENAME,MGR,
ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE),1)AS RQ
FROM EMP
) T1,
(
SELECT EMPNO,ENAME,MGR,
ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE),1)AS RQ
FROM EMP
) T2
WHERE T1.MGR=T2.EMPNO AND T1.RQ>T2.RQ
--列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
SELECT * FROM EMP
SELECT * FROM DEPT
SELECT DEPT.DEPTNO,DNAME,ENAME
FROM DEPT LEFT OUTER JOIN EMP
ON DEPT.DEPTNO=EMP.DEPTNO
--显示各部门员工薪金最高的前3位员工
SELECT EMPNO,ENAME,JOB,SAL,DEPTNO,ROWNUM
FROM
(
SELECT EMPNO,ENAME,JOB,SAL,DEPTNO
FROM EMP
ORDER BY SAL DESC
)
WHERE ROWNUM<4
--显示各部门员工薪金最高的前2名 (可以做成获取前N名工资最高的信息)
--利用游标和自定义类型;
DECLARE
CURSOR CUR_TOP2 IS
SELECT EMPNO,ENAME,JOB,SAL,DEPTNO,ROWNUM FROM
(
SELECT EMPNO,ENAME,JOB,SAL,DEPTNO
FROM EMP
ORDER BY SAL DESC
) ORDER BY ROWNUM ;
SALARY1 SCOTT.EMP.SAL%TYPE;
SALARY2 SCOTT.EMP.SAL%TYPE;
TYPE EMPX IS RECORD
(
EMPNO SCOTT.EMP.EMPNO%TYPE,
ENAME SCOTT.EMP.ENAME%TYPE,
JOB SCOTT.EMP.JOB%TYPE,
SAL SCOTT.EMP.SAL%TYPE,
DEPTNO SCOTT.EMP.DEPTNO%TYPE,
ROWNUM INT
);
TEST_EMP EMPX;
I INT;
BEGIN
I:=0;
OPEN CUR_TOP2;
FETCH CUR_TOP2 INTO TEST_EMP;
SALARY1:=TEST_EMP.SAL;
DBMS_OUTPUT.put_line(TEST_EMP.EMPNO || TEST_EMP.ENAME || TEST_EMP.JOB ||TEST_EMP.SAL);
LOOP
FETCH CUR_TOP2 INTO TEST_EMP;
SALARY2:=TEST_EMP.SAL;
IF SALARY2<SALARY1 THEN
I:=I+1;
SALARY1:=SALARY2;
END IF;
EXIT WHEN I=2;
DBMS_OUTPUT.put_line(TEST_EMP.EMPNO || TEST_EMP.ENAME || TEST_EMP.JOB ||TEST_EMP.SAL);
END LOOP;
CLOSE CUR_TOP2;
END;
--方案二:查询前工资前2名的员工的信息;
--一种简单的查询前两名的信息的方法! 利用了DESTINCT 来删除重复的行的方法
SELECT * FROM EMP WHERE SAL IN
(
SELECT SAL AS 工资
FROM
(
SELECT DISTINCT SAL FROM EMP
ORDER BY SAL DESC
)
WHERE ROWNUM<3
)
ORDER BY SAL DESC
--方案三:
select * from
(select deptno,ename,sal,row_number() over (partition by deptno
order by sal desc) rn
from emp)
where rn<3;
--得到不同部门的工资前两名的员工的信息
SELECT * FROM
(
SELECT EMPNO,ENAME,JOB,SAL,DEPTNO,ROW_NUMBER() OVER (PARTITION BY DEPTNO
ORDER BY SAL DESC) RN
FROM EMP
) WHERE RN<3
--企业级的面试题1。
CREATE TABLE TEMP
(
ID NUMBER(4)PRIMARY KEY,
NAME VARCHAR2(20),
LAST_LOGIN DATE
)
SELECT * FROM TEMP
CREATE SEQUENCE SQ_TEMP
INSERT INTO TEMP VALUES(SQ_TEMP.NEXTVAL,'JACKY','4-3月-2009');
INSERT INTO TEMP VALUES(SQ_TEMP.NEXTVAL,'JACKY','6-3月-2009');
INSERT INTO TEMP VALUES(SQ_TEMP.NEXTVAL,'JACKY','20-3月-2009');
INSERT INTO TEMP VALUES(SQ_TEMP.NEXTVAL,'LUCY','8-3月-2009');
INSERT INTO TEMP VALUES(SQ_TEMP.NEXTVAL,'LUCY','3-8月-2009');
--用sql语句查询出每个用户最近一次登陆的记录每个用户只显示一条最近的记录
SELECT * FROM (
SELECT * FROM TEMP WHERE NAME='JACKY'
ORDER BY ID DESC)
WHERE ROWNUM=1
--企业面试题2
CREATE TABLE BOOK
(
ID NUMBER(4) PRIMARY KEY,
TYPE VARCHAR2(20),
TITLE VARCHAR2(20),
PRICE NUMBER(4,2)
)
INSERT INTO BOOK VALUES(1,'文学','卡耐基',20);
INSERT INTO BOOK VALUES(2,'文学','明史',30);
INSERT INTO BOOK VALUES(3,'文学','毛泽东',25);
INSERT INTO BOOK VALUES(4,'计算机','C#',35);
INSERT INTO BOOK VALUES(5,'计算机','java',15);
INSERT INTO BOOK VALUES(6,'计算机','SQL',10);
INSERT INTO BOOK VALUES(7,'计算机','SSH',18);
INSERT INTO BOOK VALUES(8,'计算机','SSO',48);
--选出高出同类平均价格的数据;
SELECT * FROM BOOK INNER JOIN
(SELECT AVG(PRICE) AVG,TYPE FROM BOOK GROUP BY TYPE) T
ON BOOK.TYPE=T.TYPE AND BOOK.PRICE>T.AVG
--使用 NVL()函数来处理 空置的机制
SELECT SAL*12+NVL(COMM,0) FROM EMP
--每使用空值的结果
SELECT SAL*12+COMM FROM EMP
--总结 : 空值在数学表达式中的计算结果都为空值
本人声明:以上出自个人之手 如有错误 请及时的更正 在下将不胜感激 |
|