各试题解答如下(欢迎大家指出不同的方法或建议!):
--------1. 找出EMP表中的姓名(ENAME)第三个字母是A 的员工姓名。---------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '__A%';
ENAME
----------
ADAMS
BLAKE
CLARK
-------2. 找出EMP表员工名字中含有A 和N的员工姓名。----------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%' AND ENAME LIKE '%N%';
ENAME
----------
ALLEN
MARTIN
WANGJING
--------或--------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%N%';
ENAME
----------
ALLEN
MARTIN
WANGJING
/*--------3. 找出所有有佣金的员工,列出姓名、工资、佣金,显示结果按工资从小到大,
佣金从大到小。----------*/
SQL> SELECT ENAME,SAL + COMM AS WAGE,COMM
2 FROM SCOTT.EMP
3 ORDER BY WAGE,COMM DESC;
ENAME WAGE COMM
---------- ---------- ---------
TURNER 1500 0.00
WARD 1750 500.00
ALLEN 1900 300.00
MARTIN 2650 1400.00
EricHu 5514 14.00
WANGJING 5514 14.00
huyong 5514 14.00
SMITH
JONES
JAMES
MILLER
FORD
ADAMS
BLAKE
CLARK
SCOTT
KING
17 rows selected
-------4. 列出部门编号为20的所有职位。----------
SQL> SELECT DISTINCT JOB FROM EMP WHERE DEPTNO = 20;
JOB
---------
ANALYST
CLERK
MANAGER
-------5. 列出不属于SALES 的部门。----------
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME <> 'SALES';
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
40 OPERATIONS BOSTON
50 50abc 50def
60 Developer HaiKou
110 信息科 海口
6 rows selected
--或者:
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME != 'SALES';
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT IN('SALES');
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT LIKE 'SALES';
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '__A%';
ENAME
----------
ADAMS
BLAKE
CLARK
-------2. 找出EMP表员工名字中含有A 和N的员工姓名。----------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%' AND ENAME LIKE '%N%';
ENAME
----------
ALLEN
MARTIN
WANGJING
--------或--------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%N%';
ENAME
----------
ALLEN
MARTIN
WANGJING
/*--------3. 找出所有有佣金的员工,列出姓名、工资、佣金,显示结果按工资从小到大,
佣金从大到小。----------*/
SQL> SELECT ENAME,SAL + COMM AS WAGE,COMM
2 FROM SCOTT.EMP
3 ORDER BY WAGE,COMM DESC;
ENAME WAGE COMM
---------- ---------- ---------
TURNER 1500 0.00
WARD 1750 500.00
ALLEN 1900 300.00
MARTIN 2650 1400.00
EricHu 5514 14.00
WANGJING 5514 14.00
huyong 5514 14.00
SMITH
JONES
JAMES
MILLER
FORD
ADAMS
BLAKE
CLARK
SCOTT
KING
17 rows selected
-------4. 列出部门编号为20的所有职位。----------
SQL> SELECT DISTINCT JOB FROM EMP WHERE DEPTNO = 20;
JOB
---------
ANALYST
CLERK
MANAGER
-------5. 列出不属于SALES 的部门。----------
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME <> 'SALES';
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
40 OPERATIONS BOSTON
50 50abc 50def
60 Developer HaiKou
110 信息科 海口
6 rows selected
--或者:
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME != 'SALES';
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT IN('SALES');
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT LIKE 'SALES';