dual 表中只有一个记录,
sysdate 时间函数 , -------select sysdate from dual;
别名:双引号的作用 保持原来的格式
select sysdate as "中国 日期 d f" from dual;
任何含有空值的表达式 计算值 都是空值
字符串连接 ||
select ename||'asjdhjka' from emp
select ename||'asjdh''jka' from emp 但字符串中有'时 用 ''表示一个'
distinct
select distinct emp.deptno from emp;
select distinct emp.deptno,job from emp --emp.deptno,emp.job 的组合唯一
日期比较
-------------------------------------------------
not
select * from empwhere emp.sal not in < 800, 1500 >
转义字符
查找名称中有%的EMP
因为% 是模糊查找的关键符号。所以需要这里需要转义字符帮忙了
SELECT * FROM EMPWHERE ENAME LIKE '%/%%' ESCAPE '/'
排序
--照 deptno 升序, 然后 ename 降序
SELECT EMP.DEPTNO,ENAME FROM EMPORDER BY EMP.DEPTNO ASC, ENAME DESC
常用函数
Lower
upper
chr ---把一个数字 的到相应的ASCII码
ASCII--一个数字得到相应的 字符
round -----四舍五入
to_char --格式控制
SELECT to_char(sal,'$999,999,999.9999') from emp;
$800.0000
SELECT to_char(emp.hiredate,'YYYY/MM/DD HH') from emp;
日期比较
to_date
SELECT emp.hiredate from empWHERE hiredate >to_date('1981-05-1','YYYY-MM-DD');
特定数字格式比较
to_number
select emp.sal from empwhere emp.sal > to_number('$1,250.000','$9999,999.0000')
空值处理
nva
comm 为空值时用 0代替
select sal*12+ nvl(comm,0) from emp;
查询每个部门的最高工资的员工信息
SELECT emp.empno, emp.ename,emp.deptno,emp.salfrom emp join (select deptno, max(sal) sal from emp group by deptno) ton (t.deptno=emp.deptno and t.sal=emp.sal);
选取平均薪水最高的部门
select deptno,avg_sal from (
--选出所有部门的平均薪水
select deptno ,avg(sal) avg_sal from emp group by deptno ) t1wheret1.avg_sal=(
-- 选出最高的平均薪水select max(avg_sal) from (select deptno, avg(sal) avg_sal from empgroup by emp.deptno) )
组函数的嵌套
最多只能嵌套一成
demo
max(avg(sal))