标签:
Oracle继续学习中。。。。。。
查询职位是"MANAGER"或职位不是"ANALYST"的员工(方式一)
select * from emp where job=‘MANAGER‘ or not(job=‘ANALYST‘);SQL> select * from emp where job='MANAGER' or not(job='ANALYST'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7369 SMITH CLERK 7902 17-12月-80 800 20 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 7839 KING PRESIDENT 17-11月-81 5000 10 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7934 MILLER CLERK 7782 23-1月 -82 1300 10 已选择12行。查询职位是"MANAGER"或职位不是"ANALYST"的员工(方式二):select * from emp where job=‘MANAGER‘ or job!=‘ANALYST‘;
SQL> select * from emp where job='MANAGER' or job !='ANALYST'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7369 SMITH CLERK 7902 17-12月-80 800 20 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 7839 KING PRESIDENT 17-11月-81 5000 10 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7934 MILLER CLERK 7782 23-1月 -82 1300 10 已选择12行。查询员工信息,按薪水升序排序:select * from emp order by sal desc;
SQL> select * from emp order by sal desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7839 KING PRESIDENT 17-11月-81 5000 10 7902 FORD ANALYST 7566 03-12月-81 3000 20 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7934 MILLER CLERK 7782 23-1月 -82 1300 10 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7369 SMITH CLERK 7902 17-12月-80 800 20 已选择14行。查询员工信息,按入职日期降序排序:select * from emp order by hiredate desc;
SQL> select * from emp order by hiredate desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 7934 MILLER CLERK 7782 23-1月 -82 1300 10 7902 FORD ANALYST 7566 03-12月-81 3000 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7839 KING PRESIDENT 17-11月-81 5000 10 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7369 SMITH CLERK 7902 17-12月-80 800 20
已选择14行order by后面可以跟列名、别名、表达式、列号
select empno "编号",ename "姓名",sal "工 资" from emp order by "工 资" desc;
SQL> select empno "编号",ename "姓名",sal "工 资" from emp order by "工 资" desc ; 编号 姓名 工 资 ---------- -------------------- ---------- 7839 KING 5000 7902 FORD 3000 7788 SCOTT 3000 7566 JONES 2975 7698 BLAKE 2850 7782 CLARK 2450 7499 ALLEN 1600 7844 TURNER 1500 7934 MILLER 1300 7521 WARD 1250 7654 MARTIN 1250 7876 ADAMS 1100 7900 JAMES 950 7369 SMITH 800 已选择14行。
select empno "编号",ename "姓名",sal "工资",sal*12 "年薪" from emp order by sal*12 desc;
SQL> select empno "编号",ename "姓名",sal "工资",sal*12 "年薪" from emp order by sal*12 desc; 编号 姓名 工资 年薪 ---------- -------------------- ---------- ---------- 7839 KING 5000 60000 7902 FORD 3000 36000 7788 SCOTT 3000 36000 7566 JONES 2975 35700 7698 BLAKE 2850 34200 7782 CLARK 2450 29400 7499 ALLEN 1600 19200 7844 TURNER 1500 18000 7934 MILLER 1300 15600 7521 WARD 1250 15000 7654 MARTIN 1250 15000 7876 ADAMS 1100 13200 7900 JAMES 950 11400 7369 SMITH 800 9600 已选择14行。select empno "编号",ename "姓名",sal "工资",sal*12 "年薪" from emp order by 4 desc;
SQL> select empno "编号",ename "姓名",sal "工资",sal*12 "年薪" from emp order by 4 desc; 编号 姓名 工资 年薪 ---------- -------------------- ---------- ---------- 7839 KING 5000 60000 7902 FORD 3000 36000 7788 SCOTT 3000 36000 7566 JONES 2975 35700 7698 BLAKE 2850 34200 7782 CLARK 2450 29400 7499 ALLEN 1600 19200 7844 TURNER 1500 18000 7934 MILLER 1300 15600 7521 WARD 1250 15000 7654 MARTIN 1250 15000 7876 ADAMS 1100 13200 7900 JAMES 950 11400 7369 SMITH 800 9600 已选择14行。查询员工信息,按佣金升序或降序排列,null值放后面(nulls last)
select * from emp order by comm asc;
SQL> select * from emp order by comm asc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 7839 KING PRESIDENT 17-11月-81 5000 10 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7902 FORD ANALYST 7566 03-12月-81 3000 20 7934 MILLER CLERK 7782 23-1月 -82 1300 10 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7369 SMITH CLERK 7902 17-12月-80 800 20 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 已选择14行。select * from emp order by comm desc nulls last;
SQL> select * from emp order by comm desc nulls last; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 7839 KING PRESIDENT 17-11月-81 5000 10 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7902 FORD ANALYST 7566 03-12月-81 3000 20 7934 MILLER CLERK 7782 23-1月 -82 1300 10 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7369 SMITH CLERK 7902 17-12月-80 800 20 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 已选择14行。查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列
select * from emp order by sal desc,hiredate desc;
SQL> select * from emp order by sal desc,hiredate desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- ---------- ----- ------------ ----- ----- ------ 7839 KING PRESIDENT 17-11月-81 5000 10 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 7902 FORD ANALYST 7566 03-12月-81 3000 20 7566 JONES MANAGER 7839 02-4月 -81 2975 20 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7782 CLARK MANAGER 7839 09-6月 -81 2450 10 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7934 MILLER CLERK 7782 23-1月 -82 1300 10 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7900 JAMES CLERK 7698 03-12月-81 950 30 7369 SMITH CLERK 7902 17-12月-80 800 20 已选择14行。测试LOWER/UPPER/INITCAP函数
SQL> select lower('Hello World') "转小写",upper('Hello World') "转大写",initcap( 'hello world') "首字母大写" from dual; 转小写 转大写 首字母大写 ---------------------- ---------------------- ---------------------- hello world HELLO WORLD Hello World测试CONCAT/SUBSTR函数,从1开始:
SQL> select concat('hello',' world') "字符串拼接",substr('hello world',7,11) "字 符串截取" from dual; 字符串拼接 字符串截取 ---------------------- ---------- hello world world测试LENGTH/LENGTHB函数:
select length(‘中CHINA‘) "字符",lengthb(‘中CHINA‘) "字节" from dual;
SQL> select length(‘中CHINA‘) "字符",lengthb(‘中CHINA‘) "字节" from dual;
字符 字节 ---------- ---------- 6 8
length:不管中英文,都是1位
lengthb:英文,是1位;中文是3位【unicode即utf-8】SQL> select instr('hello world','o'),lpad('haha',10,'#'),rpad('hehe',10,'@') fro m dual; INSTR('HELLOWORLD','O') LPAD('HAHA',10,'#') RPAD('HEHE',10,'@') ----------------------- -------------------- -------------------- 5 ######haha hehe@@@@@@测试TRIM/REPLACE函数
select trim(‘x‘ from ‘xxxHELLOxxxWORLDxxx‘),replace(‘abcd‘,‘c‘,‘$‘) from dual;
SQL> select trim('x' from 'xxxHELLOxxxWORLDxxx'),replace('abcd','c','$') from du al; TRIM('X'FROM'XXXHELLOXXXWO REPLACE( -------------------------- -------- HELLOxxxWORLD ab$dselect trim(‘ ‘ from ‘ HELLO WORLD ‘) from dual;
SQL> select trim(' ' from ' HELLO WORLD ') from dual; TRIM(''FROM'HELLOWORLD') -------------------------- HELLO WORLD测试ROUND/TRUNC/MOD函数
SQL> select round(3.1415926,3),trunc(3.1415926,3),mod(1000,300) from dual; ROUND(3.1415926,3) TRUNC(3.1415926,3) MOD(1000,300) ------------------ ------------------ ------------- 3.142 3.141 100select round(sysdate,‘month‘) from dual;
SQL> select round(sysdate,'month') from dual; ROUND(SYSDATE, -------------- 01-7月 -16select trunc(sysdate,‘month‘) from dual;
SQL> select trunc(sysdate,'month') from dual; TRUNC(SYSDATE, -------------- 01-6月 -16显示昨天,今天,明天的日期
SQL> select sysdate-1 "昨天",sysdate "今天",sysdate+1 明天 from dual; 昨天 今天 明天 -------------- -------------- -------------- 19-6月 -16 20-6月 -16 21-6月 -16以年和月形式显示员工近似工龄
SQL> select ename "姓名",sysdate-hiredate "日工龄",(sysdate-hiredate)/30 "月工龄 ",(sysdate-hiredate)/365 "年工龄" from emp; 姓名 日工龄 月工龄 年工龄 -------------------- ---------- ---------- ---------- SMITH 12969.987 432.332899 35.5342109 ALLEN 12904.987 430.166232 35.3561287 WARD 12902.987 430.099566 35.3506492 JONES 12863.987 428.799566 35.2437999 MARTIN 12684.987 422.832899 34.753389 BLAKE 12834.987 427.832899 35.1643479 CLARK 12795.987 426.532899 35.0574985 SCOTT 10655.987 355.199566 29.1944848 KING 12634.987 421.166232 34.6164027 TURNER 12704.987 423.499566 34.8081835 ADAMS 10621.987 354.066232 29.1013342 JAMES 12618.987 420.632899 34.572567 FORD 12618.987 420.632899 34.572567 MILLER 12567.987 418.932899 34.432841 已选择14行。使用MONTHS_BETWEEN函数,以精确月形式显示员工工龄
SQL> select ename "姓名",(sysdate-hiredate)/30 "近似月工龄",months_between(sysda te,hiredate) "精确月工龄" from emp; 姓名 近似月工龄 精确月工龄 -------------------- ---------- ---------- SMITH 432.332915 426.128627 ALLEN 430.166248 424 WARD 430.099581 423.967337 JONES 428.799581 422.612498 MARTIN 422.832915 416.773788 BLAKE 427.832915 421.644756 CLARK 426.532915 420.386692 SCOTT 355.199581 350.064111 KING 421.166248 415.128627 TURNER 423.499581 417.41895 ADAMS 354.066248 348.935079 JAMES 420.632915 414.58024 FORD 420.632915 414.58024 MILLER 418.932915 412.935079 已选择14行。
测试ADD_MONTHS函数
select add_months(sysdate,3) from dual;
SQL> select add_months(sysdate,3) from dual; ADD_MONTHS(SYS -------------- 20-9月 -16测试NEXT_DAY函数【中文平台】
select next_day(sysdate,‘星期三‘) from dual;
SQL> select next_day(sysdate,'星期三') from dual; NEXT_DAY(SYSDA -------------- 22-6月 -16从今天开始算,下一个星期三是多少号?
select next_day(next_day(sysdate,‘星期三‘),‘星期三‘) from dual;
SQL> select next_day(next_day(sysdate,'星期三'),'星期三') from dual; NEXT_DAY(NEXT_ -------------- 29-6月 -16日期隐式转换,读字符串,判断是否符合日期格式要求,再隐式转换,效率低
SQL> select ename,to_char(hiredate,'yyyy-mm-dd') from emp where hiredate=to_date ('1980-12-17','yyyy-mm-dd'); ENAME TO_CHAR(HIREDATE,'YY ---------- -------------------- SMITH 1980-12-17使用to_char()函数将日期转成字符串,显示如下格式:2012-12-1 今天是 星期六 20:12:47
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; TO_CHAR(SYSDATE,'YYYY-MM-DDHH24:MI:SS' -------------------------------------- 2016-06-20 23:48:23select to_char(sysdate,‘YYYY-MM-DD-DAY HH:MI:SS AM‘)from dual;
SQL> select to_char(sysdate,'YYYY-MM-DD-DAY HH:MI:SS AM')from dual; TO_CHAR(SYSDATE,'YYYY-MM-DD-DAYHH:MI:SSAM') -------------------------------------------------------------------- 2016-06-20-星期一 11:50:02 下午select to_char(sysdate,‘yyyy-mm-dd "今天是" day hh24:mi:ss‘) "时间"from dual;
SQL> select to_char(sysdate,'yyyy-mm-dd "今天是" day hh24:mi:ss') "时间"from dua l; 时间 -------------------------------------------------------------------------------- 2016-06-20 今天是 星期一 23:50:35
SQL> select ename,to_char(sal,'$9,999') from emp; ENAME TO_CHAR(SAL,'$ ---------- -------------- SMITH $800 ALLEN $1,600 WARD $1,250 JONES $2,975 MARTIN $1,250 BLAKE $2,850 CLARK $2,450 SCOTT $3,000 KING $5,000 TURNER $1,500 ADAMS $1,100 JAMES $950 FORD $3,000 MILLER $1,300 已选择14行。
SQL> select ename,to_char(sal,'L9,999') from emp; ENAME TO_CHAR(SAL,'L9,999') ---------- -------------------------------- SMITH ¥800 ALLEN ¥1,600 WARD ¥1,250 JONES ¥2,975 MARTIN ¥1,250 BLAKE ¥2,850 CLARK ¥2,450 SCOTT ¥3,000 KING ¥5,000 TURNER ¥1,500 ADAMS ¥1,100 JAMES ¥950 FORD ¥3,000 MILLER ¥1,300 已选择14行。使用to_number()函数将字符串转成数字
SQL> select to_number('123')+100 "结果" from dual; 结果 ---------- 223使用nul函数,统计员工年收入
SQL> select ename,12*sal+nvl(comm,0) from emp; ENAME 12*SAL+NVL(COMM,0) ---------- ------------------ SMITH 9600 ALLEN 19500 WARD 15500 JONES 35700 MARTIN 16400 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 15600 已选择14行。使用nul2函数,统计员工年收入
from emp;
SQL> select ename,job,sal, 2 case job 3 when 'PRESIDENT' then sal+1000 4 when 'MANAGER' then sal+800 5 else sal+400 6 end "涨后薪水" 7 from emp; ENAME JOB SAL 涨后薪水 ---------- ---------- ----- ---------- SMITH CLERK 800 1200 ALLEN SALESMAN 1600 2000 WARD SALESMAN 1250 1650 JONES MANAGER 2975 3775 MARTIN SALESMAN 1250 1650 BLAKE MANAGER 2850 3650 CLARK MANAGER 2450 3250 SCOTT ANALYST 3000 3400 KING PRESIDENT 5000 6000 TURNER SALESMAN 1500 1900 ADAMS CLERK 1100 1500 JAMES CLERK 950 1350 FORD ANALYST 3000 3400 MILLER CLERK 1300 1700 已选择14行。各数据库厂都支持ANSISQL标准
SQL> select ename,job,sal 涨前薪水, 2 decode(job,'PRESIDENT',sal+1000,'MANAGER',sal+800,sal+400) "涨后薪水 " from emp; ENAME JOB 涨前薪水 涨后薪水 ---------- ---------- ---------- ---------- SMITH CLERK 800 1200 ALLEN SALESMAN 1600 2000 WARD SALESMAN 1250 1650 JONES MANAGER 2975 3775 MARTIN SALESMAN 1250 1650 BLAKE MANAGER 2850 3650 CLARK MANAGER 2450 3250 SCOTT ANALYST 3000 3400 KING PRESIDENT 5000 6000 TURNER SALESMAN 1500 1900 ADAMS CLERK 1100 1500 JAMES CLERK 950 1350 FORD ANALYST 3000 3400 MILLER CLERK 1300 1700 已选择14行。员工总工资,平均工资
SQL> select sum(sal) "总工资",round(avg(sal),2) "平均工资" from emp; 总工资 平均工资 ---------- ---------- 29025 2073.21员工最高工资,最低工资
SQL> select max(sal) "最高工资",min(sal) "最低工资" from emp; 最高工资 最低工资 ---------- ---------- 5000 800入职最早员工,入职最晚员工
SQL> select max(hiredate) "最晚入职员工",min(hiredate) "最早入职员工" from emp; 最晚入职员工 最早入职员工 -------------- -------------- 23-5月 -87 17-12月-80求员工总人数
SQL> select count(*) 员工总人数 from emp; 员工总人数 ---------- 14统计有佣金的员工人数
select count(comm) 有佣金的员工人数 from emp;
SQL> select count(comm) 有佣金的员工人数 from emp; 有佣金的员工人数 ---------------- 4分组函数不统计null值
SQL> select count(distinct deptno) from emp; COUNT(DISTINCTDEPTNO) --------------------- 3
SQL> select deptno,trunc(avg(sal),0) from emp group by deptno; DEPTNO TRUNC(AVG(SAL),0) ------ ----------------- 30 1566 20 2175 10 2916select trunc(avg(sal),0) from emp group by deptno;
SQL> select trunc(avg(sal),0) from emp group by deptno; TRUNC(AVG(SAL),0) ----------------- 1566 2175 2916--以下SQL语句是错误,原因在于select子句中的非组函数没有全部出现在group by子句中
SQL> select deptno,trunc(avg(sal),0) "部门平均工资" 2 from emp 3 group by deptno 4 having trunc(avg(sal),0)>2000; DEPTNO 部门平均工资 ------ ------------ 20 2175 10 2916
SQL> select deptno,trunc(avg(sal),0) "部门平均工资" 2 from emp 3 group by deptno 4 having trunc(avg(sal),0)>2000 and deptno!=10; DEPTNO 部门平均工资 ------ ------------ 20 2175方式二【先行过滤,再组过滤,性能较佳】
SQL> select deptno,trunc(avg(sal),0) "部门平均工资" 2 from emp 3 where deptno!=10 4 group by deptno 5 having trunc(avg(sal),0)>2000; DEPTNO 部门平均工资 ------ ------------ 20 2175显示部门平均工资的最大值
group by deptno;
SQL> select max(avg(sal)) 2 from emp 3 group by deptno; MAX(AVG(SAL)) ------------- 2916.66667
标签:
原文地址:http://blog.csdn.net/u010143291/article/details/51723968