創建一個表記員工個人信息:
--創建一個表 create table plspl_company_info( empno number(5) not null, ename varchar2(10) not null, job varchar2(10), manager number(5), hiredate date, sal number(5), comm number(5), deptno number(3) ); insert into plspl_company_info values (7369, 'SMITH', 'CLERK', 7902, date '1980-12-17', 800, NULL, 10); insert into plspl_company_info values (7293, 'ALLEN', 'SALESMAN', 7689, date '1981-03-27', 1867, NULL, 20); insert into plspl_company_info values (7562, 'JAMES', 'SALESMAN', 7689, date '1981-09-11', 1796, NULL, 20); insert into plspl_company_info values (7936, 'JONES', 'ANAYST', 7656, date '1980-09-01', 3250, NULL, 30); insert into plspl_company_info values (7688, 'WEST', 'MANAGER', 7839, date '1981-02-28', 2985, 900, 40); insert into plspl_company_info values (7499, 'PAUL', 'MANAGER', 7839, date '1980-03-26', 3600, NULL, 40); insert into plspl_company_info values (7778, 'FORD', 'CLERK', 7902, date '1987-04-17', 960, NULL, 10); insert into plspl_company_info values (7289, 'ADAMS', 'SALESMAN', 7689, date '1980-01-09', 1956, NULL, 20); insert into plspl_company_info values (7531, 'MATIN', 'SALESMAN', 7689, date '1980-05-14', 1906, NULL, 20); insert into plspl_company_info values (7916, 'KING', 'ANAYST', 7656, date '1982-06-04', 2864, NULL, 30); insert into plspl_company_info values (7365, 'BLKAE', 'CLERK', 7902, date '1981-09-11', 1200, 1100, 10); insert into plspl_company_info values (7784, 'CHRIS', 'CLERK', 7902, date '1981-07-16', 1376, NULL, 10); select * from plspl_company_info;
執行結果:
SQL>
Table created
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
1 row inserted
EMPNO ENAME JOB MANAGER HIREDATE SAL COMM DEPTNO
------ ---------- ---------- ------- ----------- ------ ------ ------
7369 SMITH CLERK 7902 1980/12/17 800 10
7293 ALLEN SALESMAN 7689 1981/3/27 1867 20
7562 JAMES SALESMAN 7689 1981/9/11 1796 20
7936 JONES ANAYST 7656 1980/9/1 3250 30
7688 WEST MANAGER 7839 1981/2/28 2985 900 40
7499 PAUL MANAGER 7839 1980/3/26 3600 40
7778 FORD CLERK 7902 1987/4/17 960 10
7289 ADAMS SALESMAN 7689 1980/1/9 1956 20
7531 MATIN SALESMAN 7689 1980/5/14 1906 20
7916 KING ANAYST 7656 1982/6/4 2864 30
7365 BLKAE CLERK 7902 1981/9/11 1200 1100 10
7784 CHRIS CLERK 7902 1981/7/16 1376 10
12 rows selected
SQL>
習題:
1,選出部門30裡的所有員工信息
select * from plspl_company_info where deptno = 30;
2,列出所有辦事員(CLERK)的姓名,編號和部門編號
select ename, empno , deptno from plspl_company_info where job = 'CLERK';
3,找出薪金高於傭金60%的員工
select * from plspl_company_info where comm > sal*0.6 ;
4,找出部門10的所有經理(MANAGER)和部門20的所有辦事員(CLERK)
select * from plspl_company_info where (deptno = 10 and job = 'MANAGER') or (deptno = 20 and job = 'CLERK') ;
5,找出部門10的所有經理(MANAGER)和部門20的所有辦事員(CLERK),以及既不是經理有不是辦事員,但薪金大於或等於2000的所有員工的詳細資料
select * from plspl_company_info where (deptno = 10 and job = 'MANAGER') or (deptno = 20 and job = 'CLERK') or ((job not in('MANAGER', 'CLERK')) and sal >= 2000) ;
6,找出收取傭金的員工的不同工作
select distinct job from plspl_company_info where comm is not null ;
7,找出不收取傭金或者收取傭金低於100的員工
select * from plspl_company_info where (comm is null) or comm < 100 ;
8,顯示不帶“R”的員工姓名
select * from plspl_company_info where ename not like '%A%';
9,顯示姓名字段的任何位置包含'A'的所有員工的姓名,顯示結果按照基本工資由高到低;如果工資相同,則按照雇傭年限由早到晚排序;如果雇傭時間相同,則按照職位排序
select * from plspl_company_info where ename like '%A%' order by sal DESC, hiredate, job;