Oracle基礎(chǔ)學(xué)習(xí)之簡單查詢和限定查詢
本文主要介紹的是關(guān)于Oracle中的簡單查詢和限定查詢,下面話不多說,來一起看看吧。
SQL:
1,DML(數(shù)據(jù)操作語言):主要指的是數(shù)據(jù)庫的查詢與更新的操作,查詢操作是整個(gè)sql語法 中最麻煩也是筆試中最常用的部分。
2,DDL(數(shù)據(jù)定義語言):主要指的是數(shù)據(jù)對象的創(chuàng)建(表、用戶、)例如:creat.需要相關(guān)的設(shè)計(jì)范式。
3,DCL(數(shù)據(jù)控制語言):主要進(jìn)行權(quán)限的操作(需要結(jié)合用戶來觀察),此部分由DBA負(fù)責(zé)。
簡單查詢:
1,利用select
子句控制要顯示的數(shù)據(jù)列:
select empno,ename,ename,job,sal from emp;
2,可以使用distinct
來消除重復(fù)的數(shù)據(jù)行顯示:
select distinct job from emp;
3,select
子句可以進(jìn)行四則運(yùn)算,可以直接輸出常量內(nèi)容,但是對于字符串使用單引號數(shù)字直接編寫,日期格式按照字符格式:
select empno,ename,(sal*15+(200+100)) income from emp;
4,||負(fù)責(zé)輸出內(nèi)容連接此類的操作很少直接在查詢中出現(xiàn):
select empno||ename from emp;
5,where
子句一般都寫在from
子句之后,但是是緊跟著from
子句之后執(zhí)行的,where
子句控制顯示數(shù)據(jù)行的操作,而select
控制數(shù)據(jù)列,select
子句要落后于where
子句執(zhí)行,所以在select
子句之中定義的別名無法在where
中使用。
限定查詢:
1,關(guān)系運(yùn)算符:
select * from emp where sal>1500; select * from emp where ename ='SMITH' select empno,ename,job from emp where job<>'SALESMAN';
2,邏輯運(yùn)算符:
select * from emp where sal>1500 and sal<3000; select * from emp where sal>2000 or job='CLERK'; select * from emp where not sal >=2000;
3,范圍查詢:
select * from emp where sal between 1500 and 2000; select * from emp where hiredate between '01-1月-1981'and'31-12月-1981';
4,空判斷(空在數(shù)據(jù)庫上表示不確定,如果在數(shù)據(jù)列使用null不表示0)
select * from emp where comm is not null;
5,IN操作符(類似于between and
而in給出的是指定的范圍):
select * from emp where empno in (7369,7566,7788,9999);
關(guān)于not in
與null
的問題:
在使用not in
進(jìn)行范圍判斷的時(shí)候,如果范圍有null
,那么不會有任何結(jié)果返回。
6,模糊查詢:
“-”:匹配任意一位字符;
“%”:匹配任意的0,1,,或者多位字符;
查詢姓名是以字母A開頭的雇員信息:
select * from emp where ename like 'A%'
查詢姓名第二個(gè)字母是A的雇員信息:
select * from emp where ename like '_A%';
查詢姓名任意位置是A的雇員信息:
select * from emp where ename like '%A%';
查詢排序:
ASC(默認(rèn)):按照升序排列;
DESC: 按照降序排列;
查詢所有的雇員信息,要求按照工資的由高到低:
select * from emp order by sal desc;
查詢每個(gè)雇員的編號,姓名,年薪,按照年薪由低到高排序:
select empno ,ename,sal*12 income from emp order by income;
語句的執(zhí)行順序:from
- where
-select
- order by
基礎(chǔ)練習(xí):
1,選擇部門30中的所有員工:
select * from emp where deptno=30;
2,列出所有辦事員(clerk)的姓名,編號,和部門編號:
select ename,empno,deptno from emp where job='CLERK';
3,找出傭金高于薪金的60%的員工:
select * from emp where comm>sal*0.6 ;
4,找出部門10中所有的經(jīng)理(manager)和部門20中所有的辦事員(clerk):
select * from emp where (deptno=10 and job='MANAGER' )or(deptno=20 and job='CLERK' );
5,找出部門10中所有的經(jīng)理(manager),部門20中的所有辦事員(clerk),以及既不是經(jīng)理又不是辦事員但是工資高于等于2000的所有員工資料:
select * from emp where (deptno=10 and job='MANAGER')or(deptno=20 and job='CLERK')or(job! ='MANAGER'and job!='CLERK' and sal>=2000);
select * from emp where (deptno=10 and job='MANAGER')or(deptno=20 and job='CLERK')or(job not in ('CLERK','MANAGER') and sal>=2000);
6,找出收取傭金的員工的不同工作:
select distinct job from emp where comm is not null;
7,找出收取傭金或者收取的傭金低于100的員工:
select distinct job from emp where comm is null or comm<100;
8,顯示不帶有“R”的員工姓名:
select * from emp where ename not like '%R%';
9,顯示姓名字段含有A的所有員工姓名,顯示的結(jié)果按照基本的工資由高到低,如果工資相同,則按照雇傭年限由早到晚,如果雇傭日期相同,則按職位排序:
select * from emp where ename like '%A%' order by sal desc,hiredate asc,job;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Oracle?range時(shí)間范圍自動分區(qū)的創(chuàng)建方式
這篇文章主要介紹了Oracle??range時(shí)間范圍自動分區(qū)的創(chuàng)建方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Oracle Portal及其門戶網(wǎng)站開發(fā)概述
Oracle Portal及其門戶網(wǎng)站開發(fā)概述...2007-03-03Oracle創(chuàng)建Database Link的兩種方式詳解
Oracle數(shù)據(jù)庫如何創(chuàng)建Database Link呢?本文我們主要就介紹一下這部分內(nèi)容,Oracle數(shù)據(jù)庫創(chuàng)建Database Link有兩種方式,一種是通過菜單,一種是通過SQL2016-02-02Oracle數(shù)據(jù)庫"記錄被另一個(gè)用戶鎖住"解決方法(推薦)
數(shù)據(jù)庫是一個(gè)多用戶使用的共享資源。當(dāng)多個(gè)用戶并發(fā)地存取數(shù)據(jù)時(shí),在數(shù)據(jù)庫中就會產(chǎn)生多個(gè)事務(wù)同時(shí)存取同一數(shù)據(jù)的情況。這篇文章主要介紹了Oracle數(shù)據(jù)庫"記錄被另一個(gè)用戶鎖住"解決方法2018-03-03Oracle ASM數(shù)據(jù)庫故障數(shù)據(jù)恢復(fù)解決方案
這篇文章主要介紹了Oracle ASM數(shù)據(jù)庫故障數(shù)據(jù)恢復(fù)解決方案,需要的朋友可以參考下2017-04-04ORACLE 11g從 11.2.0.1升級到11.2.0.4 詳細(xì)實(shí)戰(zhàn)教程
這篇文章主要介紹了ORACLE 11g從 11.2.0.1升級到11.2.0.4 詳細(xì)實(shí)戰(zhàn)教程,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03