数据库【简单查询】

这篇文章呢,主要是对数据库中教学管理数据库进行简单查询的一些小练习!!!

首先呢,我们需要在数据库教学管理中建立几个表

 

 

 

 

 

 

接下来呢,就是简单的练习啦......

实验内容

实验内容

  1. 指定列或全部列查询
  1. 查询Student学生表中全体学生的详细信息。
select * from student
  1. 查询所有学生的姓名及其出生年份。
select sname, birthday from student
  1. 查询所有教师的姓名和性别。
select tname ,sex from teacher
  1. 查询课程的课程编号、课程名及学分。
select cno,cname,credit from course
  1. 查询有学生选修的课程号。
select distinct cno from sc

  1. 按条件查询
  1. 查询学分为4的课程名称。
select cno,credit from course where credit = (4)
  1. 查询学号为S3的学生姓名、系名及年龄。
select sname, dept, year(getdate())-year(birthday)  as age from Student where SNO='s3'
  1. 查询考试成绩有不及格的学生学号。
select distinct sno score from SC where SCORE < 60
  1. 查询有选修课程没考试的学生学号和课程号。
select sno,cno from sc where score is NULL
  1. 查询考试成绩有75-85分之间的学生学号和成绩。
select sno,score from sc where score between 75 and 85
  1. 查询年龄在20-22岁之间的学生的姓名、系别和年龄。
select SNAME,year(getdate())-year(birthday) as 年龄,dept

from student

where year(getdate())-year(birthday) between 20 and 22
  1. 查询所有姓李或姓王的学生的姓名、学号和性别。
select sname,sno,sex from student where sname like'[李王]%'
  1. 查询名字中第二个字为“晓”字的男生姓名和系别。
select sname,dept from student where sname like '_[晓]%'
  1. 查询薪水在3000以上的信息系教师姓名。(薪水=工资+岗位津贴)
select tname from teacher

where sal+comm > 3000 and dept = '计算机'
  1. 查询计算机和通信的教师姓名及年龄。
select tname,age from teacher

where dept = '计算机'and dept = '通信' 
  1. 对查询结果排序
  1. 查询学生信息,按照出生日期降序排列。
select * from student order by birthday desc
  1. 查询计算机、电信学生的姓名、系名,结果按系名升序、姓名降序排序。

select sname,dept from student

where (dept = '计算机' or dept = '通信')

order by dept asc, sname desc

  1. 查询所选修了C2课程且有成绩的学生的学号、课程号和成绩,并按成绩降序排序。
select sno,cno,score from sc where cno = 'c2'

order by score desc
  1. 查询学号为S2的学生参加考试的成绩,结果按照成绩降序排列。
select sno ,score from sc where sno = 's2'

order by score desc
  1. 使用聚集函数的查询
  1. 查询计算机学生的总人数。
select count(*) from student where dept = '计算机'
  1. 查询C3课程考试的成绩最高分及最低分。
select max(score) 最高分,min(score) 最低分

from sc where cno = 'c3'
  1. 查询学号为S1的学生所有课程的平均成绩。
select avg(score) from sc where sno = 's1'
  1. 查询参加了C1课程考试的学生人数。
select count(cno) from sc where cno = 'c1'
  1. 查询所有姓王的计算机教师人数。
select count(dept) from teacher

where tname like'王%'and dept = '计算机'
  1. 分组统计查询
  1. 查询学生中男女生各有多少人。
select sex,count(*)

from student

group by sex
  1. 查询各个系男女教师的人数。
select sex,count(*)

from teacher

group by sex

查询每个学生的学号及所有考试的平均成绩。

select sno, AVG(score)

from sc

group by sno
  1. 查询各门课程的课程号及选课人数。
select cno as 课程号,count(sno) as 选课人数

from sc group by cno
  1. 查询选修了两门以上课程的学生学号和平均成绩。
select sno 学号,avg(score) 平均成绩

from sc group by sno

having count(*)>2
  1. 查询所有课程考试成绩均及格的学生学号。
select sno from sc

group by sno

having min(score) >= 60

思考

  1. 查询至少有4个同学选修的课程号。
select cno from sc

group by cno

having count(*)>=4
  1. 查询选修了两门以上课程的学生学号。
select sno 学号

from sc group by sno

having count(*)>2
  1. 解释下列ORDER BY子句的作用。
    1. ORDER BY SNO, SEX

order by 后根据(sno,sex)进行升序(在排序里边默认是升序)

    1. ORDER BY AGE, PROF DESC

将查询结果按照年龄,职位降序排列。

  1. 解释下列GROUP BY子句的作用。
  1. GROUP BY SEX

将查询结果按SEX的值分组,值相等的分为一组,每组保留一条记录。

  1. GROUP BY AGE, PROF

这个查首先是按照“性别”进行分组,然后在每个性别组中再按照“所在职位”进行分组;

好啦,本篇文章就写到这里吧,可能有不足或错误之处,希望大家可以提出来哦!

都看到这了,还不得一键三连鼓励鼓励作者,哈哈哈哈

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值