select * from emp
select sal from emp where job='PRESIDENT'
select * from emp where comm=0 or comm is null
select * from emp where hiredate between to_date(19810501,'yyyy/mm/dd') and to_date(19811231,'yyyy/mm/dd')
select empno,ename from emp where length(ename)=4
select * from emp where deptno=10 and job='MANAGER' or (deptno=20 and job='CLERK')
select * from emp where ename not like '%L%' or ename like '%SM%'
select deptno,sal from emp where job='MANAGER'
select * from emp where comm >sal
select ename from emp where extract(month from hiredate)=extract(month from sysdate)
select ename from emp where to_char(hiredate, 'mm') = to_char(sysdate , 'mm');
select ename from emp where extract(month from hiredate)=extract(month from sysdate)+1
select * from emp where to_char(hiredate, 'mm') = to_char(add_months(sysdate,1) , 'mm');
select ename from emp where extract(year from hiredate)=1982
select * from emp where to_char(hiredate,'yyyy') = '1982';
select ename from emp
where extract(year from hiredate)=1981 and extract(month from hiredate)>6
select * from emp where hiredate
between to_date('1981-7-1','yyyy-mm-dd') and to_date('1982-1-1','yyyy-mm-dd') - 1;
select count(*),extract(month from hiredate) from emp
where extract(year from hiredate)='1981' group by extract(month from hiredate)
select count(*), to_char(trunc(hiredate,'month'),'yyyy-mm') from emp
where to_char(hiredate,'yyyy')='1981' group by trunc(hiredate,'month') order by trunc(hiredate,'month');
Select deptno, avg(sal) from emp group by deptno
select job, min(sal) from emp group by job
select * from emp order by hiredate desc
select emp.*, t.ename managername from emp , emp t where emp.mgr=t.empno
select ename,sal from emp where sal>(select sal from emp where ename='ALLEN')
select * from emp where job=(select job from emp where ename='SCOTT') and ename <> 'SCOTT'
select ename from emp,dept where emp.deptno=dept.deptno and dname='SALES'
select ename,sal from emp where deptno=30 and sal=(select sal from emp where ename='MARTIN') and ename <> 'MARTIN'
select ename from emp where sal>(select avg(sal) from emp) and job='SALESMAN'
select ename,dname,sal from emp ,dept where emp.deptno=dept.deptno
select empno,ename,dname,loc from emp,dept where emp.deptno=dept.deptno
select dname,countnum from dept, (select deptno,count(empno) countnum from emp group by deptno) t
where dept.deptno=t.deptno
select dname,c from (select count(*) c, deptno from emp group by deptno) e inner join dept d on e.deptno = d.deptno;
select count(empno) ,job from emp where sal>(select avg(sal) from emp)group by job
select sal,ename from emp e where(select count(*) from emp where sal=e.sal group by sal)>1
select * from (select * from emp order by sal desc )t where rownum<4
select e.*, (select count(*) from emp where sal > e.sal)+1 rank from emp e order by rank;
select * from emp e where (select count(*) from emp where e.hiredate=hiredate)>1
select deptno, max(sal) from emp group by (deptno)
select deptno,job,max(sal) from emp group by deptno,job
select emp.*,grade from emp,salgrade where sal between losal and hisal
select * from(select rownum r,ename from (select ename from emp order by sal desc))where r<=10 and r>=6
select * from emp e where e.sal=(select max(sal) from emp where emp.deptno=e.deptno)
select * from emp e where (select count(*) from emp where sal > e.sal and e.deptno =deptno) < 2
select * from (
select rank() over (partition by deptno order by sal desc) rank, e.* from emp e
) where rank < 3;
select * from emp, (select mgr,count(empno)countnum from emp group by mgr)t
where t.countnum>3 and emp.empno=t.mgr
select * from emp e where (select count(*) from emp where e.empno =mgr) >2 ;
select * from emp where sal>(select avg(sal)sal from emp e where deptno=emp.deptno)
select * from dept where deptno=
(select deptno from emp group by deptno having avg(sal)=
(select max(avg(sal)) from emp group by deptno))
select * from dept where deptno=(select deptno from emp group by deptno having sum(sal)>(select avg(sum(sal))sal from emp group by deptno ))
select * from emp where deptno=
(select deptno from emp group by deptno having sum(sal)>
(select avg(sum(sal))sal from emp group by deptno ))
select d.* from dept d left join emp e on (e.deptno = d.deptno) where empno is null;
select ename,hiredate from emp where deptno=
( select deptno from emp where ename='BLAKE') and ename <> 'BLAKE'
–分别求得字符”Z、H、D和空格”的ASCII值。
SELECT ASCII('Z') Z, ASCII('H') H, ASCII('D') D, ASCII(' ') SPACE FROM DUAL;
–返回字符
SELECT CHR(90), CHR(72), CHR(68), (32) S FROM DUAL;
–连接俩个字符串
SELECT CONCAT(‘Hello’, ‘World!’) INFORMATION FROM DUAL;
–将字符串首字母大写,其余字母小写。
SELECT INITCAP('oh mY God!') INFORMATION FROM DUAL;
–在字符串”oracle 11g”中,从第3个字符开始查询字符串“1”第2次出现的位置。
–i为负数将从右至左查询
SELECT INSTR('oracle 11g', '1', 3, 2) FROM DUAL;
–查询字符串长度
SELECT LENGTH('hello') FROM DUAL;
–查询雇员名称长度大于5的雇员信息及所在部门信息。
SELECT *
FROM EMP E
INNER JOIN DEPT D
ON E.DEPTNO = D.DEPTNO
WHERE LENGTH(E.ENAME) > 5;
–将字符串转换为小写
SELECT LOWER('HellO!') FROM DUAL;
–将字符串转换为大写
SELECT UPPER('heLLo!') FROM DUAL;
–删除字符串左边的指定字符串
SELECT LTRIM('###hello##!##', '#') FROM DUAL;
–删除字符串右边的指定字符串
SELECT RTRIM('###hello##!##', '#') FROM DUAL;
–删除字符串两端的指定字符串
SELECT TRIM('#' FROM '###hello##!##') FROM DUAL;
–将字符串”Bad Luck Bad Gril”中的”Bad”字符串用”Good”替换掉
SELECT REPLACE('Bad Luck Bad Gril', 'Bad', 'Good') FROM DUAL;
–截取字符串”MessageBox”中从第8个位置开始长度为3的字符串
SELECT SUBSTR('MessageBox', 8, 3) FROM DUAL;
–返回n的绝对值
SELECT ABS(-1) FROM DUAL;
–返回大于或等于数值n的最小整数
SELECT CEIL(-7.9) FROM DUAL;
–返回n的余弦值,n为弧度
SELECT COS(30) FROM DUAL;
–返回e的n次幂
SELECT EXP(1) FROM DUAL;
–返回小于或等于n的最大整数
SELECT FLOOR(-2.2) FROM DUAL;
–返回以n1为底n2的对数
SELECT LOG(4, 16) FROM DUAL;
–返回n1除以n2的余数
SELECT MOD(3, 2) FROM DUAL;
–返回n1的n2次方
SELECT POWER(3, 4) FROM DUAL;
–返回舍入小数点右边n2位的n1的值,n2的默认值为0,
–这会返回小数点最接近的整数。
–如果n2为负数,就舍入到小数点左边相应的位置上,n2必须是整数。
SELECT ROUND(3.1415926, 2) FROM DUAL;
–若n为负数,则返回-1,若n为正数,则返回1,若n=0,则返回0.
SELECT SIGN(-22) FROM DUAL;
–返回正弦值,n为弧度
SELECT SIN(30) FROM DUAL;
–返回n的平方根,n为弧度
SELECT SQRT(16) FROM DUAL;
–返回结尾n2位小数的n1的值,n2默认设置为9,当n2为默认设置时,
–会将n1截尾为整数,如果n2为负值,就截尾在小数点左边响应的位上。
SELECT TRUNC(2333.1415, -2) FROM DUAL;
–返回日期d加上i个月之后的结果。其中,i为任意整数
SELECT ADD_MONTHS('2015-2-22', 3) FROM DUAL;
–返回包含日期d月份的最后一天
SELECT LAST_DAY('2016-3-19') FROM DUAL;
–返回d1和d2之间的数目,若d1和d2的日期都相同,或者都是该月的最后一天,
–则返回一个整数,否则返回的结果将包含一个小数。
SELECT MONTHS_BETWEEN('2016-3-19', '2015-3-19') FROM DUAL;
–其中,d1是一个日期数据类型,当时区t1中的日期和时间是d1时,
–返回时区t2中的日期和时间。t1和t2是字符串。
SELECT SYSDATE BJ_TIME, NEW_TIME(SYSDATE, 'PDT', 'GMT') LOS_ANGLES FROM DUAL;
–返回系统当前时间
SELECT SYSDATE FROM DUAL;
CHARTOROWID(s)函数
–该函数将字符串s转换为RWID数据类型
CONVERT(s,aset[,bset])函数
–该函数将字符串s由bset字符集转换为aset字符集
ROWIDTOCHAR()函数
–该函数将ROWID数据类型转换为CHAR类型
TO_CHAR(x[,format])函数
–该函数实现将表达式转换为字符串,format表示字符串格式。
SELECT TO_CHAR(SYSDATE, 'YYYY:MM:DD') FROM DUAL;
–该函数将字符串s转换成date类型,format表示字符串格式,lan表示所使用的语言
SELECT TO_DATE('2015-1-1', 'YYYY-MM-DD') FROM DUAL;
–该函数将返回字符串s代表的数字,返回值按照format格式进行显示,
–format表示字符串格式,lan表示所使用的语言。
–将十六进制数-18f转换为十进制数
SELECT TO_NUMBER('18', 'xxx') AS "十进制数" FROM DUAL;
–计算选择列表的平均值,列表项目可以是一个列或多个列的表达式
SELECT AVG(SAL) AS "平均工资" FROM EMP;
–返回查询结果中的记录数
SELECT COUNT(1) FROM EMP;
–返回选择列表项目中的最大数,列表项目可以使一个列或多个列的表达式。
SELECT MAX(SAL) AS "最高工资" FROM EMP;
–返回选择列表项目中的最小数,列表项目可以使一个列或多个列的表达式。
SELECT MIN(SAL) AS "最低工资" FROM EMP;
–返回选择列表项目中的数值总和,列表项目可以使一个列或多个列的表达式。
SELECT SUM(SAL) AS "工资之和" FROM EMP;
–返回选择列表项目的统计方差,列表项目可以使一个列或多个列的表达式。
SELECT VARIANCE(SAL) AS "统计工资方差" FROM EMP;
–返回选择列表项目中的标准偏差,列表项目可以使一个列或多个列的表达式。
SELECT STDDEV(SAL) AS "工资标准偏差" FROM EMP;