期末复习page5

数据查询:
前面已经创建了表,插入了数据,现在可以进行数据查询。

SELECT * FROM STUDENT; --查询student表所有数据。

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,但可能带有重复的输出

SELECT DISTINCT sno FROM SC;  --消除重复的输出

SELECT DISTINCT cno,grade FROM SC;  --同时消除两列重复的输出。

where引出查询条件:

SELECT sname,sage
from student
where sage>19;  --查询sage大于19的数据

SELECT sname,sage
from student
where NOT sage<=19;  --效果同上


SELECT DISTINCT sno
FROM sc
where grade<60;  --查询grade小于60的数据,去重显示

SELECT sname,sdept,sage
from student
where sage between 20 and 22;  --查询sage在20到22的数据


SELECT sname,sdept,sage
from student
where sage NOT between 20 and 22;  --查询sage不在20到22的数据

SELECT sname,ssex
from student
where sdept in('aaa','bbb');  --查询sdept列包含aaa或bbb的数据。

SELECT sname,ssex
from student
where sdept NOT IN('aaa','bbb');  --查询sdept列,既没有aaa,也没有bbb的数据
通配符
换码
SELECT * 
from student
where sno='001';

SELECT * 
from student
where sno like '001';  --查询效果同上

SELECT sname,sno,sex
from student
where  sname like '张%';  --查询姓张的数据

SELECT sname,sno,sex
from student
where  sname not like '张%';  --查询不姓张的数据

做个测试:
--插入数据:
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 '\';  --对比一下查询的数据结果。

is null 不能用 = null替代。


select sno,cno
from sc
where grade is null;  -- 查询grade为空值的数据

select sno,cno
from sc
where grade is not null;  -- 查询grade不为空值的数据

条件的优先级:and优先级高于or,可以用括号改变优先级。


select sname
from student
where sdept ='aaa'
and sage<20;  --查询sdept是aaa,sage小于20的数据。条件要同时满足。


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;
order by 子句
select sno,grade
from sc
where cno='1'
order by grade desc;   --按grade降续排列输出

select sno,grade
from sc
where cno='1'
order by 2 desc;  --按第二列降续排列输出

select sno,grade as 成绩
from sc
where cno='1'
order by 成绩 desc;  --按别名成绩降续排列输出

select * 
from student
order by sdept,sage desc;  --按sdept升续排列,同sdept的,按照sage降续排列。

select count(*)
from student;  --统计student表所有人数。

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;  --输出对应cno值的统计量

select sno,count(*)
from sc
group by sno;  --查询每个sno对应的cno数量

选组条件:
select sno 
from sc
group by sno
having count(*)>2;  --查询cno统计超过2个的sno,先进行分组统计,然后根据分组的结果进行筛选

where
having,用在group by后面

分组查询效果对比:

未分组将作用于整个查询结果:
select max(grade)
from sc;  --输出总的最大值

分组后,函数将作用于每个组
select sno,max(grade)
from sc
group by sno;  --输出的是每个分组的最大值,而不是总的

image.png

例如:

select sno,avg(grade)  --如果这里添加了cno一起查询,就是错误的
from sc
group by sno;

image.png

例如:

select sno,avg(grade)
from sc
group by sno
having avg(grade)>70;  --如果把avg(grade)换为grade就是错误的。
image.png

例如:

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的数量,要求有两个以上grade大于90的数量。顺序是:查询出grade>=90的记录,按照sno分组,按count(*)>2分组,输出sno,count(*)

集合查询:

image.png
select cno 
from sc
where sno='001'
UNION
select cno 
from sc
where sno='002';  --讲两个查询合并输出,输出已经去重,满足其中一个的即可,并集

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';  --差集,查询结果中,去掉cno=‘002’的数据
分组
dual表
select abs(999) from dual;  --返回999的绝对值

select mod(x,y) from dual;  --返回x除以y的余数,若y=0,则返回x


alter session set NLS_DATE_FORMAT='YYYY-MM-DD';--设置当前会话的日期格式为年月日。

select sysdate from dual;

select to_char(sysdate,'YYYY') from dual;

转换函数
字符函数
空值函数
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容