SQL基礎(chǔ)查詢

一、SQL簡單查詢
1.所謂簡單查詢就是查詢一張數(shù)據(jù)表中所有數(shù)據(jù)行的內(nèi)容。
①FROM 表名稱 [別名]
②SELECT [distinct] * | 列名稱 [別名],列名稱 [別名].....
全部查詢使用通配符 “ * ”完成
select* from emp;

[E)9Z]89ZL14PWDMNKN_VMO.png

查詢幾個固定的數(shù)據(jù)列,不再顯示所有的列
select empno,ename from emp;
[E)9Z]89ZL14PWDMNKN_VMO.png

2.查詢的基本內(nèi)容
①使用distinct消除重復數(shù)據(jù)行顯示的內(nèi)容
select distinct ename from emp;
[E)9Z]89ZL14PWDMNKN_VMO.png

②除了簡單的查詢外,也可以對查詢的數(shù)據(jù)列進行簡單四則運算

查詢雇員的編號、姓名、年薪

select empno,ename,sal*12 from emp;

[E)9Z]89ZL14PWDMNKN_VMO.png

但有時候查詢出來的列不清楚表達的是什么,這是可以使用別名,但不建議使用中文。
select empno,ename,sal*12 income from emp;
[E)9Z]89ZL14PWDMNKN_VMO.png

雇員每年的年薪,5個月200元的電話補貼,4個月300元的高溫補貼,2個月的500出差補貼
select empno,ename,(sal*12 +5*200+4*300+2*500) income from emp;
[E)9Z]89ZL14PWDMNKN_VMO.png

如果執(zhí)行四則運算依然是先乘除后加減。

③查詢常量
對于常量有如下三點說明:

? 如果常量是字符串,則要求使用“ ‘ ”,例如:'hello';
? 如果常量是數(shù)字,則直接編寫,例如 : 10;
? 如果常量是日期,則按照日期的風格編寫,使用“xx日-xx月-xx年”

直接查詢常量
select '雇員',empno,ename from emp;

[E)9Z]89ZL14PWDMNKN_VMO.png

④也可以在select 中使用 “||”連接連個列
select empno||ename from emp;

[E)9Z]89ZL14PWDMNKN_VMO.png

查詢雇員編號姓名收入,使用常量顯示

select '編號:'|| empno ||',姓名:'||ename||',收入:'||sal income from emp;
[E)9Z]89ZL14PWDMNKN_VMO.png

二.SQL限定查詢
1.關(guān)系運算
查詢職位不等于SALESMAN,有兩種表達方式,sql查詢job中嚴格區(qū)分大小寫

select empno,ename,job
from emp
where job!='SALESMAN';
select empno,ename,job
from emp
where job <> 'SALESMAN';
關(guān)系運算符.png

2.邏輯運算符
①與
查詢工資大于1500和小于3000

select*
from emp
where sal>1500 and sal <3000;
and.png

②或
查詢工資大于1500或小于3000

select*
from emp
where sal>1500 or sal <3000;
or.png

③非

3.空判斷
查詢所有領(lǐng)取傭金的雇員信息(傭金存在,不為空)

select* from emp where comm is not null;
select* from emp where not comm is null;

任何數(shù)據(jù)庫,空的操作只能使用以上兩個標記來判斷

4.IN操作符
要求查詢雇員編號是7369,7566,9999的雇員信息

select* from emp where empno=7369 or empno=7566 or empno=9999;
select* from emp where empno  in(7369,7566,9999);

在指定的操作范圍內(nèi)使用in,操作比較簡單


in.png

不在操作的范圍內(nèi)使用NOT IN

select* from emp where empno not in (7369,7566,9999);
select* from emp where not empno in (7369,7566,9999);
關(guān)于NOT IN 與 NULL的問題

①使用in包含有null(沒有任何影響)

select* from emp where empno in (7369,7566,null);

②使用not in里面包含有null

 select* from emp where empno not in(7369,7566,null);
not in.png

使用not in是查詢部分數(shù)據(jù),里面有null就變?yōu)榱巳坎樵?不會有任何結(jié)果返回

5.模糊查詢LIKE
在使用like的時候可以使用連個通配符:
“—” :匹配任意的一個字符;
“%”:匹配任意的零位、一位或多位字符;
①查詢以字母A開頭的任意字符

select* from emp where ename like 'A%';
.png

②查詢第二個字符是A的任意字符

select* from emp where ename like '_A%';
02.png

③查詢姓名中任意位置包含A的字符

  select* from emp where ename like '%A%';
03.png

三.排序
針對查詢結(jié)果進行排序

ORDER BY 字段 [ASC|DESC]

對于排序有兩種方式:
ASC 升序 默認情況下是升序
DESC 降序
①查詢雇員信息,要求工資由高到低排序

select* from emp order by sal desc;

②查詢銷售人員的信息,要求按照雇用日期由早到晚進行排序

select* from emp where job ='SALESMAN' order by hiredate desc;

③查詢由工資高到低,如果工資相同,則按照雇用日期由早到晚排序

select* from emp order by sal desc ,hiredate asc;
select* from emp order by sal desc ,hiredate;

④查詢每個雇員員工的編號,姓名,年薪,并按照年薪由高到低排列

select empno,ename,sal*12 income
from emp
order by income;

⑤查詢傭金高于60%的所有員工

select* from emp where comm>sal*0.6;
comm.png

⑥查詢部門10中的所有經(jīng)理或部門20中的所有經(jīng)理

select*
from emp
where (deptno=10 and job='MANAGER' or deptno=20 and job='CLERK');

⑦查詢部門編號是10的經(jīng)理,或者部門編號是20的職員,或者工資大于2000但不包括經(jīng)理、職員

select*
from emp
where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK') or (job not in ('MANAGER','CLERK')AND sal>=200);

⑧收取傭金的員工的不同工作

select distinct job
from emp
where comm is not null;

⑨顯示姓名字段的任何位置包含'A'的所有員工的姓名,顯示結(jié)果按照基本工資由高到低排序,基本工資相同按照雇用年限由早到晚排序,雇用年限相同按照職位升序。
第一步:顯示姓名字段的任何位置包含'A'的所有員工的姓名

select* from emp where ename like '%A%';

第二步:顯示結(jié)果按照基本工資由高到低排序

select* from emp where ename like '%A%' order by sal ;

第三步:基本工資相同按照雇用年限由早到晚排序

select* from emp where ename like '%A%' order by sal , hiredate ;

第四步:雇用年限相同按照職位升序

select* from emp where ename like '%A%' order by sal , hiredate ,job ;

對于較復雜的題目,分步進行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容