語法:
select * from +表名;
例:select * from emp;
select 列1,列2 … from + 表名;
例:select ename, job from emp
select 列1,列2 … from + 表名 where + 條件;
請展示 部門編號為10的員工資訊
select * from emp where deptno = 10 ;
**
知識點
例題:=,>,<,>=,<=,<>,!=
請找出 工資 大于2500的員工資訊
select * from emp where sal > 2500;
不在20號部門的員工編號和部門編號
select empno,deptno from emp where deptno <> 20;
not 取反 is null is not null
請展示沒有獎金 的 員工編號,員工姓名,員工工資 select a.*,empno 員工編號 ,ename,sal from
emp a where comm is null;
不在20號部門的 員工編號和部門編號
select empno,deptno from emp where deptno <> 20;
and / or /between and 在…之間
部門編號為20的員工里,工資大于2500的 員工資訊
select * from emp where deptno = 20 and sal >2500;
部門編號為20的員工或者工資大于2500的 員工資訊
select * from emp where deptno = 20 or sal >2500;
練習
1.顯示工資大于1500的銷售資訊 SALESMAN
select * from emp where job = ‘SALESMAN’ and sal >1500;
2.工資在1000-2000之間的員工資訊
select * from emp where sal between 1000 and 2000;
注:between and 包含邊界值
3.展示員工編號在7300- 7500之間的員工資訊
select * from emp where empno between 7300 and 7500;
數值型別的計算
±*/()
請展示年薪大于1萬5的 員工資訊
select * from emp where sal *12 >15000;
連接 ||
select empno,ename,empno||ename from emp ;
員工編號為:empno的員工是:ename.
select ‘員工編號為:’||empno||’ 的員工是:’||ename from emp ;
集合 in (值1,值2…)
部門10和部門20的員工資訊
方法1:select * from emp where deptno =10 or deptno = 20;
方法2:select * from emp where deptno in (10,20);
請顯示工種為SALESMAN和CLERK 的員工編號,員工姓名
select empno,ename from emp where job in (‘SALESMAN’,‘CLERK’);
any / all 任意一個/所有
any(1000,2000,3000) 大于集合中任意一個 等價于 大于集合里面的最小值
<any
all(1000,2000,3000)大于集合中所有的值 等價于 大于集合里面的最大值
<all
like /not like 模糊匹配
‘’ 精確匹配
請找出員工中以S開頭的員工資訊
select * from emp where ename like ‘S%’;
請找出A開頭的,倒數第二位是E的員工資訊
select * from emp where ename like ‘A%E_’;
9… 排序語法
select * from 表名 where 條件 order by + 列 asc | desc 升序 | 降序 ) order by 后是按照列的先后順序排序的
注:asc是默認的,可以不寫
例1:select * from emp where deptno = 10 order by sal asc;
例2:按照部門、姓名升序排序 | 降序排序
select * from emp order by deptno desc ,ename desc ;
聚合函式
sum() avg() count() max() min()
例:10號部門的員工工資總和
select sum(sal) from emp where deptno =10;
練習:
求出20號部門的最大工資、工資總和、平均工資
select max(sal),sum(sal),avg(sal) from emp where deptno =20;
求員工的獎金總和
select sum(comm) from emp;
求多少個員工有獎金
select count(comm) from emp;
分組
select 列1,列2 聚合函式…
from 表名
where 條件
group by 列1,列2…
having 條件
注:1)group by 后面的列是有順序的
匯總
select 列1,列2,聚合函式…
from 表名 where 條件
group by 列1,列2…
having 條件
order by
列1,列2… asc|desc;
注:對group by 后的結果進行條件過濾, 有having必有group by
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/163714.html
標籤:非技術區
