今天我們來複習一下oracle的基礎查詢,所有的查詢都基於SCOTT用戶下的emp表
1.去重
select distinct(job) FROM emp;
2.全年收入總和(工資加獎金)
select ename,sal*12+nvl(comm,0) from emp;
3.查詢員工表員工編號,姓名,輸出格式如下:編號:XXX,姓名:XXX
select concat(concat('編號:',empno),concat('姓名:',ename)) from emp;
or
select '編號:'||empno||',姓名:'||ename from emp;
4.查詢1981-1-1到1981-12-31入職的員工
select * from emp where hiredate between to_date('1981-1-1','yyyy-MM-dd') and to_date('1981-12-31','yyyy-MM-dd');
5.查詢僱員的獎金並按降序排列
select ename,comm from emp order by comm desc nulls last;
--nulls last空值在後,默認是nulls first
6.查詢僱員的工資做降序排列,並且在其中獎金部分是升序排列
select ename,sal,comm from emp order by sal desc,comm asc;
--結果就是以工資爲準,工資一樣的,按獎金排
7.將smith轉換成大寫
select upper('smith') from dual;
8.將ename全轉成小寫
select lower(ename) from emp;
9.首字母大寫
select initcap(ename) from emp;
10.'helloworld’截取成hello
select substr('helloworld',0,5) from dual;
11.將hello中l用x替換
select replace('hello','l','x') from dual;
12.數值函數
round函數
select round(15.66,-2) from dual; --0
select round(15.66,-1) from dual; --20
select round(15.66,0) from dual; --16
select round(15.66,1) from dual; --15.7
select round(15.66,2) from dual; --15.66
trunc函數
select trunc(15.66,-2) from dual; --0
select trunc(15.66,-1) from dual; --10
select trunc(15.66,0) from dual; --15
select trunc(15.66,1) from dual; --15.6
select trunc(15.66,2) from dual; --15.66
對15/3求餘
select mod(15,3) from dual;
13.日期函數
查詢僱員進入公司的週數
select ename,(sysdate-hiredate)/7 from emp;
查詢僱員進去公司的月數
select ename,months_between(sysdate,hiredate) from emp;
三個月後的日期
select ename,hiredate,add_months(hiredate,3) from emp;
15.轉換函數
將系統日期顯示爲2020-03-18 02:38:33格式
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
如果想要24小時制並去掉月份前面的0的話
select to_char(sysdate,'yyyyfm-mm-dd hh24:mi:ss') from dual;
將字符串’1982-1-1’轉成日期
select to_date('1982-1-1','yyyy-MM-dd') from dual;
16.條件表達式
查詢員工的job內容並轉成中文顯示
select ename,decode(job,'CLERK','櫃員','其他') from emp;
或者用case when的方式
select ename,case job when 'CLERK' THEN '櫃員'
when 'SALESMAN' then '銷售'
else '其他'
end from emp;
17.分組函數
查詢部門名稱,部門編號,平均工資
select dname,emp.deptno,avg(sal) from dept,emp where dept.deptno=emp.deptno group by emp.deptno,dname;
查詢出部門人數大於5的部門
select deptno,count(*) from emp group by deptno having count(*)>5;
--group by 之後再想篩選,必須用having
18.外連接
查詢員工編號,姓名,領導編號,領導姓名,包括沒有領導的
select e1.empno,e1.ename,e2.empno,e2.ename from emp e1 left join emp e2 on e1.mgr=e2.empno;
or
select e1.empno,e1.ename,e2.empno,e2.ename from emp e1,emp e2 where e1.mgr=e2.empno(+)
查詢出所有部門信息(包括沒員工的部門)及部門下的員工信息
select * from emp,dept where emp.deptno(+)=dept.deptno;
找到員工表中工資最高的前三名
select rownum,e.* from (select emp.* from emp order by sal desc)e where rownum<=3;
--rownum不能做大於
分頁公式
PageNo=1 --第一頁
PageSize=3 --每頁3條記錄
select * from (select rownum r,e.* from (select * from 表名 order by 列名 desc)e)e1 where r>(PageNo-1)*PageSize and r<=PageNo*PageSize;
19.集合運算
查詢工資大於1200並且job是SALESMAN(使用intersect)
select * from emp where sal>1200
intersect
select * from emp where job='SALESMAN'
查詢工資大於1200或者job是SALESMAN(使用UNION)
select * from emp where sal>1200
union
select * from emp where job='SALESMAN'
求工資大於1200和job是SALESMAN的差集(minus)
select * from emp where sal>1200
minus
select * from emp where job='SALESMAN'
查詢出有員工的部門(exists)
select * from dept where exists(select * from emp where dept.deptno=emp.deptno)
--當括號裏面的語句爲真,前面select纔有結果