课程复习
1.ORACLE的四个用户
超级管理员:sys (change_on_install);
普通管理员:system(manager);
普通用户:scott、hr
2.sqlplus 的主要操作命令:@、ed、conn、set、shutdown immediate、statup;
3.SQL语法的基本格式:
SELECT [DISTINCT] * |列 [别名],[列 别名,...]
FROM 表名称 [别名]
[WHERE 条件(s)]
[ORDER BY 排序字段 ASC|DESC [排序字段 ASC|DESC]];
其中对于WHERE而言,可以使用关系运算符、逻辑运算符、BETWEEN...AND、LIKE、IN、IS(NOT)NULL;
4.单行函数:UPPER()、LOWER()、INITCAP()、REPLACE()、LENGTH()、SUBSTR()、ROUND()、TRUNC()、MOD()、LAST_DAY()、NEXT_DAY()、MONTHS_BETWEEN()、ADD_MONTHS()、SYSDATE、TO_CHAR()、TO_DATE()、NVL()、DECODE(),以上的函数知道怎么使用即可,以后使用到了相关的功能之后,能用就行;
5.日期的计算:
日期+数字=日期;
日期-数字=日期;
日期-日期=数字(天数);
在很多语言之中,日期可以和数字互相转换;
习题:
1.选择部门60中的所有的员工。
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- ----------- ---------- ---------- -------------- ---------- -------------
103 Alexander Hunold AHUNOLD 590.423.4567 1990-01-03 IT_PROG 9000.00 102 60
104 Bruce Ernst BERNST 590.423.4568 1991-05-21 IT_PROG 6000.00 103 60
105 David Austin DAUSTIN 590.423.4569 1997-06-25 IT_PROG 4800.00 103 60
106 Valli Pataballa VPATABAL 590.423.4560 1998-02-05 IT_PROG 4800.00 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 1999-02-07 IT_PROG 4200.00 103 60
2.列出所有办事员(SH_CLERK)的姓名,编号的部门编号;
SQL> SELECT first_name,job_id,employee_id,department_id FROM employees WHERE job_id='SH_CLERK';
FIRST_NAME JOB_ID EMPLOYEE_ID DEPARTMENT_ID
-------------------- ---------- ----------- -------------
Donald SH_CLERK 198 50
Douglas SH_CLERK 199 50
Winston SH_CLERK 180 50
Jean SH_CLERK 181 50
Martha SH_CLERK 182 50
Girard SH_CLERK 183 50
Nandita SH_CLERK 184 50
Alexis SH_CLERK 185 50
Julia SH_CLERK 186 50
Anthony SH_CLERK 187 50
Kelly SH_CLERK 188 50
Jennifer SH_CLERK 189 50
Timothy SH_CLERK 190 50
Randall SH_CLERK 191 50
Sarah SH_CLERK 192 50
Britney SH_CLERK 193 50
Samuel SH_CLERK 194 50
Vance SH_CLERK 195 50
Alana SH_CLERK 196 50
Kevin SH_CLERK 197 50
20 rows selected
3.找出佣金高于年薪
SQL> SELECT * FROM employees WHERE commission_pct>salary;
4.找出部门80中所有经理(manager)和部门50中所有办事员(SH_CLERK)的详细信息;
SQL> SELECT * FROM employees WHERE (department_id=80 AND job_id='SA_MAN') OR (department_id=50 AND job_id='SH_CLERK');
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- ----------- ---------- ---------- -------------- ---------- -------------
145 John Russell JRUSSEL 011.44.1344.429268 1996-10-01 SA_MAN 14000.00 0.40 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 1997-01-05 SA_MAN 13500.00 0.30 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 1997-03-10 SA_MAN 12000.00 0.30 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 1999-10-15 SA_MAN 11000.00 0.30 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 2000-01-29 SA_MAN 10500.00 0.20 100 80
198 Donald OConnell DOCONNEL 650.507.9833 1999-06-21 SH_CLERK 2600.00 124 50
199 Douglas Grant DGRANT 650.507.9844 2000-01-13 SH_CLERK 2600.00 124 50
180 Winston Taylor WTAYLOR 650.507.9876 1998-01-24 SH_CLERK 3200.00 120 50
181 Jean Fleaur JFLEAUR 650.507.9877 1998-02-23 SH_CLERK 3100.00 120 50
182 Martha Sullivan MSULLIVA 650.507.9878 1999-06-21 SH_CLERK 2500.00 120 50
183 Girard Geoni GGEONI 650.507.9879 2000-02-03 SH_CLERK 2800.00 120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 1996-01-27 SH_CLERK 4200.00 121 50
185 Alexis Bull ABULL 650.509.2876 1997-02-20 SH_CLERK 4100.00 121 50
186 Julia Dellinger JDELLING 650.509.3876 1998-06-24 SH_CLERK 3400.00 121 50
187 Anthony Cabrio ACABRIO 650.509.4876 1999-02-07 SH_CLERK 3000.00 121 50
188 Kelly Chung KCHUNG 650.505.1876 1997-06-14 SH_CLERK 3800.00 122 50
189 Jennifer Dilly JDILLY 650.505.2876 1997-08-13 SH_CLERK 3600.00 122 50
190 Timothy Gates TGATES 650.505.3876 1998-07-11 SH_CLERK 2900.00 122 50
191 Randall Perkins RPERKINS 650.505.4876 1999-12-19 SH_CLERK 2500.00 122 50
192 Sarah Bell SBELL 650.501.1876 1996-02-04 SH_CLERK 4000.00 123 50
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- -------------------- ------------------------- ------------------------- -------------------- ----------- ---------- ---------- -------------- ---------- -------------
193 Britney Everett BEVERETT 650.501.2876 1997-03-03 SH_CLERK 3900.00 123 50
194 Samuel McCain SMCCAIN 650.501.3876 1998-07-01 SH_CLERK 3200.00 123 50
195 Vance Jones VJONES 650.501.4876 1999-03-17 SH_CLERK 2800.00 123 50
196 Alana Walsh AWALSH 650.507.9811 1998-04-24 SH_CLERK 3100.00 124 50
197 Kevin Feeney KFEENEY 650.507.9822 1998-05-23 SH_CLERK 3000.00 124 50
25 rows selected
5.找出收取佣金的员工的不同工作;
SQL> SELECT DISTINCT job_id FROM employees WHERE commission_pct IS NOT NULL;
JOB_ID
----------
SA_MAN
SA_REP
6.找出各月倒数第3天受雇的所有员工。
SELECT * FROM employees WHERE LAST_DAY(HIRE_DATE)-2=HIRE_DATE;
7.找出早于12年前受雇佣的员工;
如果要求年份,最准确的算法是使用总月数/12;
SQL> SELECT * FROM employees WHERE MONTHS_BETWEEN(SYSDATE,hire_date)/12>12;
8.以首字母大写的方式显示所有员工的姓名;
SQL> SELECT INITCAP(first_name) FROM employees;
INITCAP(FIRST_NAME)
--------------------
Ellen
Sundar
Mozhe
David
Hermann
Shelli
Amit
Elizabeth
Sarah
David
Laura
Harrison
Alexis
Anthony
Gerald
Nanette
John
Kelly
Karen
Curtis
9.显示正好为5个字符的员工的姓名;
SQL> SELECT first_name FROM employees WHERE LENGTH(first_name)=5;
FIRST_NAME
--------------------
Ellen
Mozhe
David
Sarah
David
Laura
Kelly
Karen
Julia
Bruce
Kevin
Nancy
Peter
Vance
Payam
Neena
James
David
Diana
Jason
10.显示不带有“R”的员工的姓名;
SQL> SELECT * FROM employees WHERE first_name NOT LIKE '%R%';
11,显示所有员工姓名的前3个字符;
SQL> SELECT SUBSTR(first_name,0,3) FROM employees;
SUBSTR(FIRST_NAME,0,3)
----------------------
Ell
Sun
Moz
Dav
Her
She
Ami
Eli
Sar
Dav
Lau
Har
Ale
Ant
Ger
Nan
Joh
Kel
Kar
Cur
12.显示所有员工的姓名,用“a”替换所有“A”;
SQL> SELECT REPLACE(first_name,'A','a') FROM employees;
REPLACE(FIRST_NAME,'A','A')
---------------------------
Ellen
Sundar
Mozhe
David
Hermann
Shelli
amit
Elizabeth
Sarah
David
Laura
Harrison
alexis
anthony
Gerald
Nanette
John
Kelly
Karen
Curtis
13.显示员工的详细信息,按姓名排序;
SQL> SELECT * FROM employees ORDER BY first_name;
14.显示员工的姓名和雇佣的日期,根据其服务年限,将最老的员工排在最前面;
SQL> SELECT first_name,hire_date FROM employees ORDER BY hire_date;
FIRST_NAME HIRE_DATE
-------------------- -----------
Steven 1987-06-17
Jennifer 1987-09-17
Neena 1989-09-21
Alexander 1990-01-03
Bruce 1991-05-21
Lex 1993-01-13
Susan 1994-06-07
Hermann 1994-06-07
Shelley 1994-06-07
William 1994-06-07
Daniel 1994-08-16
Nancy 1994-08-17
Den 1994-12-07
Payam 1995-05-01
Alexander 1995-05-18
Renske 1995-07-14
Trenna 1995-10-17
Nandita 1996-01-27
Janette 1996-01-30
Sarah 1996-02-04
15.显示所有员工的姓名、工作和工资,按工作的降序排序,若工作相同则按照工资排序;
SQL> SELECT first_name,job_id,salary FROM employees ORDER BY job_id DESC,salary;
16.显示所有员工姓名,加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面。
需要从日期之中取出年份和月份,用TO_CHAR()函数完成。
SQL> SELECT first_name,TO_CHAR(hire_date,'yyyy') year,TO_CHAR(hire_date,'mm') months FROM employees ORDER BY months,year;
17.显示在(任何年份的)2月份首批的所有员工。
SQL> SELECT * FROM employees WHERE TO_CHAR(hire_date,'mm')=2;
18.显示姓名名字段的任何位置包含“A”的所有员工的姓名
SQL> SELECT first_name FROM employees WHERE first_name LIKE '%A%';
FIRST_NAME
--------------------
Amit
Alexis
Anthony
Alberto
Adam
Alexander
Alyssa
Alexander
Allan
Alana
10 rows selected
19.以年月日的方式显示所有员工的服务年限;
第1步:求出每个雇员的雇佣年数:被雇佣的总月数/12=年数;
SQL> SELECT first_name,hire_date,TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date)/12) year FROM employees;
第2步:求出月数,以上计算之中被忽略的小数都是月份数,用MOD取即可;
SQL> SELECT first_name,hire_date,TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date)/12) year, TRUNC(MOD(MONTHS_BETWEEN(SYSDATE,hire_date),12))FROM employees;
第3步:求出天数,最准的做法是在不超过30天的范围之内求;
现在已经知道当前的时间使用SYSDATE取出,而雇佣的日期使用hire_date取出,但是hire_date和SYSDATE之间的差距太大了,所以肯定会有误差,那么就必须想办法将hire_date的日期提升到和SYSDATE差距在30天的范围之内;
在之前学过2个函数:
MONTHS_BETWEEN():求出2个日期间的月份数,如果是:MONTHS_BETWEEN(SYSDATE,hire_date)求出的是雇佣日期到今天为止的雇佣月份数;
ADD_MONTHS():在一个日期上加上指定的月之后的日期,如果说hire_date + 与今天相距的月份数 = 一个新的日期数,且这个新的日期和SYSDATE就不超过30天了;
SQL> SELECT first_name,hire_date,
TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date)/12) year,
TRUNC(MOD(MONTHS_BETWEEN(SYSDATE,hire_date),12)),
TRUNC(SYSDATE-ADD_MONTHS(hire_date,MONTHS_BETWEEN(SYSDATE,hire_date))) day
FROM employees;
本文出自 “strom的博客” 博客,谢绝转载!