天天看點

oracle 簡單查詢,限定查詢及排序簡單查詢限定查詢排序

    SQL(structured Query Language  結構化查詢語言)是一種資料庫查詢和程式設計語言,用于存取資料以及查詢、更新和管理關系資料庫系統。

SQL語言包含4個部分:

※資料定義語言(DDL),例如:CREATE、DROP、ALTER等語句。

※資料操作語言(DML),例如:INSERT、UPDATE、DELETE語句。

※資料查詢語言(DQL),例如:SELECT語句。

※資料控制語言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等語句。

簡單查詢

簡單的SQL查詢語句的文法格式是:

Select *|字段清單别名

From 表名稱;

-- 查詢全部列
select * from emp;

-- 查詢指定列
select empno, ename, job from emp;

-- 指定列别名
select empno 編号, ename 姓名, job 工作 from emp;
select empno as 編号, ename as 姓名, job as 工作 from emp;

-- 去重複結果
select job from emp;
select distinct job from emp;

-- 連接配接列結果
select '編号是' || empno || '雇員的' || '姓名是:'|| ename || ', 工作是:'|| job from emp;

-- 簡單的四則運算
select sal*12 年薪 from emp;
           

限定查詢

限定查詢的文法格式為

Select{distinct}*|具體的列名 别名

From 表名稱

{Where 條件語句};

-- 查詢出工資大于1500 的所有員工
select * from emp where sal > 1500;

-- 查詢每月可以得到獎金的雇員資訊
select * from emp where comm is not null; 

-- 查詢沒有獎金的職工資訊
select * from emp where comm is null;

-- 查詢出基本工資大于1500,并且可以領取獎金的職工資訊
select * from emp where sal > 1500 and comm is not null;

-- 查詢出基本工資不大于1500,同時不可以領取獎金的職工的資訊
select * from emp where sal <= 1500 and comm is null;

-- not 可以對條件取反,把條件真變為假,把假變為真 
select * from emp where not(sal > 1500 or comm is not null);

-- 查詢基本工資大于等于1500,但是小于等于3000的職工的全部資訊
select * from emp where sal >= 1500 and sal <= 3000;

-- between 關鍵字,sql中提供了一個專門的指定範圍的過濾語句 BETWEEN … AND … 格式為:BETWEEN 最小值 AND 最大值  
select * from emp where sal between 1500 and 3000;

-- 查詢出生在1981年1月1日到12月31日的雇員資訊
select * from emp where hiredate between date '1981-01-01' and date '1981-12-31';
select * from emp where hiredate between '1-1月-81' and '31-12月-81';
select * from emp where hiredate between '1-1月-1981' and '31-12月-1981';

-- 查詢出姓名是SMITH 的雇員資訊
select * from emp where ename = 'SMITH';

-- 查詢出雇員編号是 7369,7499,7521的雇員的資訊
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;

-- in 關鍵字 in(val1, val2, val3 ... valn)
select * from emp where empno in (7369, 7499, 7521);

-- 查詢出雇員編号不是7369,7499,7521的雇員的資訊
select * from emp where empno not in(7369, 7499, 7521);

-- 查詢出姓名為 SMITH,ALLEN,KING的雇員資訊
select * from emp where ename in ('SMITH','ALLEN', 'KING');

-- 查詢出雇員第二個字母為“L”的雇員資訊
select * from emp where ename like '_L%';

-- 查詢出雇員姓名以字母“S”開頭的雇員資訊
select * from emp where ename like 'S%';

-- 查詢出雇員姓名包含字母“S”的雇員資訊
select * from emp where ename like '%S%';

-- 查詢入職年份為81年的雇員資訊
select * from emp where hiredate like '%81';

-- 查詢工資值中包含數字5的雇員資訊
select * from emp where sal like '%5%';

-- 查詢所有部門号不是10的雇員資訊
select * from emp where deptno <> 10;
select * from emp where deptno != 10;
           

排序

排序的文法格式為

Select {distinct} *|具體的列 别名

From 表名稱

{Where 條件}

{Order By  排序字段1,排序字段2 ASC|DESC}

-- 按工資升序對雇員資訊進行排序
select * from emp order by sal asc;

-- 按工資降序對雇員資訊進行排序
select * from emp order by sal desc; 

-- 檢索出部門号為30的雇員資訊,并按照工資降序,工資相同則按照入職日期升序排列
select * from emp where deptno = 30 order by sal desc, hiredate asc;
select * from emp where deptno = 30 order by sal desc, hiredate;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where deptno = 30 order by 6 desc, 5 asc;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where deptno = 30 order by 6 desc, 5;
select * from emp where deptno = 30 order by 6 desc,5;
           

繼續閱讀