学生选课表

use 学生

--1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student
--2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
--3、 查询Student表的所有记录。
select * from student
--4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80
--5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in(85,86,88)

--6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class=‘95031‘ or ssex=‘女‘
--此处where条件后面使用到or,or是一个逻辑运算符,
--是或者的意思,满足任意一个条件即返回true

--7、 以Class降序查询Student表的所有记录。
select * from student order by class desc
--此处使用到排序语法order by 列名 asc或者desc,意思是对某一列升序或者降序排列

--8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc
--此处是对两列进行排序,当对两列进行排序的时候,
--会优先对order by后的第一列进行排序,
--然后在不影响第一列排序的情况下,再对第二列进行排序

--=============================================================
--9、 查询“95031”班的学生人数。
select count(*) from student where class=‘95031‘
--此处使用到聚合函数count(*),意思是按where选择条件查询后计算数据个数

--10、 查询Score表中的最高分的学生学号和课程号。
select top 1 sno,cno from score order by degree desc
--此处结合排序使用,按分数排序后,查第一个数据,
--使用到top关键字,top 数字,代表取前几条

--11、 查询每门课的平均成绩
select cno,avg(degree) from score group by cno
--此处使用到聚合函数avg(),以及group by
--avg(列) 意思是求一列的平均值
--group by 是对某一列进行分组,列值相同的分到一组中
--当group by 结合其他聚合函数使用时候,会先分组,再分别操作每组数据

--12、 查询‘3-105’号课程的平均分。
select cno,avg(degree) from score group by cno having cno=‘3-105‘
select * from score

--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score group by cno having cno like ‘3%‘ and count(sno)>=5
--此题用到了having选择条件,having是配合group by使用的
--意思是在分组前提下,选择符合条件的某些数据然后进行操作
--select与from之间还是只能用聚合函数或者group by的那一列

--13、查询分数大于70,小于90的Sno列。
select sno from score where degree>70 and degree<90
--使用and连接两个选择条件,因为此处说的是大于小于,所以不能使用between and

--==============================================================

--14、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree from student a,score b where a.sno=b.sno
select sname,cno,DEGREE from student join Score on Student.Sno=Score.Sno
--此题首先将两个表进行表连接,通过where条件说明关系,然后再查询数据

--15、查询所有学生的Sno、Cname和Degree列。
select sno,cname,degree from score a,course b where a.cno=b.cno
--此题同14题,进行表连接然后查询对应列

select b.sno,cname,degree from student a,score b,course c
where a.sno=b.sno and c.cno=b.cno

--16、查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from student a
inner join score b on a.sno=b.sno
inner join course c on c.cno=b.cno
--此题是三表联合查询,使用join进行连接,join表on关系条件

--17、查询“95033”班学生的平均分。
select avg(degree) from score where sno in (
select sno from student where class=‘95033‘)
--此处使用子查询,in括号内本来应该放参数,
--使用查询语句查询出一列来当做参数使用,注意必须是一列

--18、假设使用如下命令建立了一个grade表:
create table grade(low int,upp int,rank char(1))
insert into grade values(90,100,‘A‘)
insert into grade values(80,89,‘B‘)
insert into grade values(70,79,‘C‘)
insert into grade values(60,69,‘D‘)
insert into grade values(0,59,‘E‘)
--现查询所有同学的Sno、Cno和rank列(即分数表的分数对应级别显示)。
select sno,cno,degree,rank from score,grade
where degree between low and upp
select *from grade
--此题首先创建表,然后插入数据再进行查询,进行表连接之后通过degree与高低范围建立关系

--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select *from student where Sno in(
select sno from Score where Cno=‘3-105‘ and Degree>(
select Degree from Score where Cno=‘3-105‘ and Sno=‘109‘
)
)
select *from student,score where Cno=‘3-105‘ and Degree>
(select Degree from Score where Cno=‘3-105‘ and Sno=‘109‘)
--最里面一个子查询查出109号学生的3-105这门课的分数,然后作为参数比较
--中间一个子查询是查出比刚才查出的分数高的同学的学号
--然后最后根据这些学号查出所有信息

--20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select *from Score as a where Sno in(
select sno from Score as b group by sno having COUNT(sno)>1 )
and Degree <(select max(Degree) from Score as c where c.Cno=a.Cno)
--此题使用子查询查询出按学号分组后的个数大于1的学号信息,
--然后根据学号查分数信息并要满足外面表的分数小于分组后每组的最高分

--21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select a.sname,a.sno,b.cno,b.degree from student a,score b where a.sno=b.sno
and b.degree>all(select degree from score where sno=‘109‘ and cno=‘3-105‘)
--此处使用all关键字,all结合关系运算符> < =来使用,
--此处表示degree列里的数据满足大于后面查询出来的degree列中的所有数据才符合条件

