题目数据:
drop database if exists 学生课程系统;
create database 学生课程系统;
use 学生课程系统;
create table Student
(Sno int primary key,
Sname nchar(10) ,
Ssex nchar(2),
Sage int,
Sdept nvarchar(30));
create table Course
(Cno int primary key,
Cname nvarchar(30),
Cpno int,
Ccredit int);
create table SC
(Sno int,
Cno int,
Grade int,
primary key(Sno,Cno));
insert into Student values
(201215121,‘李勇’,‘男’,20,‘CS’),
(201215122,‘刘晨’,‘女’,19,‘CS’),
(201215123,‘王敏’,‘女’,18,‘MA’),
(201215124,‘张立’,‘男’,19,‘IS’),
(201215125,‘刘婷’,‘女’,21,‘MA’),
(201215126,‘李悦’,‘女’,19,‘CS’),
(201215127,‘寒梅’,‘女’,18,‘IS’),
(201215128,‘夏雨’,‘女’,19,‘MA’),
(201215129,‘童琳’,‘女’,21,‘MA’);
insert into Course values
(1,‘数据库’,5,4),
(2,‘数学’,NULL,2),
(3,‘信息系统’,1,4),
(4,‘操作系统’,6,3),
(5,‘数据结构’,7,4),
(6,‘数据处理’,NULL,2),
(7,‘PASCAL’,6,4);
insert into SC values
(201215121,1,92),
(201215121,2,85),
(201215121,3,88),
(201215122,2,90),
(201215122,3,80),
(201215124,1,55);
1.检索年龄大于18的男学生的学号和姓名。
select Sno,Sname from student where Sage > 18 and Ssex=‘男’;
2.检索姓名以"李"打头的所有学生的姓名和年龄。
select Sname,Sage from student where Sname like ‘李%’;
3.把选课成绩不及格的成绩全改为空值。
update sc set Grade=null where grade<60;
4.查询每门课程的学生平均成绩(显示课程号和平均成绩,不使用别名)。
select cno,avg(grade) from sc
group by cno;
5.查询CS和MA系(Sdept)的学生姓名和性别(条件判断时CS在前,MA在后)
select Sname,Ssex from student
where Sdept=‘CS’ or Sdept=‘MA’;
6.查询年龄介于17岁(包含17)到19岁(包含19)之间的学生信息。
select * from student
where sage between 17 and 19;
7.在成绩表中检索成绩为空值的学生学号和成绩。
select sno,grade from sc
where Grade is null;
8.查询课程名称中第二个字不是‘据’的课程号、课程名,学分。
select Cno,Cname,Ccredit from course
where Cname not like ‘_据%’;
9.查询‘cs’系年龄大于18岁的女同学学号,姓名(按题目给出的信息顺序进行条件判断)。
select sno,sname from student
where Sdept=‘CS’ and Sage>18 and Ssex=‘女’;
10.随机生成一个0到1000之间的随机整数(包括0和1000)。
select floor(rand()*1001);
11.统计参与选修课程的学生人数。
select count(distinct sno) from sc;
统计女同学的每一年龄组(多于1人)有多少人?要求查询结果按人数升序排列,人数相同的按年龄降序排列。
select sage,count()
from student
where ssex=‘女’
group by sage
having count()>1
order by count(*),sage desc;
13.统计既不属于‘cs’系,也不属于‘MA’系的学生信息(判断条件时’cs’在前,'ma’在后)。
select * from student
where Sdept!=‘CS’ and Sdept!=‘MA’;
14.查询每个学生的学号,出生年份及所在系(使用别名)
select sno 学号,year(now())-Sage 出生年份,Sdept 所在系 from student;