实验七 带函数查询和综合查询(2)

1 检索至少选修课程"数据结构"和"C语言"的学生学号

方法一:

select Stu_id

from StudentGrade,Course

where Course.Course_id=StudentGrade.Course_id and

Course_name='数据结构' and

Stu_id in (select Stu_id

from StudentGrade,Course

where Course.Course_id=StudentGrade.Course_id and

Course_name='C语言')

方法二:

select Stu_id

from StudentGrade,Course

where Course.Course_id=StudentGrade.Course_id and

Course_name in('数据结构','C语言')

group by stu_id

having count(*)>=2/以学号分组,该学号选修满足条件的课程数>=2/

2 列出所有班名、班主任、班长、系名。

(请使用连接查询;进一步考虑使用外连接,因为很多班级可能是没有班长的,考虑需要显示所有班级的信息)

select class_name,director,monitor,depar_name

from class left join deparment on (deparment.depar_id=class.depar_id)

3.没有选修以"01"开头的课程的学生学号,姓名,选课的课程号。(用子查询完成,提示not in或not exists。需考虑没选课的学生,仔细对比实验六第5题)

方法一

select student.stu_id,stu_name,course_id

from student,studentgrade

where studentgrade.stu_id not in(select stu_id

from studentgrade

where course_id like '01%')

and student.stu_id=studentgrade.stu_id

union

/没选课的学生信息 /

select student.stu_id,stu_name,null

from student where stu_id not in(select stu_id from studentgrade)

/没有选修以"01"开头的课程的学生信息。 /

select stu_id,stu_name

from student

where stu_id not in(select stu_id

from studentgrade

where course_id like '01%')

方法二

select student.stu_id,stu_name,course_id

from student,studentgrade

where not exists(select *

from studentgrade

where course_id like '01%' and stu_id=student.stu_id)

and student.stu_id=studentgrade.stu_id

union

/没选课的学生信息 /

select student.stu_id,stu_name,null

from student where not exists(select * from studentgrade where stu_id=student.stu_id)

方法三 错误,不以01又以01开头的出现在结果中

select student.stu_id,stu_name,course_id

from student,studentgrade

where exists(select *

from studentgrade

where course_id not like '01%' and stu_id=student.stu_id)

and student.stu_id=studentgrade.stu_id

4.统计各门课程的选修人数,并按人数降序排列,找出排名前三位的课程。(提示:可以使用TOP 3)

select top 3 course_id,count()
from studentgrade
group by course_id
order by count(
) desc

/distinct top 3 course_id,count( ) 为什么不能解决并列情况?/
select distinct top 3 course_id,count(
)

from studentgrade

group by course_id

order by count(*) desc

5.统计各门课程的选修人数,并按人数降序排列,找出排名前三位的课程。

(上述使用TOP 3,却不能处理人数并列的情况。试考虑一种方法能处理人数并列的情况。)

select course_id,count()
from studentgrade
group by course_id
having count(
) in(select distinct top 3 count()
from studentgrade
group by course_id
order by count(
) desc)

order by count(*) desc

6.检索选修了'0103'和'0105'两门课程,并且

'0103'这门课程成绩高于'0105'的学生的学号。

select A.stu_id /select A.stu_id,A.course_id,B.course_id,A.grade,B.grade /

from studentgrade A,studentgrade B

where A.stu_id=B.stu_id and A.course_id='0103' and B.course_id='0105'

and A.grade>B.grade

7.检索选修了课程"数据结构"和"C语言"两门课程并且

"数据结构"分数高于"C语言"的学生学号和姓名。

select A.stu_id /*select A.stu_id,A.course_id,B.course_id,A.grade,B.grade */

from Studentgrade A,Studentgrade B

where A.stu_id=B.stu_id

and A.course_id in(select course_id from course where course_name='数据结构')

and B.course_id in(select course_id from course where course_name='c语言')

and A.grade>B.grade

相关推荐
此生只爱蛋8 分钟前
【Redis】主从复制
数据库·redis
马猴烧酒.27 分钟前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
天天爱吃肉82181 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
大巨头1 小时前
sql2008 数据库分页语句
数据库
m0_715575341 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
老邓计算机毕设2 小时前
SSM智慧社区家政服务系统80q7o(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架
松涛和鸣2 小时前
72、IMX6ULL驱动实战:设备树(DTS/DTB)+ GPIO子系统+Platform总线
linux·服务器·arm开发·数据库·单片机
likangbinlxa3 小时前
【Oracle11g SQL详解】UPDATE 和 DELETE 操作的正确使用
数据库·sql
r i c k3 小时前
数据库系统学习笔记
数据库·笔记·学习
野犬寒鸦3 小时前
从零起步学习JVM || 第一章:类加载器与双亲委派机制模型详解
java·jvm·数据库·后端·学习