SQL查询学习

Posted 搬砖的老王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL查询学习相关的知识,希望对你有一定的参考价值。

四张表 ,学生、课程、分数、教师。

student(学号#,姓名,性别,年龄)
course(课程号#,课程名,教师号#)
score(学号#,课程号#,成绩)
teacher(教师号#,教师名)


查询平均成绩大于60分的同学的学号和平均成绩
select stuNo,avg(score) from score  group by  stuNo  having  avg(score)>60 

查询所有同学的学号、姓名、选课数、总成绩
select a.stuNo,a.stuName,count(cNo),sum(score)  from student a,score b 

where a.stuNo=b.stuNo group by a.stuNo,a.stuName

查询姓“赵”的老师的个数
select count(tName),tName from teacher  where tName like ‘赵%‘ group by tName 

查询没学过“某某”老师课的同学的学号、姓名
select stuNo,stuName from student
where stuNo not in
(select a.stuNo from student a,score b where a.stuNo=b.stuNo and cNo in
(select d.cNo from teacher c,course d where c.tNo=d.tNo and c.tName=‘钱市保‘))

查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
select a.stuNo,a.stuName from student a,score b,score c
where a.stuNo=b.stuNo and b.stuNo=c.stuNo and b.cNo=‘c001‘ and c.cNo=‘c002‘

查询学过“某某”老师所教的所有课的同学的学号、姓名
select stuNo,stuName from student
where stuNo in (select stuNo from score a,course b,teacher c
where a.cNo=b.cNo and b.tNo=c.tNo and c.tName=‘钱市保‘
group by stuNo
having count(a.cNo)>=(select count(cNo) from course d,teacher e
where d.tNo=e.tNo and e.tName=‘钱市保‘))


查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
select stuNo,stuName from student
where stuNo in
(select a.stuNo from score a,score b
where a.cNo=‘c001‘ and b.cNo=‘c002‘ and a.stuNo=b.stuNo and a.score>b.score)

查询所有课程成绩小于60分的同学的学号、姓名
select stuNo,stuName from student
where stuNo in (select stuNo from score
where score<60
group by stuNo
having count(cNo)=(select count(cNo) from course))

查询没有学全所有课的同学的学号、姓名
select b.stuNo,a.stuName,count(b.cNo) from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having count(b.cNo)<(select count(cNo) from course)

查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名
select distinct a.stuNo,stuName from student a,score b
where a.stuNo=b.stuNo and cNo in (select cNo from score
where stuNo=‘001‘)

查询至少学过学号为“001”同学一门课的其他同学学号和姓名
select distinct a.stuNo,stuName from student a,score b
where a.stuNo=b.stuNo and cNo all join (select cNo from score
where stuNo=‘001‘)

把“SC”表中“某某”老师教的课的成绩都更改为此课程的平均成绩
update score set score=savg
from score d,(select avg(score) as savg,a.cNo from score a,course b,teacher c
where a.cNo=b.cNo and b.tNo=c.tNo and tName=‘钱市保‘
group by a.cNo) e
where d.cNo=e.cNo

查询和“001”号的同学学习的课程完全相同的其他同学学号和姓名
select stuNo from score
where cNo in (select cNo from score where stuNo=‘005‘)
group by stuNo
having count(cNo)=(select count(*) from score where stuNo=‘005‘)
 

查询不同老师所教不同课程平均分从高到低显示
select tNo,a.cNo,avg(score) from course a,score b
where a.cNo=b.cNo
group by tNo,a.cNo
order by avg(score) desc

统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select distinct e.cNo,count(a.stuNo) as ‘100-85‘,count(b.stuNo) as ‘85-70‘,count(c.stuNo) as ‘70-60‘,count(d.stuNo) as ‘<60‘ from score a,score b,score c,score d,score e
where a.cNo in (select cNo from course) and a.score between 85 and 100 and b.cNo in (select cNo from course) and b.score between 71 and 84 and c.cNo in (select cNo from course) and c.score between 60 and 70 and d.cNo in (select cNo from course) and d.score<60
group by e.cNo,a.stuNo,b.stuNo,c.stuNo,d.stuNo
having a.stuNo<>b.stuNo and a.stuNo<>c.stuNo and a.stuNo<>d.stuNo and b.stuNo<>c.stuNo and b.stuNo<>d.stuNo and c.stuNo<>d.stuNo
select cNo,count(stuNo) from score
where score between 70 and 100 and cNo=‘c001‘
group by cNo

查询学生平均成绩及其名次
select stuNo,avg(score) from score
group by stuNo
order by avg(score) desc

查询各科成绩前三名的记录:(不考虑成绩并列情况)
select a.stuNo,a.cNo,a.score
from score a
where a.score in (select top 3 score from score b
where a.cNo=b.cNo
order by score)
order by a.cNo

查询每门课程被选修的学生数
select b.cNo ,count(stuNo) from score a right join course b
on a.cNo=b.cNo
group by b.cNo

查询出只选修了一门课程的全部学生的学号和姓名
select b.stuNo,a.stuName from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having count(b.cNo)=1

查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cNo,avg(score) from score
group by cNo
order by avg(score) asc,cNo

查询平均成绩大于70的所有学生的学号、姓名和平均成绩
select b.stuNo,a.stuName,avg(score) from student a,score b
where a.stuNo=b.stuNo
group by b.stuNo,a.stuName
having avg(score)>70

查询课程名称为“java”,且分数低于70的学生姓名和分数
select a.stuName,b.score from student a,score b
where a.stuNo=b.stuNo and score<70 and b.cNo=(select cNo from course where cName=‘java‘)

查询所有学生的选课情况
select a.stuNo,c.cNo from student a,score b,course c
where a.stuNo=b.stuNo and b.cNo=c.cNo
order by a.stuNo
select a.stuNo,cNo from student a left join (select a.stuNo,c.cNo from student a,score b,course c
where a.stuNo=b.stuNo and b.cNo=c.cNo) d
on a.stuNo=d.stuNo
order by a.stuNo

查询不及格的课程,并按课程号从大到小排列
select cNo,score from score
where score<60
order by cNo

查询课程编号为003且课程成绩在60分以上的学生的学号和姓名
select b.stuNo,a.stuName from student a,score b
where b.cNo=‘c003‘ and score>60 and a.stuNo=b.stuNo

查询每门功成绩最好的前两名
select a.stuNo,a.cNo,a.score
from score a
where score in(select top 2 score from score b
where a.cNo=b.cNo
order by score desc)
order by a.cNo

统计每门课程的学生选修人数(超过2人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cNo,count(stuNo) 课程数 from score
group by cNo
having count(stuNo)>2
order by count(stuNo) desc,cNo

检索至少选修两门课程的学生学号
select stuNo from score
group by stuNo
having count(cNo)>=2

查询全部学生都选修的课程的课程号和课程名
select a.cNo,b.cName from score a,course b
where a.cNo=b.cNo
group by a.cNo,b.cName
having count(a.stuNo)=(select count(stuNo) from student)
select a.cNo,b.cName from score a,course b
group by a.cNo,b.cName,b.cNo
having a.cNo=b.cNo and count(a.stuNo)=(select count(stuNo) from student)

查询两门以上不及格课程的同学的学号及其平均成绩
select stuNo,avg(score) from score
where score<60
group by stuNo
having count(cNo)>2

检索“004”课程分数小于60,按分数降序排列的同学学号
select stuNo from score
where score<60 and cNo=‘c004‘
order by score desc



































































































































以上是关于SQL查询学习的主要内容,如果未能解决你的问题,请参考以下文章

sql查询学习和实践点滴积累

知识扩展-SQL查询基础

sql学习笔记2

知了堂学习笔记SQL查询基础语句(单表查询多表查询)

最近学习的sql查询语句连接查询,标记一下

SQL Server学习笔记——单表查询