Sql 练习题 (四)

文章包含一系列SQL查询语句,涉及查询与特定学生在同一系学习的学生、成绩高于90分的学生、计算机系选修特定课程的学生、选修特定课程的学生详细信息、课程的选课门数和平均成绩、高于平均成绩的成绩、未选修特定课程的学生等。这些查询涉及到学生表、课程表和选课表的联接操作、条件过滤和聚合函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--(1)查询与“刘晨”在同一个系学习的学生。

select sname 学生姓名,Dept 系名 from student 
where dept in 
      (select dept from student  where sname='刘晨') and sname!='刘晨';

--(2)查询考试成绩大于90分的学生的学号和姓名。

select sno 学号,sname 姓名 
from student 
where Sno in 
     (select Sno from SC where Grade >90);

--(3)查询计算机系选了“C002”课程的学生,列出姓名和性别。

select sname 姓名 ,ssex 性别 
from student s join SC c on s.Sno = c.Sno 
where Dept='计算机系' and Cno='C002';

--(4)查询选修了“Java”课程的学生的学号和姓名。

select s.sno 学号 ,sname 姓名 
from student s join SC c on s.Sno=c.Sno join Course co on c.cno=co.Cno 
where co.Cname='Java';

--(5)统计选了Java课程的这些学生的选课门数和平均成绩。

select count(*) 选课门数 ,avg(c.grade) 平均成绩 
from student s join SC c on s.Sno=c.Sno join Course co on c.cno=co.Cno 
group by co.cname 
having Cname='Java';

--(6)查询选了“JAVA”课程的学生学号、姓名和JAVA成绩。

select s.sno 学号 ,sname 姓名 ,c.grade 成绩 
from student s join SC c on s.Sno=c.Sno join Course co on c.cno=co.Cno 
where co.Cname='Java';


--(7)查询选了“C004”号课程且成绩高于此课程的平均成绩的学生的学号和成绩。

select sno,grade from SC 
where Cno='C004' and Grade > 
      (select AVG(grade)from SC where cno='C004');

--(8)查询考试平均成绩高于全体学生的总平均成绩的学生的学号和平均成绩。

select sno,grade from SC where Grade > 
     (select AVG(grade) from SC where Grade is not null;

--(9)查询没选“C001”号课程的学生姓名和所在系。

select sname 学生姓名 , dept 所在系 
from student 
where Sno not in 
    (select Sno from SC where Cno='C001'); 

--(10)查询计算机系没选JAVA课程的学生姓名和性别。

select sname 学生姓名,ssex 性别 from student 
where Dept='计算机系' and Sno not in 
       (select Sno from SC c join Course co on c.Cno=co.Cno where Cname='Java');

--(11) 查询其他学期开设的课程中比第1学期开设课程的学分少的课程名、开课学期和学分。

select cname 课程名 , semester 开课学期 ,credit 学分 from Course 
where semester!=1 and Credit<
                     (select MAX(credit) from Course where Semester=1); 

--(12)查询至少有一次成绩大于等于90的学生的姓名,所修的课程号和成绩。

select s.sname 姓名,s.sno 课程名 ,c.grade 成绩 from student s join SC c on s.Sno=c.Sno 
where exists
   (select * from SC s2 where Grade >=90 and s.Sno=s2.sno);

--(13)查询比第1学期开设的所有课程的学分都小的其他学期开设的课程名、开课学期和学分。

select cname 课程名 ,semester 课程名 ,credit 学分 from Course 
where Semester!=1 and Credit<
                (select MIN(credit)from Course where Semester=1);

--(14)查询每个学期学分最低的课程的课程名、开课学期和学分。

select cname 课程名 ,semester 开课日期 ,credit 学分 from Course c1 
where Credit <= all
              (select Credit from Course c2 where  c1.Cno<>c2.Cno and c1.Semester=c2.semester);

--(15)查询每门课程考试成绩最高的两个学生的学号以及相应的课程号和成绩。不包括没考试的课程。  

select s1.sno 学号,s1.cno 课程号,grade 成绩 from SC s1 where s1.Sno in 
      (select top 2 Sno  from SC s2 where s1.cno=s2.cno and grade is not null order by Grade desc ) 
         order by s1.Cno,s1.Grade desc;


 
--(16)查询每门课程中,考试成绩低于该门课程的平均成绩的学生的学号和成绩。

select cno 课程号,sno 学号 ,grade 成绩 from SC s1 
where Grade <
     (select AVG(grade) from SC s2 where s1.Cno=s2.Cno and grade is not null);

--(17)查询有最高学分超过本学期平均学分1.5倍的学期。

select semester 学期 from Course c1 group by Semester having MAX(Credit)>
    (select AVG(credit)*1.5 from Course c2 where c1.Semester =c2.Semester );

--(18)查询学生姓名、所在系和该学生选的课程门数。

select s.sname 学生姓名 ,s.dept 所在系 ,COUNT(c.cno) 课程门数 
from student s join SC c on s.Sno=c.sno 
group by s.Sname,s.dept ;

--(19)查询课程名、开课学期及选该门课的学生人数、平均成绩。不包括没人选的课程。

select cname 课程名,semester 开课学期 ,COUNT(sno) 学生人数 ,AVG(grade) 平均成绩 
from SC c join Course co on c.Cno=co.Cno 
group by Cname,Semester ;

--(20) 使用exists查询选了“C002”课程的学生姓名。

select distinct sname 学生姓名 from student s join SC c1 on s.Sno=c1.Sno 
where exists 
       (select * from sc c2 where c1.Sno=c2.Sno and c2.cno='C002')

--(21)使用exists查询选了JAVA课程的学生姓名和所在系。

select sname 学生姓名 , dept 所在系 from student s join SC c1 on s.Sno=c1.Sno 
where exists
    (select * from SC c2 join Course co on c2.Cno=co.Cno where co.Cname='Java' and c1.Cno=c2.Cno  );

--(22)使用exists查询没有选修“C001”课程的学生姓名和所在系。

select distinct sname 学生姓名 ,dept 所在系 from student s
where not exists 
       (select * from sc c where s.Sno=c.Sno and c.cno='C001')

--(23) 使用exists查询计算机系没选JAVA的学生姓名和性别。

select sname 学生姓名 , ssex 性别 from student s 
where Dept='计算机系' and not exists 
    (select * from SC c join Course co on c.Cno=co.Cno where s.Sno=c.Sno and Cname ='Java')

--(24) 使用exists查询至少选了全部课程的学生的学号、姓名和所在系。

select sno 学号,sname 姓名 ,dept 所在系 from student s1 
where not exists
    (select * from SC s2 where not exists
    (select * from SC s3 where s1.Sno=s3.Sno and s2.Sno=s3.sno));

--(25) 使用exists查询至少选了“0811102”学生所选的全部课程的学生的学号和所选的课程号。

select sno 学号, Cno 课程号 from SC s1 where not exists
   (select * from SC s2 where not exists
     (select * from SC s3 where s1.Sno=s3.sno and s2.Cno=s3.cno) and sno=0811102)and sno!=0811102;

--(26) 查询同时选修”Java”和“高等数学”,并且“Java”成绩高于“高等数学”的学生学号;

select sno 学生学号 from SC s1 join course c1 on s1.cno=c1.cno 
where Sno in 
      (select sno from SC s2 join course c2 on s2.cno=c2.cno 
               where s1.Sno=s2.Sno and c1.cname='Java' and c2.Cname='高等数学' and s1.grade>s2.grade);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好学的9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值