student
sno 学号 sname 姓名 ssex 性别 sage 年龄 sdept 所在系
20260701 小李 23 理学
20260702 小刘 22 文学
20260703 小王 18 工学
20260704 小周 19 理学
sc
sno 学号 cno 课程号 grade 成绩
20260701 1 82
20260701 2 91
20260701 3 89
20260703 2 98
20260703 3 96
20260704 1 95
20260704 3 99
course
cno 课程号 cname 课程名 ccredit 学分
1 高等数学 3
2 C语言 2
3 统计学 3
4 英语一 3
5 离散数学 2
6 操作系统 2
7 数据结构 2

一、单表查询

1.查询student表中所有年龄在20以下的学生姓名和年龄

select sname,sage from student where sage<20;

2.查询sc表中成绩大于等于85分且小于等于90分的学号和成绩

select sno,grade from sc where grade>=85 and grade<=90;

3.查询student表中工学系和理学系学生的学号和姓名

select sno,sname from student where sdept='工学' or sdept='理学';

或者

select sno,sname from student where sdept in ('工学','理学');

4.查询sc表中选修了课程号2或3的学生学号

select sno from sc where cno in (2,3);

5.去除4中的重复数据

select distinct sno from sc where cno in (2,3);

6.查询信息course表中高等数学、操作系统、C语言三门课程的学分情况

select cname,ccredit from course where cname in ('高等数学','操作系统','C语言');

7.查询工学系年龄在20岁以下学生的student表信息

select * from student where sage<20 and sdept='工学';

8.查询以“数”开头的课程名和对应课程的学分

select cname,ccredit from course where cname like '数%';

9.向student表插入一条新记录("20260630","小刘同学","女",22,"经管");

insert into student values ("20260630","小刘同学","女",22,"经管");

10.查询student表中姓‘小’同学的信息

select * from student where sname like '小%';

select * from student where sname like '小_';

11.统计姓“小”的学生人数

select count(sno) from student where sname like '小%';

12.查询选修课程号为3的学生的学号和成绩,查询结果按成绩降序排序

select sno,grade from sc where cno=3 order by grade desc;

13.查询学生总人数

select count(*) from student;

14.计算选修1号课程学生的平均成绩

select avg(grade) from sc where cno=1;

15.查询选修1号课程的学生最高分数

select max(grade) from sc where cno=1;

16.查询选修3门及以上课程的学生学号

select sno from sc group by sno having count(cno)>=3;

二、多表查询

1.用自然连接的方式查询小李同学选修的课程号、课程名、成绩

select sname,sc.cno,cname,grade from student natural join sc natural join course where sname='小李';

2.利用左连接方式查询课程表和选修表所有记录的前3条记录

select * from sc left join course on sc.cno=course.cno limit 0,3;

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