--22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student
where year(sbirthday)=
year((select sbirthday from student where sno=‘108‘)) --and sno<>‘108‘
--此题使用取年份的时间日期函数,可以获取时间中的年份返回一个整数值

--23、查询“王萍“教师任课的学生成绩。
select a.tname,b.degree,c.cno from teacher a,score b,course c
where a.tno=c.tno and b.cno=c.cno and tname=‘王萍‘

select *from Score where Cno in (select Cno from Course where Tno in(select Tno from Teacher where Tname=‘王萍‘))
--连接查询,加选择条件和关系

--24、查询选修某课程的同学人数多于5人的教师姓名。
select tname from teacher where tno in (
select tno from course where cno in(
select cno from score group by cno having count(sno)>5 )
)
--此题使用子查询,逻辑是先查人数多于5的课程号
--再查这些课程号对应的教师号
--再根据教师号查教师姓名,然后逆向组合起来

--25、查询95033班和95031班全体学生的记录。
select * from student where class=‘95033‘ or class=‘95031‘
--逻辑或条件关联查询,满足任意一个条件即查询出数据

--26、查询存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>85
--使用distinct 关键字去重并加where选择条件筛选

--27、查询出“计算机系“教师所教课程的成绩表。
select cno ,degree from score where cno in (
select cno from course where tno in (
select tno from teacher where depart=‘计算机系‘))
--子查询,根据计算系只能查询出教师编号
--查出教师号后可以查出对应的课程号,然后根据课程号查分数表

--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select Tname,Prof from Teacher where Prof<>
(select Prof from Teacher group by Prof having count(Prof)>=2)
--第二种解法
select *from Teacher where Prof not in(
select prof from Teacher where Depart=‘计算机系‘ and Prof in (
select prof from Teacher where Depart=‘电子工程系‘))

select prof,tname from Teacher where Prof not in(
select prof from Teacher where Depart=‘计算机系‘ and
Prof in (select Prof from Teacher where Depart=‘电子工程系‘))
and Depart in(‘计算机系‘,‘电子工程系‘)
--29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select cno,sno,degree from score where cno=‘3-105‘ and degree >any (
select degree from score where cno=‘3-245‘)
order by degree desc
--第一种解法使用any,any是满足大于任何一个就可以,也可以用大于最小的那个值来操作替代any
select cno,sno,degree from score where cno=‘3-105‘ and degree > (
select min(degree) from score where cno=‘3-245‘)
order by degree desc

--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select cno,sno,degree from score where cno=‘3-105‘ and
degree >all (select degree from score where cno=‘3-245‘) order by degree desc
--此题使用子查询,然后使用all来操作使3-105的大于所有3-245的才筛选出来

--31、查询所有教师和同学的name、sex和birthday.
select sname 姓名,ssex 性别,sbirthday 生日 from student
union
select tname,tsex,tbirthday from teacher
--这个是将两个表的数据上下拼接起来,使用union关键词来纵向拼接,
--然后上面的列的数据类型要和下面的列的数据类型对应

--32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname 姓名,ssex 性别,sbirthday 生日 from student where ssex=‘女‘
union
select tname,tsex,tbirthday from teacher where tsex=‘女‘
--此题同上,只不过每个查询后加了一个where进行筛选选择

--33、查询成绩比该课程平均成绩低的同学的成绩表。

select sno,cno,degree from score a where degree<(
select avg(degree) from score b group by cno having b.cno=a.cno)
--此题是运用子查询,并在子查询中进行分组,并将分组后的数据与外表的列值建立筛选关系

--34、 查询所有任课教师的Tname和Depart.
select tname,depart from teacher where tno in (select tno from course)

--子查询,去教师表里查教师标号在课程表里有的那些,将课程表里的教师编号作为参数使用

--35 查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher where tno not in (select tno from course)
--同上题,只是去相反范围,在in前面加not,not的意思是非的意思

--36、查询至少有2名男生的班号。
select class from student where ssex=‘男‘ group by class having count(sno)>=2
--分组加having筛选

--37、查询Student表中不姓“王”的同学记录。
select * from student where sname not like ‘王%‘
--此处使用到like模糊查询,like的意思是好像的意思,在字符串中加%,%代表任意一串字符

--38、查询Student表中每个学生的姓名和年龄。
select sname,year(getdate())-year(sbirthday) 年龄 from student
--此处使用取当前日期函数getdate(),另外使用取年份函数year(),并运用减法运算

--39、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday) from student
--此题使用聚合函数取最大最小值

--40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc,sbirthday asc
--使用排序order by ,然后先按class排,在不影响class排序的情况下排sbirthday

