面试题:请书写下面的SQL语句如下表:Student(Stid,Sname,Sage,Ssex) 学生表 Stid:学号 Sname:学生姓名 Sage:学生年龄……

题目:

请书写下面的SQL语句如下表:

Student(Stid,Sname,Sage,Ssex) 学生表 Stid:学号 Sname:学生姓名 Sage:学生年龄 Ssex:学生性别
Course(Cid,Cname,Tid) 课程表 Cid:课程编号 Cname:课程名称 Tid:教师编号
SC(Sid,Cid,score) 成绩表 Sid:学号 Cid:课程编号 score:成绩
Teacher(Tid,Tname) 教师表 Tid:教师编号 Tname:教师名字

一、查询平均成绩大于60的同学的学号和平均成绩

二、查询所有同学的学号、姓名、选棵数、总成绩

三、查询姓‘李’的老师的个数

四、查询没有学过‘叶平’老师可的同学的学号、姓名

答案:

第一题:

select sid,avg(score) avgScore from sc group by sid having avg(score)>60;

解释:本题想求的数据在SC成绩表中,求平均成绩自然是要分组,但是还要求平均成绩大于60,那就需要分组之后使用having过滤

第二题:

select sc.sid,s.sname,count(sc.cid) cidNum,sum(sc.score) scoreSum from sc join student s on sc.sid=s.stid group by sc.sid;

解释:本题想求的数据在SC成绩表和Student学生表中,所以可以求出使用SC表连接Student表,既然问的是有关学生的内容,在SC成绩表中肯定是以sid进行分组,分组之后就可以求选课数和总成绩了

第三题:

select count(*) number from teacher where tname like '李%';

解释:本次想求的数据在Teacher教师表中,求的是姓李的老师,那就使用模糊查询,然后对模糊查询出来的内容进行计数即可

第四题:

select stid,sname from student where stid not in (select distinct sid from sc where cid in (select cid from course where tid = (select tid from teacher where tname='叶平')));

解释:这次查询的涉及四张表,首先要在Teacher表中查询叶平老师的教师编号,可以用:select tid from teacher where tname=‘叶平’;,然后去Course表中查询叶平老师教授的课程编号,可以用:select cid from course where tid = (select tid from teacher where tname=‘叶平’);,然后去SC表中查找选过叶平老师所教授课程的学生编号,可以用:select cid from course where tid = (select tid from teacher where tname=‘叶平’);,然后去学生表中查找没有学过叶平老师课的学生编号和姓名,可以用:select stid,sname from student where stid not in (select distinct sid from sc where cid in (select cid from course where tid = (select tid from teacher where tname=‘叶平’)));

建表语句:

如果想用建表来体验一下的,我提供自建的表格代码如下:

drop table if exists SC;
drop table if exists Student;
drop table if exists Course;
drop table if exists Teacher;
create table Teacher(
	Tid int,
	Tname varchar(255),
	primary key(Tid)
);
create table Course(
	Cid int,
	Cname varchar(255),
	Tid int,
	primary key(Cid),
	foreign key(Tid) references Teacher(Tid)
);
create table Student(
	Stid int,
	Sname varchar(255),
	Sage int,
	Ssex char(1),
	primary key(Stid)
);
create table SC(
	id int auto_increment,
	Sid int,
	Cid int,
	Score int,
	primary key(id),
	foreign key(Sid) references Student(Stid),
	foreign key(Cid) references Course(Cid)
);
INSERT INTO `teacher` VALUES ('1', '李华');
INSERT INTO `teacher` VALUES ('2', '李明轩');
INSERT INTO `teacher` VALUES ('3', '叶平');
INSERT INTO `teacher` VALUES ('4', '华晓波');

INSERT INTO `course` VALUES ('1', '数学', '2');
INSERT INTO `course` VALUES ('2', '英语', '3');
INSERT INTO `course` VALUES ('3', '化学', '1');
INSERT INTO `course` VALUES ('4', '美术', '4');
INSERT INTO `course` VALUES ('5', '语文', '2');
INSERT INTO `course` VALUES ('6', '生物', '3');

INSERT INTO `student` VALUES ('1001', '李四', '12', '女');
INSERT INTO `student` VALUES ('1002', '张三', '24', '男');
INSERT INTO `student` VALUES ('1003', '王五', '14', '女');

INSERT INTO `sc` VALUES ('1', '1001', '1', '45');
INSERT INTO `sc` VALUES ('2', '1001', '2', '80');
INSERT INTO `sc` VALUES ('3', '1001', '5', '48');
INSERT INTO `sc` VALUES ('4', '1002', '6', '78');
INSERT INTO `sc` VALUES ('5', '1002', '2', '90');
INSERT INTO `sc` VALUES ('6', '1002', '1', '49');
INSERT INTO `sc` VALUES ('7', '1002', '5', '70');
INSERT INTO `sc` VALUES ('8', '1003', '3', '90');
INSERT INTO `sc` VALUES ('9', '1003', '1', '70');

你可能感兴趣的:(java学习之路)