面试必问SQL语句练习35题(mysql版)

表名和字段
–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数
面试必问SQL语句练习35题(mysql版)_第1张图片

CREATE TABLE `Student`(
	`s_id` VARCHAR(20),
	`s_name` VARCHAR(20) NOT NULL DEFAULT '',
	`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
	`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
	PRIMARY KEY(`s_id`)
);

CREATE TABLE `Course`(
	`c_id`  VARCHAR(20),
	`c_name` VARCHAR(20) NOT NULL DEFAULT '',
	`t_id` VARCHAR(20) NOT NULL,
	PRIMARY KEY(`c_id`)
);

CREATE TABLE `Teacher`(
	`t_id` VARCHAR(20),
	`t_name` VARCHAR(20) NOT NULL DEFAULT '',
	PRIMARY KEY(`t_id`)
);

CREATE TABLE `Score`(
	`s_id` VARCHAR(20),
	`c_id`  VARCHAR(20),
	`s_score` INT(3),
	PRIMARY KEY(`s_id`,`c_id`)
);

INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女');

INSERT INTO Course VALUES('01' , '语文' , '02');
INSERT INTO Course VALUES('02' , '数学' , '01');
INSERT INTO Course VALUES('03' , '英语' , '03');


INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');


INSERT INTO Score VALUES('01' , '01' , 80);
INSERT INTO Score VALUES('01' , '02' , 90);
INSERT INTO Score VALUES('01' , '03' , 99);
INSERT INTO Score VALUES('02' , '01' , 70);
INSERT INTO Score VALUES('02' , '02' , 60);
INSERT INTO Score VALUES('02' , '03' , 80);
INSERT INTO Score VALUES('03' , '01' , 80);
INSERT INTO Score VALUES('03' , '02' , 80);
INSERT INTO Score VALUES('03' , '03' , 80);
INSERT INTO Score VALUES('04' , '01' , 50);
INSERT INTO Score VALUES('04' , '02' , 30);
INSERT INTO Score VALUES('04' , '03' , 20);
INSERT INTO Score VALUES('05' , '01' , 76);
INSERT INTO Score VALUES('05' , '02' , 87);
INSERT INTO Score VALUES('06' , '01' , 31);
INSERT INTO Score VALUES('06' , '03' , 34);
INSERT INTO Score VALUES('07' , '02' , 89);
INSERT INTO Score VALUES('07' , '03' , 98);

– 1、查询01课程比02课程成绩高的学生的信息及课程分数

SELECT a.*,b.`s_score` 01_sc,c.`s_score` 02_sc FROM Student a ,Score b,Score c
WHERE a.`s_id` = b.`s_id`
AND a.`s_id` = c.`s_id`
AND b.`c_id` = '01'
AND c.`c_id` = '02'
AND b.`s_score`>c.`s_score`; 

– 2、查询01课程比02课程成绩低的学生的信息及课程分数

SELECT a.* ,b.`s_score` 01_sc ,c.`s_score`02_sc FROM Student a,Score b,Score c
WHERE a.`s_id` = b.`s_id`
AND a.`s_id` = c.`s_id`
AND b.`c_id` = '01'
AND c.`c_id` = '02'
AND b.`s_score`

– 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT stu.`s_id`,stu.`s_name`,AVG(sc.`s_score`) AS avg_score FROM Student stu 
JOIN Score sc ON stu.s_id = sc.s_id
GROUP BY stu.`s_id`,stu.`s_name`HAVING avg_score >=60;

– 4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT stu.`s_id`,stu.`s_name`,COUNT(sc.`c_id`)AS sum_course,SUM(sc.`s_score`)AS sum_score 
	FROM Student stu
	LEFT JOIN Score sc ON stu.`s_id`=sc.`s_id`
	GROUP BY stu.s_id,stu.s_name;

– 5、查询"李"姓老师的数量

SELECT COUNT(t_id)FROM Teacher WHERE t_name LIKE '李%';

– 6、查询学过"张三"老师授课的同学的信息

