1. 程式人生 > 其它 >使用Oracle資料庫scott使用者測試常用基本查詢語句

使用Oracle資料庫scott使用者測試常用基本查詢語句

技術標籤:Oracleoraclebasic skillscott基本查詢語句

文章目錄

Oracle 基本查詢語句

使用oracle資料庫scott使用者自帶的四張table作為例子進行例項。如下:

  • BONUS
  • DEPT
  • EMP
  • SALGRADE

查詢使用者語言環境與解鎖賬戶

--查詢使用者語言環境
select userenv('language') from dual;--AMERICAN_AMERICA.ZHS16GBK
select * from v$nls_parameters --解鎖scott賬戶並重新設定密碼 alter user scott account unlock;--解鎖使用者 alter user scott identified by tiger;--修改使用者密碼

基本查詢

--=======================基本查詢=======================
--1.查詢出所有emp表中的資訊,並用中二五年進行欄位重新命名
select empno as "員工編號",ename "姓名",job "職位"
,mgr "上級領導編號",hiredate "入職日期",sal "工資",comm "獎金",deptno "部門編號" from emp;--欄位別名用雙引號,其他全部用單引號 --2.查詢emp表中員工的job資訊,並去除重複資訊 select distinct(job) from emp; --3.查詢emp表中員工的全年工資(sal)總和 select sum(sal) from emp; --4.查詢emp表中員工全年收入總和(sal+comm) select ename,12*sal+
nvl(comm,0) from emp;--nvl(comm,0)如果為空則返回0 --5.查詢emp表中員工編號,姓名 -- 輸出格式如下: -- 編號:xxx,姓名:xxx select concat('編號:',empno) from emp;--編號:7369 select '編號:'||empno||',姓名:'||ename from emp;

條件查詢

--=======================條件查詢=======================
--1.查詢工資大於1500的員工
select ename from emp where sal>=1500;
--2.查詢工資大於1500並且有獎金的員工
select ename from emp where sal>=1500 and comm is not null;
--3.查詢工資大於1500或者有獎金的員工
select ename from emp where sal>=1500 or comm is not null;
--4.查詢工資大於1500並且沒有獎金的員工
select ename from emp where sal>=1500 and comm is null;
--5.查詢姓名為SMITH的員工
select * from emp where ename='SMITH'--oracle中大小寫敏感

範圍查詢

--=======================範圍查詢=======================
--1.查詢工資大於1500但小於3000的員工
select * from emp where sal>=1500 and sal<=3000;
select * from emp where sal between 1500 and 3000;
--2.查詢1981-1-1到1981-12-31期間入職的員工
select * from emp where to_char(hiredate,'yyyy-MM-dd') between '1981-01-01' and '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');
--3.查詢員工編號是7369,7654的員工
select * from emp where empno=7369 or empno=7654;
select * from emp where empno in(7369,7654);
--4.查詢員工姓名是SMITH、ALLEN的員工
select * from emp where ename in('SMITH','ALLEN');

模糊查詢

--=======================模糊查詢=======================
--1.查詢所有員工姓名中第二個字元是'M'的員工
select * from emp where ename like '_M%';
--2.查詢員工姓名中帶有'M'的員工資訊
select * from emp where ename like '%M%';
--3.查詢員工編號不是7369的員工資訊
select * from emp where empno <> 7369;
select * from emp where empno != 7369;

排序查詢

--=======================排序查詢=======================
--1.查詢員工的工資進行降序排序
select ename,sal from emp order by sal desc;
--2.查詢員工的工資進行升序排序
select ename,sal from emp order by sal asc;
--3.查詢員工獎金並做降序排序(nulls first / nulls last)
select ename,comm from emp order by comm desc nulls last;
--4.查詢員工工資做降序排序並且將獎金升序排序
select ename,sal,comm from emp order by sal desc,comm asc;

單行函式

字元函式
--========字元函式
--1.將'smith'轉換成大寫
select upper('smith') from dual;--dual偽表,只為了補全oracle語法
--2.將員工姓名轉換成小寫
select lower(ename) from emp;
--3.將員工姓名轉換成首字母大寫
select initcap(ename) from emp;
--4.將'helloword'擷取成'hello'
select substr('helloword',1,5) from dual;--substr(0,5)同義
select substr('helloword',0,5) from dual;
--5.獲取'hello'的字串長度
select length('hello') from dual;
--6.將'hello'中的'l'替換成'x'
select replace('hello','l','x') from dual;
數值函式
--========數值函式
--1.將15.66進行四捨五入
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
--2.將15.66進行截斷
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
--3.對數字15進行求餘
select mod(15,4) from dual;--3
日期函式
--========日期函式
--1.獲取系統時間
select sysdate from dual;
--2.查詢員工進入公司的週數
select ename,(sysdate-hiredate)/7 from emp;
--3.查詢員工進入公司的月數
select ename,months_between(sysdate,hiredate) from emp;
--4.求出員工入職三個月後的日期
select ename,hiredate,add_months(hiredate,3) from emp;
轉換函式
--========轉換函式
--1.將日期格式顯示為yyyy-mmdd hh:mi:ss(去掉補零和24小時顯示)
select to_char(sysdate,'yyyy/MM/dd HH:mm:ss') from dual;--最常用格式yyyy-MM-dd HH:mm:ss
select to_char(sysdate,'yyyyfm/mm/dd hh24:mi:ss') from dual;
--2.顯示成年月日格式
select to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'mm')||'月'||to_char(sysdate,'dd')||'日' from dual;
--3.將字串'1981-1-1'轉換成日期格式
select to_date('1981-1-1','yyyy-MM-dd') from dual;
select to_number('99') from dual; --字元轉數字
select to_char(99) from dual;--數字轉字元
通用函式
--========通用函式
--1.空值處理函式
select comm,nvl(comm,0) from emp;
select nvl2(null,'1','2') from dual;--如果為null則返回2
select nvl2('xxx','1','2') from dual;--如果不為null則返回1
條件表達
--========條件表示式
--1.查詢員工的job內容並轉換成中文顯示
select ename,decode(job,'CLERK','櫃員','SALESMAN','銷售','MANAGER','管理','其他') from emp;
select ename,case when job='CLERK' then '櫃員' when job='SALESMAN' then '銷售' else '其他' end from emp;

多行(聚合)函式

--=======================多行(聚合)函式=======================
--1.查詢所有員工記錄數
select count(0) from emp;
--2.查詢獎金的總數
select sum(comm) from emp;
--3.查詢最低工資
select min(sal) from emp;
--4.查詢最高工資
select max(sal) from emp;
--5.查詢平均工資
select avg(sal) from emp;
--6.查詢部門編號為20的總工資
select sum(sal) from emp where deptno=20;

分組函式

--=======================分組函式=======================
--1.查詢部門編號及人數
select deptno,count(0) from emp group by deptno;
--2.查詢每個部門編號及平均工資
select deptno,avg(sal) from emp group by deptno;
--3.查詢部門名稱、部門編號、平均工資
select dname,dept.deptno,avg(sal) from dept,emp where dept.deptno=emp.deptno group by dname,dept.deptno order by dept.deptno asc;
--4.查詢出部門人數大於5的部門
select deptno,count(0) from emp group by deptno having count(0)>=5;
--5.查詢部門編號、部門名稱、平均工資且平均工資大於2000的記錄
select emp.deptno,dname,avg(sal) from dept,emp where emp.deptno=dept.deptno group by emp.deptno,dname having avg(sal)>2000 order by emp.deptno asc;