--41、查询“男”教师及其所上的课程。
select tname,cname from teacher a,course b where a.tno=b.tno and tsex=‘男‘
--表连接,将两个表通过关系拼接,然后加条件筛选

--42、查询最高分同学的Sno、Cno和Degree列。
select top 1 cno,sno,degree from score order by degree desc
--使用order by 将分数排序后,取第一个使用top 1来获取第一条数据

--43、查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex= (select ssex from student where sname=‘李军‘) and sname <>‘李军‘
--子查询,查出李军的性别作为where选择条件的参数,然后and是排除李军,使用到不等号<>

--44、查询和“李军”同性别并同班的同学Sname.
select sname from student where ssex= (select ssex from student where sname=‘李军‘)
and sname <>‘李军‘ and class in(select class from student where sname=‘李军‘)
--分别使用子查询查出李军的班级和性别作为where选择条件的参数,并用and连接选择条件

--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select * from score where sno in (select sno from student where ssex=‘男‘)
and cno in(select cno from course where cname=‘计算机导论‘)
--子查询,子查询的一个根本道理就是,查出一列作为参数使用

--======================================================================
select * from student
select * from score
select * from course
select * from teacher

时间: 2024-08-08 01:26:35

学生选课表的相关文章

学生选课表建表

use 学生gocreate table Teacher(Tno char(3)primary key,--教工编号Tname char(4)not null,--教工姓名Tsex char(2)not null,--教工性别Tbirthday datetime,--教工出生年月Prof char(6) ,--职称Depart varchar(10)not null,--教职工所在部门)goinsert into Teacher values('804','李诚','男','1958-12-02

数据库题:学生表,选课表,课程表

设教学数据库中有三个基本表: 学生表 S(S#,SNAME,AGE,SEX),其属性表示学生的学号.姓名.年龄和性别:选课表 SC(S#,C#,GRADE),其属性表示学生的学号.所学课程的课程号和成绩:课程表 C(C#,CNAME,TEACHER),其属性表示课程号.课程名称和任课教师姓名. 下面的题目都是针对上述三个基本表操作的. (1)写出检索全是女同学选修的课程的课程号的SQL语句. select C# from S,SC where S.S#=SC.S# and S.SEX='女' (

MySQL常用SQL语句(Python实现学生、课程、选课表增删改查)

以基本的学生选课为例,建立选课数据库,学生.班级.选课信息三张表,并分别对表进行插删改操作: import MySQLdb try: conn = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'root', db = 'xuanke', port = 3306) cur = conn.cursor() cur.execute("CREATE DATABASE xuanke") cur.execute("

学生表和选课表问题解答

别人说这个题目涉及了百分之八十的oracle知识,来挑战下吗? 建表语句如下: 选课表: CREATE TABLE PM_CI ( CI_ID VARCHAR2(20) NOT NULL ENABLE, STU_IDS VARCHAR2(100) ) : 学生表: CREATE TABLE PM_STU ( STU_ID VARCHAR2(20) NOT NULL ENABLE, STU_NAME VARCHAR2(100) ) : 数据如下: 选课表: ci_id  stu_ids 1 1,2

SQL Server 基础之《学生表-教师表-课程表-选课表》(二)

表结构 --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称CourseName.教师编号TeaId) --成绩表tblScore(学生编号StuId.课程编号CourseId.成绩Score) --教师表tblTeacher(教师编号TeaId.姓名TeaName) CREATE TABLE tblStudent ( StuId INT, StuName nvarchar(32

学生表 课程表 成绩表 教师表 50个常用sql语句

原文:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/23/1572276.html Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表   create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)

学生表 课程表 成绩表 教师表 50个常用sql语句[转]

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表   create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables wh

在线考试系统(Online Exam System)--ASP.NET

用户设计 -|学生 -|老师 -|管理员 学生结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|选课中心 -|显示所有老师所开课的信息可进行选课 -|显示自己已选课程 -|在线考试 -|对已选老师开设的课程选择进行考试 -|成绩查询 -|查看自己考试成绩   老师结构设计 -|个人信息管理 -|修改个人信息 -|修改登录密码 -|课程管理 -|显示学校开设的课程(老师可选择添加课程) -|显示老师开设的课程 -|考试管理 -|显示老师自己开设的课程 -|对课程添加试题(选择.填

mysql数据库的优化

表的设计符合三范式 每一列属性都是不可分割的属性值,确保每一列的原子性 学生编号 姓名 性别 联系方式20080901 张三 男 email:[email protected],phone:8888666620080902 李四 女 email:[email protected],phone:66668888 以上的表就不符合,第一范式:联系方式字段可以再分,所以变更为正确的是: 学生编号 姓名 性别 电子邮件 电话20080901 张三 男 [email protected] 88886666