SELECT stu.* FROM student stu
JOIN score sc ON stu.`s_id`=sc.`s_id`WHERE sc.`c_id`IN(
	SELECT c_id FROM Course WHERE t_id=(
	SELECT t_id FROM Teacher WHERE t_name = '张三'));

–7、查询没学过"张三"老师授课的同学的信息

SELECT * FROM Student stu
WHERE stu.`s_id` NOT IN (
	SELECT a.`s_id` FROM Student a JOIN Score b ON a.`s_id` = b.`s_id`WHERE b.`c_id`IN(
	SELECT c.`c_id` FROM Course c JOIN Teacher t ON c.`t_id` = t.`t_id`WHERE t_name ='张三'
	)
);

–8、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT a.* FROM Student a,Score b ,Score c 
WHERE a.`s_id` =b.`s_id`
AND a.`s_id` = c.`s_id`
AND b.`c_id` = '01'
AND c.`c_id`= '02'; 

– 9、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT a.* FROM Student a WHERE a.`s_id`IN(SELECT s_id FROM score WHERE c_id ='01')
AND a.`s_id`NOT IN (SELECT s_id FROM score WHERE c_id ='02');

– 10、查询没有学全所有课程的同学的信息

SELECT stu.* FROM Student stu
LEFT JOIN Score s ON s.`s_id` = stu.`s_id`
GROUP BY stu.`s_id` HAVING COUNT(s.`c_id`)<(SELECT COUNT(*)FROM Course);

– 11、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT * FROM Student WHERE s_id IN(
	SELECT  s.`s_id` FROM Score s WHERE s.`c_id` IN(SELECT s1.`c_id`FROM Score s1
	 WHERE s1.`s_id` = '01')
);

– 12、查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT * FROM  student 
	WHERE s_id IN 
	(SELECT s_id FROM score WHERE s_id!='01' GROUP BY s_id HAVING COUNT(c_id)=(SELECT COUNT(c_id) 
	FROM score WHERE s_id='01'));

– 13、查询没学过"张三"老师讲授的任一门课程的学生姓名

SELECT stu.s_name FROM Student stu WHERE stu.`s_id` NOT IN (
	SELECT s_id FROM Score WHERE c_id = (SELECT c_id FROM Course WHERE t_id =(
	SELECT t_id FROM Teacher WHERE t_name ='张三'
	))
);

– 14、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT a.s_id,a.s_name,ROUND(AVG(b.s_score),2)AS avg_sc FROM 
	student a 
	LEFT JOIN score b ON a.s_id = b.s_id
	WHERE a.s_id IN(
			SELECT s_id FROM score WHERE s_score<60 GROUP BY  s_id HAVING COUNT(c_id)>=2)
	GROUP BY a.s_id,a.s_name;

– 15、检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT stu.* ,sc.`c_id`,sc.`s_score` FROM Student stu ,Score sc
	WHERE stu.`s_id` = sc.`s_id` 
	AND sc.`c_id` = '01'
	AND sc.`s_score` < 60 
	ORDER BY sc.`s_score` DESC;

– 16、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT sc.`s_id`,
(SELECT s_score FROM Score WHERE s_id = sc.`s_id`AND c_id ='01')AS 语文,
(SELECT s_score FROM Score WHERE s_id = sc.`s_id`AND c_id ='02')AS 数学,
(SELECT s_score FROM Score WHERE s_id = sc.`s_id`AND c_id ='03')AS 英语,
SUM(s_score) AS 平均分 FROM Score sc GROUP BY sc.`s_id` ORDER BY 平均分 DESC;

– 17、查询不同老师所教不同课程平均分从高到低显示

SELECT c.`t_id`,t.`t_id`,c.`c_name`,AVG(s_score)AS 平均分 FROM Course c
LEFT JOIN Score s ON c.`c_id` = s.`c_id` 
LEFT JOIN Teacher t ON t.`t_id` = c.`t_id`
GROUP BY c.`c_id`ORDER BY 平均分 DESC;

