Day17-作业

1.学生表(student)

create table student(sno varchar(3),sname varchar(10) not null,ssex varchar(3),sbirthday datetime,class varchar(5) not null,primary key(sno))engine=innodb default charset=utf8;



2.课程表
create table course(cno varchar(10),cname varchar(30) not null,tno varchar(10),primary key(cno))engine=innodb default charset=utf8;


3.成绩表
create table grade(sno varchar(3),cno varchar(5),degree tinyint not null,primary key(sno,cno));


4.教师表
create table teacher(tno varchar(5),tname varchar(20) not null,tsex varchar(2),tbirthday datetime,prof varchar(20) not null,depart varchar(30) not null,primary key(tno))engine=innodb default charset=utf8;



5.等级表
create table rank(down tinyint not null,up tinyint not null,rank varchar(2),primary key(rank))engine=innodb default charset=utf8;


6.将下列数据插入表中
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' , '1977-09-01',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,’1975-10-02’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,’1976-01-23’, 95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,’1976-02-20’,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,’1975-02-10’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,’1974-06-03’,95031);


INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论', '825');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' , '804');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' , '856');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' , '100');

INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES ('103','3-245',86);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);


INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');


insert into rank(down,up,rank) values(90,100,'A');
insert into rank(down,up,rank) values(80,89,'B');
insert into rank(down,up,rank) values(70,79,'C');
insert into rank(down,up,rank) values(60,69,'D');
insert into rank(down,up,rank) values(0,59,'E');


sql语句练习:
1、 查询Student表中的所有记录的Sname、Ssex和Class列。



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



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

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

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



6、 查询Student表中“95031”班或性别为“女”的同学记录。

7、 以Class降序查询Student表的
所有记录。

8、 以Cno升序、Degree降序查询Grade表所有记录。

9、 查询“95031”班的学生人数。

10、查询Grade表中的最高分的学生学号和课程号。

11、查询‘3-105’号课程的平均分。


12、查询Grade表中至少有5名学生选修的并以3开头的课程的平均分数。
mysql> select avg(degree) as '平均成绩' from grade where cno like '3%' group by cno having count(*)>=5;


13、查询最低分大于70,最高分小于90的Sno列。


14、查询所有学生的Sname、Cno和Degree列。
mysql> select sname as Sname,cno as Cno,degree as Degree from student as s join grade as g on s.sno=g.sno;

15、查询所有学生的Sno、Cname和Degree列。
mysql> select sno as Sno,cname as Cname,degree as Degree from student as s join degree as d on s.sno=d.sno join course as c d.cno=c.cno;

16、查询所有学生的Sname、Cname和Degree列。
mysql> select sname as Sname,cname as Cname,degree as Degree from student as s join grade as g on s.sno=g.sno join course as c on g.cno=c.cno;

17、查询“95033”班所选课程的平均分。
mysql> select avg(degree) as '平均分' from student as s join grade as g on s.sno=g.sno where class='95033';

18、查询选修课成绩为A等的学生信息
mysql> select * from student as s join grade as g on s.sno=g.sno join course as c on g.cno=c.cno,rank where down=90 and up=100 and degree between down and up;

19、在grade表中查询选修“3-105”课程的成绩高于选修“3-105”并且学号为“109”的所有同学的记录。
mysql> select s.sno,sname,ssex,sbirthday,class from student as s join grade as g on s.sno=g.sno where cno='3-105'and degree>(select degree from grade where sno='109' and cno='3-105');

20、查询grade中小于最高分成绩并且选学一门以上课程的同学的sno。
mysql> select sno from grade where degree<(select max(degree) from grade) group by sno having count()>1;

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
mysql> select * from grade where degree>(select degree from grade where sno='109' and cno='3-105');

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
mysql> select sno as Sno,sname as Sname,sbirthday as Sbirthday from student where sbirthday=(select sbirthday from student where sno='108');

23、查询“张旭“教师任课的学生成绩。
mysql> select s.sname,g.degree from student as s join grade as g on s.sno=g.sno join course as c on g.cno=c.cno join teacher as t on c.tno=t.tno where t.tname='张旭';

24、查询选修某课程的同学人数多于5人的教师姓名。
mysql>select tname from teacher as t join course as c on t.tno=c.tno where c.cno=(select cno from grade group by cno having count(
)>5);

25、查询95033班和95031班全体学生的记录。
mysql>select * from student where class='95033' or class='95031';

26、查询存在有85分以上成绩的课程Cno.
mysql> select cno as Cno from grade where degree>=85 group by Cno;

27、查询出“计算机系“教师所教课程的成绩表。
mysql> select sname as '学生',cname as '课程名',tname as '老师',depart as '院系',degree as '成绩' from student as s join grade as g on s.sno=g.sno join course as c on g.cno=c.cno join teacher as t on c.tno=t.tno where t.deprt='计算机系' order by cname;


28、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学最低分的Cno、Sno和Degree,并按Degree从高到低次序排序。
mysql> select cno as Cno,sno as Sno,degree as Degree from grade where cno='3-105' and degree>(select min(degree) from grade where cno='3-245') order by Degree desc;


29、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
mysql> select sno from grade where sno in ( select sno from grade where cno='3-105' and sno in ( select sno from grade where cno='3-245')) and cno='3-105' and degree in(select max(degree) from grade where sno in (select sno from grade where cno='3-105' and sno in ( select sno from grade where cno='3-245')) group by sno);



30、查询所有教师和同学的name、sex和birthday.
mysql> select sname as name,ssex as sex,sbirthday as birthday from student union select tname,tsex,tbirthday from teacher;



31、查询所有“女”教师和“女”同学的name、sex和birthday.
mysql> select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女' union select tname,tsex,tbirthday from teacher where tsex='女';


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,752评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,100评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,244评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,099评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,210评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,307评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,346评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,133评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,546评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,849评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,019评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,702评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,331评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,030评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,260评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,871评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,898评论 2 351

推荐阅读更多精彩内容