數(shù)據(jù)查詢:
前面已經(jīng)創(chuàng)建了表碰声,插入了數(shù)據(jù)部宿,現(xiàn)在可以進行數(shù)據(jù)查詢。
SELECT * FROM STUDENT; --查詢student表所有數(shù)據(jù)笔诵。
SELECT sno,sname,ssex,sage,sdept FROM student; --同上钟病,可以查
所有列萧恕,也可以直接用*替代所有列名字屹徘。
SELECT sno,sname FROM student; --查詢部分列
SELECT sname,2017-sage FROM student; --查詢名字和出生的年份
SELECT sname,2017-sage as 出生年份 FROM student;
--查詢名字和出生的年份簿煌,列名為出生年份。
SELECT sname,2017-sage 出生年份 FROM student; --同上
SELECT sname,2017-sage 出生年份,'aaa' FROM student; --同上鉴吹,且追加輸出一列aaa
SELECT '名字是:' || sname FROM student; --查詢名字姨伟,輸出帶 名字是: 前綴的。
SELECT sno FROM SC; --查詢所有sno拙寡,但可能帶有重復(fù)的輸出
SELECT DISTINCT sno FROM SC; --消除重復(fù)的輸出
SELECT DISTINCT cno,grade FROM SC; --同時消除兩列重復(fù)的輸出。
where引出查詢條件:
SELECT sname琳水,sage
from student
where sage>19; --查詢sage大于19的數(shù)據(jù)
SELECT sname肆糕,sage
from student
where NOT sage<=19; --效果同上
SELECT DISTINCT sno
FROM sc
where grade<60; --查詢grade小于60的數(shù)據(jù)般堆,去重顯示
SELECT sname,sdept,sage
from student
where sage between 20 and 22; --查詢sage在20到22的數(shù)據(jù)
SELECT sname诚啃,sdept,sage
from student
where sage NOT between 20 and 22; --查詢sage不在20到22的數(shù)據(jù)
SELECT sname,ssex
from student
where sdept in('aaa','bbb'); --查詢sdept列包含aaa或bbb的數(shù)據(jù)淮摔。
SELECT sname,ssex
from student
where sdept NOT IN('aaa','bbb'); --查詢sdept列,既沒有aaa始赎,也沒有bbb的數(shù)據(jù)
SELECT *
from student
where sno='001';
SELECT *
from student
where sno like '001'; --查詢效果同上
SELECT sname,sno,sex
from student
where sname like '張%'; --查詢姓張的數(shù)據(jù)
SELECT sname,sno,sex
from student
where sname not like '張%'; --查詢不姓張的數(shù)據(jù)
做個測試:
--插入數(shù)據(jù):
insert into cource values('4','DB_design',1,4);
insert into cource values('5','DBAdesign',1,3);
select cno,ccredit
from cource
where cname like 'DB_design';
select cno,ccredit
from cource
where cname like 'DB\_design' ESCAPE '\'; --對比一下查詢的數(shù)據(jù)結(jié)果和橙。
is null 不能用 = null替代。
select sno,cno
from sc
where grade is null; -- 查詢grade為空值的數(shù)據(jù)
select sno,cno
from sc
where grade is not null; -- 查詢grade不為空值的數(shù)據(jù)
條件的優(yōu)先級:and優(yōu)先級高于or造垛,可以用括號改變優(yōu)先級魔招。
select sname
from student
where sdept ='aaa'
and sage<20; --查詢sdept是aaa,sage小于20的數(shù)據(jù)五辽。條件要同時滿足办斑。
SELECT sname,ssex
from student
where sdept='aaa' or sdept='bbb';
---等同于SELECT sname,ssex
from student
where sdept in('aaa','bbb');
SELECT sname,sdept,sage
from student
where sage>=20
and sage<=22;
--等同于SELECT sname杆逗,sdept,sage
from student
where sage between 20 and 22;
select sno,grade
from sc
where cno='1'
order by grade desc; --按grade降續(xù)排列輸出
select sno,grade
from sc
where cno='1'
order by 2 desc; --按第二列降續(xù)排列輸出
select sno,grade as 成績
from sc
where cno='1'
order by 成績 desc; --按別名成績降續(xù)排列輸出
select *
from student
order by sdept,sage desc; --按sdept升續(xù)排列乡翅,同sdept的,按照sage降續(xù)排列罪郊。
select count(*)
from student; --統(tǒng)計student表所有人數(shù)蠕蚜。
select avg(grade)
from sc
where cno='1'; --查詢平均值
select max(grade)
from sc
where cno='1'; --查詢最大值
select cno,count(sno)
from sc
group by cno; --輸出對應(yīng)cno值的統(tǒng)計量
select sno,count(*)
from sc
group by sno; --查詢每個sno對應(yīng)的cno數(shù)量
選組條件:
select sno
from sc
group by sno
having count(*)>2; --查詢cno統(tǒng)計超過2個的sno,先進行分組統(tǒng)計悔橄,然后根據(jù)分組的結(jié)果進行篩選
分組查詢效果對比:
未分組將作用于整個查詢結(jié)果:
select max(grade)
from sc; --輸出總的最大值
分組后,函數(shù)將作用于每個組
select sno,max(grade)
from sc
group by sno; --輸出的是每個分組的最大值橄维,而不是總的
例如:
select sno,avg(grade) --如果這里添加了cno一起查詢尺铣,就是錯誤的
from sc
group by sno;
例如:
select sno,avg(grade)
from sc
group by sno
having avg(grade)>70; --如果把avg(grade)換為grade就是錯誤的。
例如:
selecr sno,avg(grade)
from sc
where grade<70 --這個位置不能用avg
group by sno;
select count(*)
from student
group by sdept;
select sum(grade)
from sc
group by sno;
select sno,count(*)
from sc
where grade >=90
group by sno
having count(*)>2; --查詢sno和cno的數(shù)量争舞,要求有兩個以上grade大于90的數(shù)量凛忿。順序是:查詢出grade>=90的記錄,按照sno分組竞川,按count(*)>2分組店溢,輸出sno,count(*)
集合查詢:
select cno
from sc
where sno='001'
UNION
select cno
from sc
where sno='002'; --講兩個查詢合并輸出委乌,輸出已經(jīng)去重床牧,滿足其中一個的即可,并集
select cno
from sc
where sno='001'
INTERSECT
select cno
from sc
where sno='002'; --必須同時滿足兩個條件的查詢遭贸。交集
select sno
from sc
MINUS
select sno
from sc
where cno='002'; --差集戈咳,查詢結(jié)果中,去掉cno=‘002’的數(shù)據(jù)
select abs(999) from dual; --返回999的絕對值
select mod(x,y) from dual; --返回x除以y的余數(shù),若y=0著蛙,則返回x
alter session set NLS_DATE_FORMAT='YYYY-MM-DD';--設(shè)置當(dāng)前會話的日期格式為年月日删铃。
select sysdate from dual;
select to_char(sysdate,'YYYY') from dual;