字符和日期 --字符和日期都要包含在单引号中 --字符大小写敏感,日期格式敏感 --默认的日期格式是DD-MON-RR--查询当前系统时间 SQL> select sysdate from dual;
--查询工资在1000~2000之间的员工信息 SQL> select * from emp where sal>=1000 and sal<2000; SQL> select * from emp where sal between 1000 and 2000;--查询10号和20号部门的员工信息 SQL> select * from emp where deptno in (10,20); --注意:in关键字后面是一个集合,11号部门不会被搜索出来--查询不是10号和20号部门的员工信息 SQL> select * from emp where deptno not in (10,20);
like关键字 --使用like运算选择类似的值 --选择条件可以包含字符或数字%代表零个或者多个字符(任意个字符)_代表一个字符--查询员工信息(名字中含有m字符的) SQL> select * from emp where ename like '%M%'; --注意:单引号中的字符区分大小写--查询员工信息(名字中含有4个字符的) SQL> select * from emp where ename like '____';关键字escape --回避特殊符号:使用转义字符--查询名字中含有'_'的字符串 SQL> select * from emp where ename like '%\_%' escape '\';
空值的第三点:如果集合中含有空值,不能使用not in 操作符,但是可以使用in操作符 --注意null不是空值 SQL> select * from emp where deptno in (10,null); --注意in关键字下sql会自动忽略null SQL> select * from emp where deptno not in (10,null); --not in关键字下使用null无法查出任何数据
order by子句 --使用order by子句排序ASC:升序(默认)DESC:降序 --order by子句在select语句结尾 --order by后面 +列名 表达式 别名 序号--查询10号部门员工信息,按薪水排序 SQL> select * from emp where deptno=10 order by sal desc;--按别名排序 SQL> select ename,sal*12 年薪 from emp where deptno=10 order by 年薪 desc;--按表达式排序 SQL> select * from emp where deptno=10 order by sal*12 desc;--按序号排序 SQL> select * from emp where deptno=10 order by 1 desc; --注意sql语句中列的序号从1开始,不是0--多列排序 SQL> select * from emp order by deptno desc,sal;先按部门降序排序,再按工资升序排序空值的第四点:排序时,如果将空值排在最后 SQL> select * from emp order by comm desc;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ---------- --------- ---------- ------------ ------ ------ ------27934 MILLER CLERK 7782 23-1月 -82 1300 107369 SMITH CLERK 7902 17-12月-80 800 201 ton_abc 8000 107902 FORD ANALYST 7566 03-12月-81 3000 207900 JAMES CLERK 7698 03-12月-81 950 307876 ADAMS CLERK 7788 23-5月 -87 1100 207566 JONES MANAGER 7839 02-4月 -81 2975 207698 BLAKE MANAGER 7839 01-5月 -81 2850 307782 CLARK MANAGER 7839 09-6月 -81 2450 107788 SCOTT ANALYST 7566 19-4月 -87 3000 207839 KING PRESIDENT 17-11月-81 5000 107654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 307521 WARD SALESMAN 7698 22-2月 -81 1250 500 307499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 307844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30--需要加上关键字nulls last SQL> select * from emp order by comm desc2 nulls last3 ;