关系数据库标准语言SQL之数据查询

关系数据库标准语言SQL之数据查询

个人考研,持续分享学习总结!

3.4数据查询

select [ all | distinct ] <目标列表达式>…
from <表名和视图名>[as]<别名>
[where<条件表达式>]
[group by<列名1>[having<条件表达式>]]
//属性值相等的元组为一个组,如果含有having表只有满足指定条件的组才能输出
[order by<列名2>[ASC | DESC]];
//按序查询

3.4.1单表查询

(1)选择表中的若干列
例如
select Sno,Sname
from Student;
例如:查询全体学生的姓名、出生年份、所在的院系(要求用小写字母表示系名)
select Sname,2014-Sage,LOWER(Sdept)
//LOWER表小写,UPPER表大写
from Student;
例如
select Sname NAME,‘Year of Birth:’as BIRTH,
2014-Sage BIRTHDAY,LOWER(Sdept)DEPARTMENT
//用户可指定别名改变查询结果的列标题(as可省略)
from Student;

(2)选择表中的若干元组
例如
select distinct Sno
//查询结果包含许多重复的行,如想去掉表中的重复行,必须指定distinct;如果没有指定distinct则默认为all即保留表中重复的行
from SC;

补充:where子句常用的查询条件
字符匹配:like,not like
空值:is null,is not null
多重条件:and,or,not
确定范围:between and,not between and
//between后是范围的下限,and后是范围的上限
确定集合:in,not in
比较运算符:=,<,>,<=,>=,!=,<>,!<,!>,;not+以上比较运算符

(比较大小)
例如
select distinct Sno
from SC
where Grade<60;

(确定范围)
例如
select Sname,Sdept,Sage
from Student
where Sage between 30 and 40;

(确定集合)
例如 查找既不是计算机系,数学系,也不是信息系的学生姓名和性别
select Sname,Ssex
from Student
where Sdept not in(‘CS’,‘MA’,‘Is’);
//in可以用于查找属性值属于指定集合的元组,not in相反

(字符匹配)
[not] like ‘<匹配串>’[escape‘<换码字符>’]
//%表示任意长度,表示任意单个字符
例如
select *
from Student
where Sname like‘刘%’;
//姓“刘”的学生
where Sname like‘欧阳
’;
//姓“欧阳”且全名为三个汉字的学生
where Sname like‘_阳%’;
//名字中第二个字为“阳”的学生
where Sname not like ‘刘%’;
//不姓“刘”的学生

例如:查询DB_Design课程的课程号和学分
select Cno,Ccredit
from Course
where Cname like ‘DB_Design’ escape’’;
//escape’'表示“\”的换码字符,这样后面的“”就不在有任意单个字符的含义
例如
select *
from SC
where Sname like‘DB_%i
’ escape ‘\’;
// i后面的_仍为通配符,前面的不为通配符

(涉及空值的查询)
例如
select Sno,Cno
from SC
where Grade is null;
//is不能被省略或用=替换

(多重条件查询)
select *
from Student
where Sdept=‘CS’ and Sage<=20;

(3)order by子句
//记得使用升序ASC和降序DESC,不使用的话默认是升序
例如
select Sno,Grade
from SC
where 条件
order by Grade DESC,Cno ASC;

