Oracle數據庫的查詢、函數及其轉換

Oracle數據庫的查詢、函數、函數轉換

###一、 基本查詢 ###
select 列名1,列名2… from 表名

1.請查詢出s_emp表中所有的員工信息:

select * from s_emp;

2.請查詢出s_emp表中所有的員工的部門ID,工資:

select DEPT_ID from s_emp;

3.請查詢出s_emp表中所有的員工的年薪:

select salary+COMMISSION_PCT/100*salary from s_emp;

4.請查詢出s_emp表中所有的員工的姓名:
列名不用可’’ 字符內容要加’ ',在sql語句中字符串用 ’ '引號

select FIRST_NAME||' '||LAST_NAME from s_emp;
select FIRST_NAME||' '||LAST_NAME as 姓名 from s_emp;

5.請查詢出s_emp表中所有的員工的工資:

select salary from s_emp;

6.請查詢出s_dept表的部門名稱

select NAME from s_dept;

DISTINCT 用來去除重複項

select DISTINCT name from s_dept;

7.請查詢出s_emp表中所有的員工的部門ID及職稱:

select DEPT_ID,TITLE from s_emp;

8.查詢s_emp表要求輸出員工姓名(firs_name、last_name)和實際工資(基本工資+提成)

select FIRST_NAME||LAST_NAME,nvl(COMMISSION_PCT/100,0)*salary+salary from s_emp;

9.請查詢出s_emp表中dept_id爲41的員工信息 並且 工資大於1000
select 列名1,列名2
from 表名
where 條件

select *
from s_emp
where DEPT_ID=41 and salary>1000;

10.請查詢出s_emp表中dept_id爲41的員工信息 或者 工資大於1000

select *
from s_emp
where DEPT_ID=41 or salary>1000;

11.請查詢出s_emp表中last_name爲Smith的員工的信息:

select *
from s_emp
where last_name = 'Smith';

12.請查詢出s_emp表中部門ID爲50並且工資大於1500的員工的信息:

select *
from s_emp
where DEPT_ID=50 and salary>1500;

13.請查詢出s_emp表中工資在1500到2000之間的員工信息:

方式1

select *
from s_emp
where salary>1500 and salary<2000;

方式2

select *
from s_emp
where salary between 1500 and 2000;

14查詢出s_dept表中region_id爲1,3的部門信息:

select *
from s_dept
where REGION_ID=1 or REGION_ID=3;

select *
from s_dept
where REGION_ID in(1,3);

15.請查詢出s_emp表中姓中含有字母a的員工信息:
–%表示可以有,可以沒有,有可以有多個

select *
from s_emp
where LAST_NAME like '%a%';

16.請查詢出s_emp表姓中第二個字母爲a的員工信息:
– _表示一定有,只能有一位

select *
from s_emp
where LAST_NAME like '_a%';

17.請查詢出當前用戶下所有以‘s_’開頭的表:

select *
from user_tables
where table_name like 's\_%';

18.查詢出s_emp表中非銷售職位的員工信息:

select *
from s_emp
where COMMISSION_PCT is null;

19.查詢出s_emp表中銷售職位的員工信息:

select *
from s_emp
where COMMISSION_PCT is not null;

20.order by 列名 用來完成指定列的排序, 默認升序ASC ,desc表示降序 ,order by 子句一般放在最後

select *
from s_emp
where SALARY>1000
order by salary desc;

二、字符函數

1.LOWER 將字符串轉換成小寫

select lower('WERT') from dual;

2.UPPER 將字符串變爲大寫

select upper('weRt') from dual;

3.INITCAP 將字符串的第一個字母變爲大寫,同時後面的轉化爲小寫

select initcap('weRT') from dual;

三、字符串操作函數

1.CONCAT 拼接兩個字符串,與 || 相同

select concat('ac','bd') from dual;

2.SUBSTR(‘String’,1,3) 取字符串的子串,下標從1開始

select substr('qwert',1,3) from dual;

3.LENGTH以字符給出字符串的長度 給出字符串的長度

select length('asdfghj') from dual;

四、數字函數

1.ROUND(value,precision) 按precision 精度4舍5入

select round(55.55,1) from dual;--precision不賦值,默認精度爲0

2.TRUNC(value,precision) 按precision 截取value

SELECT TRUNC (124.16666, -1) FROM dual; --120

五、日期函數

1.獲取當前系統日期

select sysdate from dual;--格式:07-11月-19

2.將系統當前日期改爲指定日期

select To_date('07-10月-19') from dual;

3.MONTHS_BETWEEN(date2,date1) 給出 Date2 - date1的月數

 select  months_between('01-6月-95','11-1月-94')
 from s_emp;

4.NEXT_DAY ( date,numbe) 給出日期date之後下週第number天的日期

select next_day(date,2) from dual;

六、轉換函數

1.to_char()將日期轉化爲字符串

select to_char(sysdate,'yyyy/MM/dd hh:mm:ss') from dual; --2019/11/07  06:11:24


select to_char(123123,'L999,999,999') from dual;   -- ¥123,123

select * 
from s_emp 
where to_char(START_DATE,'MM')='02';--開始日期是2月份的員工信息

2.to_number(‘String’)字符串轉到數字
to_number(varchar2 or char,’format model’)

3.to_date(‘String’)字符串轉到日期

 select TO_DATE('2019-11-7','yyyy-mm-dd') from dual;

七、轉換函數的嵌套

查詢員工表中manager_id爲空的員工查詢出來,並將空列的值置爲“No Manager”:

select last_name,
nvl(TO_CHAR(manager_id),'No Manager')
from	s_emp
where	manager_id is null;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章