这篇文章呢,主要是对数据库中教学管理数据库进行简单查询的一些小练习!!!
首先呢,我们需要在数据库教学管理中建立几个表
接下来呢,就是简单的练习啦......
实验内容
实验内容
- 指定列或全部列查询
- 查询Student学生表中全体学生的详细信息。
select * from student
- 查询所有学生的姓名及其出生年份。
select sname, birthday from student
- 查询所有教师的姓名和性别。
select tname ,sex from teacher
- 查询课程的课程编号、课程名及学分。
select cno,cname,credit from course
- 查询有学生选修的课程号。
select distinct cno from sc
- 按条件查询
- 查询学分为4的课程名称。
select cno,credit from course where credit = (4)
- 查询学号为S3的学生姓名、系名及年龄。
select sname, dept, year(getdate())-year(birthday) as age from Student where SNO='s3'
- 查询考试成绩有不及格的学生学号。
select distinct sno score from SC where SCORE < 60
- 查询有选修课程没考试的学生学号和课程号。
select sno,cno from sc where score is NULL
- 查询考试成绩有75-85分之间的学生学号和成绩。
select sno,score from sc where score between 75 and 85
- 查询年龄在20-22岁之间的学生的姓名、系别和年龄。
select SNAME,year(getdate())-year(birthday) as 年龄,dept
from student
where year(getdate())-year(birthday) between 20 and 22
- 查询所有姓李或姓王的学生的姓名、学号和性别。
select sname,sno,sex from student where sname like'[李王]%'
- 查询名字中第二个字为“晓”字的男生姓名和系别。
select sname,dept from student where sname like '_[晓]%'
- 查询薪水在3000以上的信息系教师姓名。(薪水=工资+岗位津贴)
select tname from teacher
where sal+comm > 3000 and dept = '计算机'
- 查询计算机和通信的教师姓名及年龄。
select tname,age from teacher
where dept = '计算机'and dept = '通信'
- 对查询结果排序
- 查询学生信息,按照出生日期降序排列。
select * from student order by birthday desc
- 查询计算机、电信学生的姓名、系名,结果按系名升序、姓名降序排序。
select sname,dept from student
where (dept = '计算机' or dept = '通信')
order by dept asc, sname desc
- 查询所选修了C2课程且有成绩的学生的学号、课程号和成绩,并按成绩降序排序。
select sno,cno,score from sc where cno = 'c2'
order by score desc
- 查询学号为S2的学生参加考试的成绩,结果按照成绩降序排列。
select sno ,score from sc where sno = 's2'
order by score desc
- 使用聚集函数的查询
- 查询计算机学生的总人数。
select count(*) from student where dept = '计算机'
- 查询C3课程考试的成绩最高分及最低分。
select max(score) 最高分,min(score) 最低分
from sc where cno = 'c3'
- 查询学号为S1的学生所有课程的平均成绩。
select avg(score) from sc where sno = 's1'
- 查询参加了C1课程考试的学生人数。
select count(cno) from sc where cno = 'c1'
- 查询所有姓王的计算机教师人数。
select count(dept) from teacher
where tname like'王%'and dept = '计算机'
- 分组统计查询
- 查询学生中男女生各有多少人。
select sex,count(*)
from student
group by sex
- 查询各个系男女教师的人数。
select sex,count(*)
from teacher
group by sex
查询每个学生的学号及所有考试的平均成绩。
select sno, AVG(score)
from sc
group by sno
- 查询各门课程的课程号及选课人数。
select cno as 课程号,count(sno) as 选课人数
from sc group by cno
- 查询选修了两门以上课程的学生学号和平均成绩。
select sno 学号,avg(score) 平均成绩
from sc group by sno
having count(*)>2
- 查询所有课程考试成绩均及格的学生学号。
select sno from sc
group by sno
having min(score) >= 60
思考
- 查询至少有4个同学选修的课程号。
select cno from sc
group by cno
having count(*)>=4
- 查询选修了两门以上课程的学生学号。
select sno 学号
from sc group by sno
having count(*)>2
- 解释下列ORDER BY子句的作用。
- ORDER BY SNO, SEX
order by 后根据(sno,sex)进行升序(在排序里边默认是升序)
-
- ORDER BY AGE, PROF DESC
将查询结果按照年龄,职位降序排列。
- 解释下列GROUP BY子句的作用。
- GROUP BY SEX
将查询结果按SEX的值分组,值相等的分为一组,每组保留一条记录。
- GROUP BY AGE, PROF
这个查首先是按照“性别”进行分组,然后在每个性别组中再按照“所在职位”进行分组;
好啦,本篇文章就写到这里吧,可能有不足或错误之处,希望大家可以提出来哦!
都看到这了,还不得一键三连鼓励鼓励作者,哈哈哈哈