(4)聚集函数
统计元组个数:count(
统计一列值的个数:count([distinct | all]<列名>)
计算一列值的总和:sum([distinct | all]<列名>)
计算一列值的平均值:avg([distinct | all]<列名>)
求一列值的最大值:max([distinct | all]<列名>)
求一列值的最小值:([distinct | all]<列名>)
//注意:指定distinct则表示计算时要取消指定列的重复值
例如
select count(

from Student;

例如
select avg(Grade),min(Grade)
from SC
where Cno=‘1’;
例如:查询学生201215012选修课程的总学分数
select sum(Ccredit)
from SC,Course
where Sno=‘201215012’and SC.Cno=Course.Cno;

(5)group by子句(将查询结果按某一列或多列的值分组,值相等的为一组)
(写语句时带上having短语)
例如:查询选修了三门以上课程的学生学号
select Sno
from SC
group by Sno having count(Sdept)>3;
例如:查询平均成绩大于等于90分的学生学号和平均成绩
select Sno,avg(Grade)
from SC
group by Sno having avg(Grade)>=90;

3.4.2连接查询

(1)等值与非等值连接查询
[<表名1>.]<列名1><比较运算符>[<表名2>.]<列名2>
或者
[<表名1>.]<列名1>between[<表名2>.]<列名2>and[<表名3>.]<列名3>
//当运算符为=时,为等值连接,否则为非等值连接
例如
Select Student.,SC.,Ssex,Cno
From Student,SC
Where Student.Sno=SC.Sno;
例如
Select Student.Sno,Sname
From Student,SC
Where Student.Sno=SC.Sno
And SC.Cno=‘2’
And SC.Grade>90;

(2)自身连接
//要为Course表取两个别名,一个是first,一个是second
例如:查询一门课的先修课的先修课
Select first.Cno,second.Cpno
From Course first,Course second
Where first.Cpno=second.Cno;

(3)外连接
//如果把悬浮元组也保存在结果关系中,在其他属性值上要填空值NULL。
例如
Select *
From S left outer join SC on(S.Sno=SC.Sno);

(4)多表连接
例如
Select Student.Sno, Sname, Cname, Grade
From Student,SC,Course
Where Student.Sno=SC.Sno
And SC.Cno=Coures.Cno;

3.4.3嵌套查询

//查询块的含义:select-from-where
//将一个查询块嵌套在另一个查询块的where子句或having短语的条件查询为嵌套查询
例如
Select Sname
From Student
Where Sno
IN(
Select Sno
From Sc
Where Cno=‘2’
);
//in很重要,别忘了

(1)带有in的谓语的子查询
例如
Select Sno,Sname
From Student
Where Sno
In(
Select Sno
From SC
Where Cno
In(
Select Cno
From Course
Where Cname=‘信息系统’

);
//如果子查询的查询条件不依赖于父查询,这类子查询称为相关子查询

(2)带有比较运算符的子查询
例如
Select Sno,Sname,Sdept
From Student
Where Sdept
=(
//当用户能确切知道内层查询返回的是单个值时,可用比较运算符。此处用“=”代替in。
Select Sdept
From Student
Where Sname=‘刘晨’
);
//如果子查询的查询条件依赖于父查询,这类子查询称为相关子查询

补充:limit关键字
例如:查询员工信息表中第六条至第十条数据
Select * from S limit 5,5;
//此时limit arg1,arg2中,arg1表示从哪一行开始,初始行为0。
arg2表示有多少条数据(当arg1=0时,可不写arg2)

(3)带有any(some)或all谓词的子查询
大于 子查询结果中的某个值:>any 等价于>max
大于 子查询结果中的所有值:>all 等价于>min
大于等于 子查询结果中的某个值:>=any等价于>=max
大于等于 子查询结果中的所有值:>=all 等价于>=min
小于 子查询结果中的某个值:<any 等价于<max
小于 子查询结果中的所有值:<all 等价于<min
小于等于 子查询结果中的某个值:<=any 等价于<=max
小于等于 子查询结果中的所有值:<=all 等价于<=min
等于 子查询结果中的某个值:=any 等价于in
等于 子查询结果中的所有值:=all
不等于 子查询结果中的某个值:!=any 或 <>any
不等于 子查询结果中的任何值:!=all 或 <>all 等价于not in
例如:查询非计算机系中比计算机系任意一个学生年龄小的学生姓名和年龄
Select Sname,Sage
From Student
Where Sage < any(
Select Sage
From Student
Where Sdept=‘CS’

And Sdept != ‘CS’;

(4)带有(not)exists 谓词的子查询
//带有exists谓词的子查询不返回任何数据,只产生逻辑真值“true”或逻辑假值“false”
例如
Select Sname
From Student
Where exists(
Select *
From SC
Where Cno=‘1’
);

3.4.4集合查询

//包括并操作union(or),交操作intersect(and)和差操作except
例如
Select 狗From 狗 Where 狗=‘0’
Union / Intersect
Select 狗 From 狗 Where 狗=‘0’;
//此处引用张宇老师的名词,还望理解

3.4.5基于派生表的查询

例如
Select 狗
From 狗,
(select狗 from狗group by狗)as 狗
where 狗
//as关键字
//此处引用张宇老师的名词,还望理解

发布了11 篇原创文章 · 获赞 3 · 访问量 325

猜你喜欢

转载自blog.csdn.net/weixin_44367041/article/details/103690671