– 18、查询每门课程被选修的学生数

SELECT c_id,COUNT(s_id)FROM Score GROUP BY c_id;

– 19、查询出只有两门课程的全部学生的学号和姓名

SELECT s_id,s_name FROM Student WHERE s_id IN(
SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(c_id)=2
);

– 20、查询男生、女生人数

SELECT s_sex ,COUNT(s_sex)FROM Student GROUP BY s_sex ;

– 21、查询名字中含有"风"字的学生信息

SELECT * FROM Student WHERE s_name LIKE '%风%';

–22、查询同名同性学生名单,并统计同名人数

SELECT a.`s_name`,a.`s_sex`,COUNT(*)  FROM Student a
JOIN Student b ON a.`s_id`!=b.`s_id`AND a.`s_name`=b.`s_name`AND a.`s_sex`=b.`s_sex`
GROUP BY a.`s_name`,a.`s_sex`;

– 23、查询1990年出生的学生名单

SELECT s_name FROM Student WHERE s_birth LIKE'1990%';

– 24、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

SELECT c_id ,AVG(s_score) AS avg_sc FROM Score GROUP BY c_id ORDER BY avg_sc DESC,c_id ASC;

– 25、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

SELECT sc.`s_id`,stu.`s_name`,AVG(sc.`s_score`) AS avg_sc FROM Score sc
LEFT JOIN Student stu ON stu.`s_id` = sc.`s_id` GROUP BY s_id HAVING avg_sc>=85;

– 26、查询课程名称为"数学",且分数低于60的学生姓名和分数

SELECT stu.`s_name` ,sc.`s_score` FROM Student stu JOIN Score sc ON sc.`s_id`=stu.`s_id`
WHERE sc.`c_id` = (SELECT c_id FROM Course WHERE c_name ='数学')AND sc.`s_score`<60;

– 27、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

SELECT stu.`s_name` ,cou.`c_name`,sc.`s_score`FROM Student stu LEFT JOIN Score sc 
ON sc.`s_id` = stu.`s_id`LEFT JOIN Course cou ON cou.`c_id` = sc.`c_id`
WHERE sc.`s_score`>=70;

– 28、查询不及格的课程

SELECT sc.`s_id`,sc.`c_id`,c.`c_name` FROM Score sc LEFT JOIN Course c ON sc.`c_id` = c.`c_id`
WHERE sc.`s_score`<60;

– 29、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;

SELECT sc.`s_id`,stu.`s_name`FROM Score sc 
LEFT JOIN Student stu ON stu.`s_id` = sc.`s_id`
WHERE sc.`c_id`= '01'AND sc.`s_score`>80;

– 30、求每门课的学生人数

SELECT sc.`c_id`,COUNT(*) FROM Score sc GROUP BY c_id;

– 31、查询不同课程成绩相同的学生编号

SELECT DISTINCT b.`c_id`,b.`s_id`,b.`s_score` FROM Score a,Score b WHERE a.`c_id`!=b.`c_id`AND a.`s_score` = b.`s_score`;

– 32、查询每门成绩最好的前两名

SELECT * FROM score a WHERE 
(SELECT COUNT(1)FROM Score b WHERE b.`c_id`= a.`c_id`AND b.`s_score`>=a.`s_score`)
<=2 ORDER BY a.`c_id`;

– 33、统计每门课程的学生选修人数(超过5人的课程才统计),要求输出课程奥和选修人数,查询

结果按人数的降序排列,若人数相同,按课程序号排列
SELECT c_id ,COUNT(*) AS num FROM Score GROUP BY c_id HAVING num >5 ORDER BY num ,c_id ASC; 

– 34、检索至少选修两门课程的学生号

SELECT s_id ,COUNT(*) AS num FROM Score GROUP BY s_id HAVING num>2;

– 35、查询选修热闹全部课程的学生信息

SELECT * FROM Student WHERE s_id IN(
SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(*)= (SELECT COUNT(*) FROM Course)
);

你可能感兴趣的:(mysql)