首先创建题目需求的表,分别为sc(学生选课表),student(学生表),stugrade(成绩表)
1.查询学生选课表中的全部数据
select * from sc;
2.查询全体学生的姓名、学号和所在系。
select sname,sno,sdept from student;
3.查询全体学生的姓名及其出生年份。
select sname,2022-sage as 出生年份 from student;
4.查询计算机系全体学生的姓名。
select sname from student where sdept='计算机系';
5.查询年龄在20岁以下的学生的姓名及年龄。
select sname,sage from student where sage<20;
6.查询考试成绩有不及格的学生的学号
select distinct sno from sc where grade<60;
7.查询成绩在70~80分之间的学生,包括学号,课程号和成绩
select sno, cno, grade from sc where grade between 70 and 80;
8.查询年龄在20~23岁之间的学生的姓名、所在系和年龄。
select sname,sdept,sage from student where sage between 20 and 23;
9.查询年龄不在20~23之间的学生姓名、所在系和年龄。
select sname,sdept,sage from student where sage not between 20 and 23;
10.查询信息系和计算机系学生的姓名和性别。
select sname,ssex from student where sdept in ('信息系','计算机系');
11.查询既不是信息系,也不是计算机系学生的姓名和性别。
select sname,ssex from student where sdept not in ('信息系','计算机系');
12.查询姓‘张’的学生的详细信息。
select * from student where sname like '张%';
13.查询所有不姓“刘”的学生。
select * from student where sname not like '刘%';
14.查询无考试成绩的学生的学号和相应的课程号。
select sno,cno from sc where grade is null;
15.查询所有有考试成绩的学生的学号和课程号。
select sno,cno from sc where grade is not null;
16.将学生按年龄的升序排序。
select * from student order by sage;
17.查询计算机系年龄在18~20之间,且性别为男的学生,包括姓名和年龄
select sname,sage from student where sdept=’计算机系’ and sage between 18 and 20 and ssex='男';
聚合函数
1.统计学生总人数。
select count(*) from student;
2.统计选修了课程的学生的人数。
select count(distinct sno) from sc where category='选修';
3.计算9512101号学生的考试总成绩之和。
select sum(grade) from sc where sno='9512101';
4.计算’C01’号课程学生的考试平均成绩。
select avg(grade) from sc where cno='c01';
5.查询选修了’C01’号课程的学生的最高分和最低分。
select max(grade),min(grade) from sc where cno='c01';
6.查询计算机系学生的最大年龄和最小年龄
select max(sage) as max_age,min(sage) as min_age from student where sdept='计算机系';
7.统计每个系的学生人数
select sdept,count(*) from student group by sdept;
8.统计每门课程的选课人数和考试最高分
select cno,count(*),max(grade) from sc group by cno;
9.统计每名学生的选课门数和平均成绩。
select sno,count(*) as 选课门数,avg(grade) as 平均成绩 from sc group by sno;
10.查询修课门数等于或大于4门的学生的平均成绩和选课门数。
select sno,avg(grade) 平均成绩,count(*) 修课门数 from sc group by sno having count(cno)>3;
11.查询总成绩超过200分的学生,要求列出学号,总成绩
select sno, sum(grade) from sc group by sno having sum(grade)>200;
keep learning
转载请注明来源