SQL——student表数据简单查询
选择表中若⼲元组
选择表中若⼲列
order by ⼦句
group by⼦句
聚集函数
选择表中若⼲列
查询全体学⽣的学号与姓名
select sno,sname
from student
选择表中若⼲⾏
消除取值重复的⾏
DISTINCT
查询选修了课程的学⽣学号
select distinct sno
from sc
查询成绩有不及格的学⽣的学号
select distinct sno
from sc
where grade<60
查询年龄不在20-23岁之间的学⽣姓名、系别和年龄
select sname,sdept,sage
from student
where sage not between20and23
查询信息科学系、数学系、和计算机科学系学⽣的姓名和性别
select sname,ssex
from student
where sdept in('is','ma','cs')
查询既不是信息科学系、数学系、和计算机科学系学⽣的姓名和性别
select sname,ssex
from student
where sdept not in('is','ma','cs')
查询表中姓张、姓李和姓刘的学⽣的情况
select*
from student
where sname like'[张李刘]%'
查询所有不姓刘的学⽣姓名、学号和性别
select sname,sno,ssex
from student
where sname not like'刘%'
查询学号的最后⼀位不是2.3.5的学⽣情况
select*
from student
where sno like'%[^235]'
select*
from student
where sno not like'%[235]'
ORDEY BY⼦句
ASC:升序 降序:DESC
查询选修了3号课程的学⽣的学号及其成绩,查询结果按分数降序排列
select sno,gradedistinct查询
from sc
where cno='3'
order by grade desc
查询全体学⽣情况,查询结果按所在系的系名升序排列,同⼀系中的学⽣按年龄降序排列
select*
from student
order by sdept,sage desc
在使⽤select语句中,有时只希望列出结果集中的前⼏⾏结果,可⽤top谓词来限制输出结果查询年龄最⼤的三个学⽣的姓名,年龄,及所在系
select top3 sname,sage,sdept
from student
order by sage desc
select top2with ties *
from student
order by sage
聚集函数
COUNT(*):统计表中元组个数
COUNT(列名):统计本列列值个数
SUM:计算列值综合
AVG
MAX
MIN
上述函数中除了COUNT(*)外,其他函数在计算过程中均忽略NULL值
统计学⽣总⼈数
select count(*)总⼈数from student
统计选修了课程的学⽣的⼈数
select count(sno)选课⼈数
from sc
计算9512101号学⽣的考试总成绩之和
select sum(grade)总成绩
from sc
where sno ='9512101'
计算‘c01’号课程学⽣的考试平均成绩
select avg(grade)平均成绩
from sc
where cno='c01'
查询选修了‘c01’号课程的学⽣的最⾼分和最低分
select max(grade)最⾼分,min(grade)最低分
from sc
where cno='c01'
聚合函数不能出现在where⼦句中
查询年龄最⼤的学⽣的姓名
select sname
from student
where sage=max(sage)
这种写法是错的
GROUP BY⼦句
可将计算控制在组⼀级,分组的⽬的是细化计算函数的作⽤对象
分组语句位置在where⼦句的后边
统计每门课程的选课⼈数,列出课程号和⼈数
按照每⼀个课程号,将选课信息分类
统计每⼀个课程的学号数
select cno 课程号,count(sno)选课⼈数
from sc
group by cno
查询每名学⽣的选课门数和平均成绩
select sno 学号,count(cno)选课门数,avg(grade)平均成绩
from sc
group by sno
使⽤HAVING
HAVING⽤于对分组进⾏赛选,他有点像where⼦句,但它⽤于组⽽不是单个记录在HAVING⼦句中可以使⽤计算函数,但在WHERE⼦句中则不能
HAVING通常与GROUP BY⼦句⼀起使⽤
查询选修了3门以上课程的学⽣的学号
SELECT SNO FROM SC
GROUP BY Sno
HAVING COUNT(Cno)>=3
查询修课门数⼤于或等于4门的学⽣的平均成绩和选课门数
select sno 学号,avg(grade)平均成绩,count(cno)选课门数
from sc
group by sno
having count(cno)>=4
select sno 学号,sum(grade)总成绩
from sc
group by sno
having sum(grade)>200
查询计算机系和信息科学系的学⽣⼈数
select sdept,count(*)学⽣⼈数
from student
group by sdept
having sdept in('CS','IS')
select后⾯的列名必须包含在聚合函数或者group by⼦句中,having 后⾯的列名必须包含在聚合函数 group by⼦句中
select sdept,count(*)学⽣⼈数
from student
where sdept in('cs','is')
group by sdept
多表连接查询
⾸先选取表1中的第⼀个元组,然后从头开始扫描表2,注意查满⾜连接条件的元组。
到后就将表2中第⼀个元组与该院组拼接起来,形成结果表中的⼀个元组
表2全部查询完毕后,再取表1中的第2个元组,然后再从头开始扫描表2.并逐⼀连接满⾜条件的元组将其加⼊结果
重复这个过程,直到表1中的全部元组都处理完毕为⽌
查询每个学⽣基本信息及其修课的情况
select*
from student inner join sc
on student.sno=sc.sno
select*
from student,sc
where student.sno=sc.sno
两个表中的连接结果包含了两个表的全部列,sno列重复了两次,这是不必须要的因此,在写查询语句中应当将这些重复的列去掉
select student.sno,sname,ssex,sage,sdept,cno,grade,xklb
from student,sc
where student.sno=sc.sno
查询信息系学⽣的修课情况,要求列出学⽣的名字、所修课的课程号和成绩
select sname,cno,grade
from student,sc
where student.sno=sc.sno
where sdept='信息系'
查询‘信息系’修了VB课程的学⽣,列出学⽣姓名,课程名和成绩
select sname 姓名,cname 课程名,grade 成绩
from student ,sc,course
where student.sno=sc.sno and courseo=sco
where sdept='信息系'and  cname='VB'
外连接
只限制⼀张表中的数据必须满⾜连接条件,⽽另⼀张表数据可以不满⾜条件
可以输出不满⾜连接条件的元组的信息
查询学⽣的修课情况,包括修了课程的学⽣和没有修课的学⽣
select student.sno,sname,cno,grade
from student left outer join sc on student.sno=sc.sno
也可以⽤右外连接来实现
select student.sno,sname,cno,grade
from sc right outer join student on student.sno=sc.sno
⼦查询
⼀个select-from-where语句称为⼀个查询块
将⼀个查询快嵌套在另⼀个select,insert、update、delete语句中的拆线呢被称为嵌套查询
select sname
from student
where sno in(select sno from sc where cno='2')
带有IN谓词的⼦查询
该表达式的值与集合中的某个值相等, 则此测试为true,如果该表达式与集合中所有的值均不相等,则返回false 确定与刘晨在同⼀个系学习的学⽣

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。