资源描述
1、 查询xs表中计算机专业同学的学号、姓名和总学分,结果中各列的标题分别指定为number、name和mark。
Select 学号 as number,姓名 as name,总学分 as mark from xs where 专业名="计算机";
2、
查询xs表中计算机专业各同学的学号、姓名和总学分,对总学分按如下规则进行替换:
若总学分为空值,替换为“尚未选课”;若总学分小于50,替换为“不及格”;若总学分在50~52之间,替换为“合格”;若总学分大于52,替换为“优秀”。总学分列的标题更改为“等级”。
select 学号,姓名,
case
when 总学分 is null then "尚未选课"
when 总学分 < 50 then "不及格"
when 总学分 >=50 and 总学分 <=52 then "合格"
else "优秀"
end as 等级
from xs
where 专业名="计算机";
3、 按120分制重新计算成绩,显示xs_kc表中学号为81101的学生成绩信息。
select 学号,课程号,成绩*1.2 as 成绩120 from xs_kc where 学号="81101";
3 rows in set
4、 对xscj数据库的xs表只选择专业名和总学分,消除结果集中的重复行。
select distinct 专业名,总学分 from xs;
8 rows in set
5、 统计备注不为空的学生数目。
select count(备注) as 人数 from xs where 备注 is not null;
6、 统计总学分在50分以上的人数。
select count(总学分) as 人数 from xs where 总学分 > 50;
1 row in set
7、 求选修101课程的学生的最高分和最低分。
select max(成绩) as 最高分 ,min(成绩) as 最低分 from xs_kc where 课程号="101";
1 row in set
8、 求学号081101的学生所学课程的总成绩。
select sum(成绩) as 总成绩 from xs_kc where 学号="81101";
9、 求选修101课程的学生的平均成绩。
select 学号,avg(成绩) from xs_kc where 课程号="101";
10、 求选修101课程的成绩的方差。
select variance(成绩) from xs_kc where 课程号="101";
11、 求选修101课程的成绩的标准差。
select stddev(成绩) from xs_kc where 课程号="101";
12、 求选修了206课程的学生的学号。
select group_concat(学号) from xs_kc where 课程号="206";
13、 从xs表中检索出所有学生的信息,并使用表别名student。
select * from xs as student;
14、 查找xscj数据库中所有学生选过的课程名和课程号
select distinct kc.课程名,xs_kc.课程号 from kc,xs_kc where kc.课程号=xs_kc.课程号;
3 rows in set
15、 用FROM子句的JOIN关键字表达下列查询:查找选修了206课程且成绩在80分以上的学生姓名及成绩。
select 姓名,成绩 from xs inner join xs_kc on xs.学号=xs_kc.学号 where 课程号="206" and 成绩 > 80;
3 rows in set
16、 用FROM的JOIN关键字表达下列查询:查找选修了“计算机基础”课程且成绩在80分以上的学生学号、姓名、课程名及成绩。
select xs.学号,姓名,课程名,成绩 from xs join xs_kc on xs.学号=xs_kc.学号 join kc on xs_kc.课程号=kc.课程号 where 课程名="计算机基础" and 成绩 > 80;
8 rows in set
17、 查找xscj数据库中课程不同、成绩相同的学生的学号、课程号和成绩。
select a.学号,a.课程号,b.课程号,a.成绩 from xs_kc as a join xs_kc as b on a.成绩=b.成绩 and a.学号=b.学号 and a.课程号 != b.课程号;
2 rows in set
18、 查找所有学生情况及他们选修的课程号,若学生未选修任何课,也要包括其情况。
select xs_kc.*,课程号 from xs left outer join xs_kc on xs.学号=xs_kc.学号;
19、 查找被选修了的课程的选修情况和所有开设的课程名。
select xs_kc.*,课程名 from xs_kc right join kc on xs_kc.课程号=kc.课程号;
20、 列出学生所有可能的选课情况。
mysql> select 学号,姓名,课程号,课程名
-> from xs cross join kc;
21、 查询xscj数据库xs表中学号为81101的学生的情况。
select 学号,姓名,总学分 from xs where 学号="81101";
1 row in set
22、 查询xs表中总学分大于50分的学生的情况。
select * from xs where 总学分 > 50;
23、 查询xs表中备注为空的同学的情况。
select * from xs where 备注 is null;
mysql> select * from xs where 备注 <=> null;
24、 查询xs表中专业为计算机,性别为女(0)的同学的情况。
select * from xs where 专业名="计算机" and 性别=0;
25、 查询xscj数据库xs表中姓“王”的学生学号、姓名及性别。
select 学号,姓名,性别 from xs where 姓名 like "王%";
26、 查询xscj数据库xs表中,学号倒数第二个数字为0的学生的学号、姓名及专业名。
select 学号,姓名,专业名 from xs where 学号 like "%0_";
|查询xs表中名字包含下画线的学生学号和姓名。
select 学号,姓名 from xs where 学号 like "%#_%" escape "#";
28、查询xscj数据库xs表中不在1993年出生的学生情况。
select * from xs where 出生时间<=>1993;
29、查询xs表中专业名为“计算机”、“通信工程”或“无线电”的学生的情况。
select * from xs where 专业名="计算机" or 专业名="通信工程" or 专业名="无线电";
30、查询xscj数据库中总学分尚不定的学生情况。
select * from xs where 总学分 is null;
31、查找在xscj数据库中选修了课程号为206的课程的学生的姓名、学号。
select 姓名,学号 from xs where 学号 in(select 学号 from xs_kc where 课程号="206");
32、查找未选修离散数学的学生的姓名、学号、专业名。
select 姓名,学号,专业名 from xs where 学号 not in (select 学号 from xs_kc where 课程号 in (select 课程号 from xs where 课程号="离散数学"));
33、查找选修了离散数学的学生学号。
select 学号 from xs_kc where 课程号 = (select 课程号 from kc where 课程名="离散数学");
34、查找xs表中比所有计算机系的学生年龄都大的学生学号、姓名、专业名、出生日期。
select 学号,姓名,专业名,出生时间 from xs where 出生时间<all(select 出生时间 from xs where 专业名="计算机");
35、查找xs_kc表中课程号206的成绩不低于课程号101的最低成绩的学生的学号。
select 学号 from xs_kc where 课程号="206" and 成绩>any(select 成绩 from xs_kc where 课程号="101");
36、查找选修206号课程的学生姓名。
select 姓名 from xs where exists (select * from xs_kc where 课程号="206" and 学号=xs.学号);
37、查找选修了全部课程的同学的姓名。
select 姓名 from xs where not exists (select * from kc where not exists (select * from xs_kc where 学号=xs.学号 and 课程号=kc.课程号));
38、从xs表中查找总学分大于50分的男同学的姓名和学号。
select 姓名,学号 from xs where 总学分>50 and 性别=1;
select 姓名,学号 from xs where 学号 in (select 学号 from xs where 总学分>50 and 性别=1);
select 姓名,学号,总学分 from (select 姓名,学号,性别,总学分 from xs where 总学分 > 50) as student where 性别=1;
39、从xs表中查找所有女学生的姓名、学号,以及与81101号学生的年龄差距。
select 姓名,学号,year(出生时间)-year((select 出生时间 from xs where 学号="81101" ) ) as 年龄差距 from xs where 性别=0;
40、查找与81101号学生性别相同、总学分相同的学生学号和姓名。
select 学号,姓名 from xs where (性别,总学分)=(select 性别,总学分 from xs where 学号="81101");
41、将xscj数据库中各专业名输出。
select 专业名 from xs group by 专业名;
42、求xscj数据库中各专业的学生数。
select 专业名,count(*) as "学生数" from xs group by 专业名;
43、求被选修的各门课程的平均成绩和选修该课程的人数。
select 课程号,avg(成绩) as "平均成绩",count(*) as "总人数" from xs_kc group by 课程号;
44、在xscj数据库上产生一个结果集,包括每个专业的男生人数、女生人数、总人数,以及学生总人数。
select 专业名,性别,count(*) as 总人数 from xs group by 专业名,性别 with rollup;
45、在xscj数据库上产生一个结果集,包括每门课程各专业的平均成绩、每门课程的总平均成绩和所有课程的总平均成绩。
select 课程名,专业名,avg(成绩) as 平均成绩 from xs_kc,kc,xs where xs_kc.课程号 =kc.课程号 and xs_kc.学号=xs.学号 group by 课程名,专业名 with rollup;
46、查找xscj数据库中平均成绩在85分以上的学生的学号和平均成绩。
select 学号,avg(成绩) as "平均成绩" from xs_kc group by 学号 having avg(成绩)>=85;
47、查找选修课程超过2门且成绩都在80分以上的学生的学号。
select 学号 from xs_kc group by 学号 having count(*) >2;
48、查找通信工程专业平均成绩在85分以上的学生的学号和平均成绩。
select 学号,avg(成绩) as "平均成绩" from xs_kc where 学号 in (select 学号 from xs where 专业名="通信工程") group by 学号 having avg(成绩) >=85;
49、将通信工程专业的学生按出生日期先后排序。
select 学号,姓名,专业名,出生时间 from xs where 专业名="通信工程" order by 出生时间;
50、将计算机专业学生按其平均成绩排列。
select 学号,姓名,专业名 from xs where 专业名="计算机" order by (select avg(成绩) from xs_kc group by xs_kc.学号 having xs.学号=xs_kc.学号);
51、查找xs表中学号最靠前的5位学生的信息。
select * from xs order by 学号 limit 5;
52、查找xs表中从第4位同学开始的5位学生的信息。
select * from xs order by 学号 limit 3,5
;
53、查找学号为81101和学号为81210的两位同学的信息。
select * from xs where 学号="81101" union select * from xs where 学号="81210";
54、一行一行地浏览KC表中满足要求的内容,要求第一行为学分大于4的第一行数据。
展开阅读全文