基本查詢語句
from 子句
指定從哪張表范圍內查找數據
select 字段名from 表名
查詢對應字段的數據
where子句
where條件
根據指定條件的對查詢記錄進行過濾
select子句
select字段名 查詢對應字段的數據
--查詢工資大于3000的員工信息
select * from emp where salary >300
順序是 from→where→select
查詢條件
使用 大于,小于,大于等于,小于等于,等于(=),不等于(!=)、不等于(<>)
使用and or 關鍵字
查詢不是20部門且工資大于2000的員工信息
select *from emp where sal>2000 and deptno !=20;
or與and優先級問題:or的優先級是最低的,如果需要提高or的優先級必須加上()
使用in,not in關鍵字
查詢20或者30部門中工資>2000的員工信息
select * from emp where deptno in (20,30) and sal >2000
使用like關鍵字
模糊查詢
like不會單獨使用,配合_ 或者 %一起使用
_ 表示單個匹配,%表示多個匹配
select * from emp where ename like '__A%';
使用 is null 和 is not null
使用between ……and……關鍵字
在兩個值之間,也可以使用>=值1 and <=值2 兩端都包括
select * from emp where sal between 1000 and 2000
查詢81年入職的員工信息
select * from emp where hiredate between '01-1月-81' and '31-12月-81'
select *from emp where extract(year from hiredate)=1981;
使用 any、all關鍵字
任意一個,>最小一個即可
<任意一個,<最大一個即可
所有,>最大一個即可
<所有,<最小一個即可
查詢條件中,使用表達式和函數
查詢年薪大于30000
select* from emp where sal*12 >3000
查詢工資大于30部門所有工資的員工信息
select * from emp where sal>all(select sal from emp where deptno=20);
使用distinct關鍵字
去除查詢結果中重復的數據
查詢員工所在的部門編號
select distinct deptno from emp
基本sql高級組成部分
order by 子句
排序
在一條查詢語句中,只有一次,且放在語句最后
查詢20部門員工信息,按員工工資順序排序
select * from emp where deptno=20 order by sal
asc:順序排序關鍵字(默認的排序規則,asc可缺省)
desc:倒序排序關鍵字
多字段排序
order by 字段名1,字段名2……
先將結果數據按照字段1排序,再排完序的結果上,再按照字段2排序
select * from emp order by deptno ,sal desc