目录
数据:
一、现有一“学生管理”数据库,包括两个表:学生表(Student)和成绩表(Score)。表的结构分别如表1-1的表(一)、表(二)所示,数据如表1-2的表(一)、表(二)所示。
表(一)Student
属性名
数据类型
可否为空
含 义
Sno
Char(3)
否
学号(主键)
Sname
Char(8)
否
学生姓名
Ssex
Char(2)
否
学生性别
Sbirthday
datetime
可
学生出生年月
Class
Char(5)
可
学生所在班级
表(二)Score
属性名
数据类型
可否为空
含 义
Sno
Char(3)
否
学号(主键)
Cno
Char(5)
否
课程号
Degree
Decimal(4,1)
可
成绩
主键:Sno+ Cno
表1-2数据库中的数据
表(一)Student
Sno
Sname
Ssex
Sbirthday
class
108
曾华
男
1977-09-01
95033
105
匡明
男
1975-10-02
95031
107
王丽
女
1976-01-23
95033
101
李军
男
1976-02-20
95033
109
王芳
女
1975-02-10
95031
103
陆君
男
1974-06-03
95031
表(二)Score
Sno
Cno
Degree
103
3-105
92
105
3-105
88
109
3-105
76
101
3-105
64
107
6-166
79
108
6-166
81
题目:
-- 1.查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,class from student
-- 2.查询Student表中不姓“王”的同学记录。
select * from Student where Sname not like '王%'
-- 3.查询Score表中成绩在60到80之间的所有记录。
select * from Score where degree between 60 and 80
-- 4.查询Score表中成绩为85,86或88的记录。
select * from Score where degree in(85,86,88)
-- 5.查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class = 95031 or Ssex ='女'
-- 6.以Class降序查询Student表的所有记录。
select *from student order by class desc
-- 7.以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by Degree desc,Cno asc
-- 8.查询Score表成绩前3名的
select * from Score order by degree desc limit 3
-- 9.查询student表中名字性王的人,并且出生日期在1976以前的
select * from student where Sname like '王%' and Sbirthday <'1976%'
-- 10.给Score表的degree数据加上5.5
update Score set degree=degree+5.5
-- 11.查询学号为108的学生的Sno、Sname和Sbirthday列。
select s1.Sno,s2.Sname,s2.Sbirthday from Score s1 inner join student s2 using(Sno)
-- 12.查询student表中的男女的人数
select Ssex 性别,count(0) from student group by Ssex
-- 13.找出匡明和王丽的班级和成绩(列别名)
select s1.Sname 姓名,s2.degree 成绩 from student s1 inner join Score s2 on s1.sno=s2.sno
where s1.`Sname` in('匡明','王丽')
-- 14.找出男生成绩最高的人(要求显示性别、姓名、成绩)
select s1.Sname 姓名,s1.Ssex 性别,max(degree) 成绩 from student s1 left join Score s2 on s1.sno=s2.sno group by Ssex
-- 15.查询课程号在3-105并且是女生的学生姓名和班级
select s2.Sname 姓名,s2.class 班级 from Score s1 inner join student s2 using(sno) where s1.Cno='3-105' and s2.Ssex='女'
-- 16.查询替换:
如果分数在60~79,则为合格,如果分数在80~89,则为良好,如果分数大于等于90,为优秀,否则为不及格
select s1.degree 原成绩,s2.Sname 姓名,
case
when degree >= 60 and degree <= 79 then '合格'
when degree >= 80 and degree <=89 then '良好'
when degree>=90 then '优秀'
else '不及格'
end as 成绩等级
from Score s1 natural join student s2
-- 17.找出不及格的人和人数
select s2.Sname 姓名,count(0) 不及格人数 from Score s1 natural join student s2 where s1.Degree<60
-- 18.查询第2条开始的4条信息,并且显示姓名、性别、班级
select s2.Sname 姓名,s2.Ssex 性别,s2.class 班级 from Score s1 natural join student s2 limit 1,4
-- 19.查询最高分和最低分的差分
select max(degree)-min(degree) 差分 from Score
-- 20.年龄最大的人,显示姓名和出生日期
select Sname 姓名,Sbirthday 出生日期,year(now())-year(Sbirthday) 现年龄 from student order by Sbirthday limit 1
完结:
幸福是努力出来的,每天努力一点就有收获,加油少年 !