MySQL基础 练习题2

create table students (
sno varchar(3) not null,
sname varchar(4) not null,
ssex varchar(2) not null,
sbirthday datetime,
class varchar(5)
);

create table courses (
cno varchar(5) not null,
cname varchar(10) not null,
tno varchar(10) not null
);
create table scores (
sno varchar(3) not null,
cno varchar(5) not null,
degree int not null
);
create table teachers (
tno varchar(3) not null,
tname varchar(4) not null,
tsex varchar(2) not null,
tbirthday datetime not null,
prof varchar(6),
depart varchar(10) not null
);

insert into students(sno,sname,ssex,sbirthday, class) values (108 ,‘曾华’ ,‘男’ ,‘1977-09-01’,95033);
insert into students(sno,sname,ssex,sbirthday, class) values (105 ,‘匡明’ ,‘男’ ,‘1975-10-02’,95031);
insert into students(sno,sname,ssex,sbirthday, class) values (107 ,‘王丽’ ,‘女’ ,‘1976-01-23’,95033);
insert into students(sno,sname,ssex,sbirthday, class) values (101 ,‘李军’ ,‘男’ ,‘1976-02-20’,95033);
insert into students(sno,sname,ssex,sbirthday, class) values (109 ,‘王芳’ ,‘女’ ,‘1975-02-10’,95031);
insert into students(sno,sname,ssex,sbirthday, class) values (103 ,‘陆君’ ,‘男’ ,‘1974-06-03’,95031);

insert into courses(cno,cname,tno) values(‘3-105’ ,‘计算机导论’,825);
insert into courses(cno,cname,tno) values(‘3-245’ ,‘操作系统’ ,804);
insert into courses(cno,cname,tno) values(‘6-166’ ,‘数据电路’ ,856);
insert into courses(cno,cname,tno) values(‘9-888’ ,‘高等数学’ ,100);

insert into scores(sno,cno,degree) values(103,‘3-245’,86);
insert into scores(sno,cno,degree) values(105,‘3-245’,75);
insert into scores(sno,cno,degree) values(109,‘3-245’,68);
insert into scores(sno,cno,degree) values(103,‘3-105’,92);
insert into scores(sno,cno,degree) values(105,‘3-105’,88);
insert into scores(sno,cno,degree) values(109,‘3-105’,76);
insert into scores(sno,cno,degree) values(101,‘3-105’,64);
insert into scores(sno,cno,degree) values(107,‘3-105’,91);
insert into scores(sno,cno,degree) values(108,‘3-105’,78);
insert into scores(sno,cno,degree) values(101,‘6-166’,85);
insert into scores(sno,cno,degree) values(107,‘6-106’,79);
insert into scores(sno,cno,degree) values(108,‘6-166’,81);

insert into teachers (tno,tname,tsex,tbirthday, prof,depart) values(804,‘李诚’,‘男’,‘1958-12-02’,‘副教授’,‘计算机系’);
insert into teachers (tno,tname,tsex,tbirthday, prof,depart) values(856,‘张旭’,‘男’,‘1969-03-12’,‘讲师’,‘电子工程系’);
insert into teachers (tno,tname,tsex,tbirthday, prof,depart) values(825,‘王萍’,‘女’,‘1972-05-05’,‘助教’,‘计算机系’);
insert into teachers (tno,tname,tsex,tbirthday, prof,depart) values(831,‘刘冰’,‘女’,‘1977-08-14’,‘助教’,‘电子工程系’);

简单难度:
1、 查询 students 表中的所有记录的 sname、ssex 和 class 列。
2、 查询教师所有的单位即不重复的 depart 列。
3、 查询 students 表的所有记录。
4、 查询scores 表中成绩在 60 到 80 之间的所有记录。
5、 查询 scores 表中成绩为 85,86 或 88 的记录。
6、 查询students 表中“95031”班或性别为“女”的同学记录。
7、 以 class 降序查询students 表的所有记录。
8、 以 cno 升序、 degree 降序查询 scores表的所有记录。
9、 查询“95031”班的学生人数。
10、查询 scores 表中的最高分的学生学号和课程号。
11、查询‘3-105’号课程的平均分。
12、查询 students 表中不姓“王”的同学记录。
13、查询 students 表中最大和最小的 sbirthday 日期值。
14、以班号和年龄从大到小的顺序查询 students 表中的全部记录。

中等难度:
15、查询至少有 2名男生的班号。
16、查询 scores 表中至少有 5 名学生选修的并以 3 开头的课程的平均分数。
17、查询最低分大于70,最高分小于90 的 sno列。
18、查询所有学生的sname、cno 和degree列。
19、查询所有学生的 sno 、cname 和degree 列。
20、查询95033班所选课程的平均分。
21、查询95033班和 95031 班全体学生的记录。
22、查询存在有 85分以上成绩的课程cno.
23、查询“男”教师及其所上的课程。
24、查询最高分同学的sno、cno和degree列。
25、查询和“李军”同性别的所有同学的sname.
26、查询和“李军”同性别并同班的同学sname.
27、查询所有任课教师的tname和depart. (没代课的不统计)
28、查询所有未讲课的教师的tname和depart.

高等难度:
29、查询“李诚“教师任课的学生成绩。
30、查询选修某课程的同学人数多于 5 人的教师姓名。
31、查询出“计算机系“教师所教课程的成绩表。
32、查询所有教师和同学的name、sex 和 birthday. (使用 union )
33、查询所有“女”教师和“女”同学的 name 、sex 和 birthday. . (使用 union )
34、查询所有学生的 sname 、 cname 和 degree 列。
35、查询 scores 中选学一门以上课程的同学中分数为非最高分成绩的记录。
参考答案:

简单难度:

1、 查询 students 表中的所有记录的 sname、ssex 和 class 列。

select sname,ssex,class from students;

2、 查询教师所有的单位即不重复的 depart 列。

select distinct depart from teachers;

3、 查询 students 表的所有记录。

select * from students;

4、 查询scores 表中成绩在 60 到 80 之间的所有记录。

select * from scores where degree between 60 and 80;

5、 查询 scores 表中成绩为 85,86 或 88 的记录。

select * from scores where degree in (
  • 15
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值