基本查詢(xún): -
- select * from emp;
-
- set linesize 120;
-
- col empno for 9999;
-
- col ename for a8
-
- set pagesize 30;
-
- select empno,ename,sal,sal*12,comm,sal*12+comm from emp;
-
- select empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0) from emp;
-
- select empno as "員工編號(hào)",ename "姓名",sal 月薪,comm,sal*12+nvl(comm,0)年收入 from emp;
-
- select distinct deptno from emp;
-
- select distinct deptno,job from emp;
-
- select ename ||'的薪水是'||sal from emp;
注意: - SQL 語(yǔ)言大小寫(xiě)不敏感。
- SQL 可以寫(xiě)在一行或者多行
- 關(guān)鍵字不能被縮寫(xiě)也不能分行
- 各子句一般要分行寫(xiě)。
- 使用縮進(jìn)提高語(yǔ)句的可讀性。
過(guò)濾和排序 -
- select * from v$nls_parameters;
-
- alter session set NLS_DATE_FORMAT='yyyy-mm-dd';
-
- select * from emp where hiredate='1981-11-17';
-
- select * from emp where hiredate=to_date('1981-11-17','yyyy-mm-dd');
-
- select * from emp where sal>=1000 and sal<=2000;
- select * from emp where sal between 1000 and 2000;
-
- select * from emp where deptno=10 or deptno=20;
- select * from emp where deptno in (10,20);
-
- select * from emp where ename like 'S%';
-
- select * from emp where ename like '____';
-
- select * from emp where ename like '%\_%' escape '\';
-
- select * from emp where comm is not null;
-
- select * from emp order by sal;
-
- a desc
-
- select empno,ename,sal,sal*12 from emp order by sal*12;
-
- select empno,ename,sal,sal*12 年薪 from emp order by 年薪;
-
- select empno,ename,sal,sal*12 from emp order by 4;
-
- select * from emp order by deptno,sal;
-
- select * from emp order by deptno desc,sal desc
-
- select * from emp order by comm desc nulls last;
-
- set feedback off/set feedback on
排序的規(guī)則 : - 可以按照select語(yǔ)句中的列名排序
- 可以按照別名列名排序
- 可以按照select語(yǔ)句中的列名的順序值排序
- 如果要按照多列進(jìn)行排序,則規(guī)則是先按照第一列排序,如果相同,則按照第二列排序;以此類(lèi)推
|