SQL 50题

建表语句

--学生表
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);

50题

-- 1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点
SELECT
	st.s_id
FROM Student st
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id = '01') a ON st.s_id = a.s_id
INNER JOIN (SELECT s_id, s_score FROM Score WHERE c_id = '02') b ON a.s_id = b.s_id
WHERE a.s_score > b.s_score

-- 2、查询平均成绩大于60分的学生的学号和平均成绩(简单,第二道重点)
SELECT
	s_id,
	avg(s_score)
FROM Score
GROUP BY s_id
HAVING avg(s_score) > 60

-- 3、查询所有学生的学号、姓名、选课数、总成绩(不重要)
SELECT
	st.s_id, st.s_name,
	count(sc.c_id) 'num',
	sum(sc.s_score) 'sum'
FROM Student st 
LEFT JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id

-- 4、查询姓“猴”的老师的个数(不重要)
SELECT
	count(t_id)
FROM Teacher
WHERE t_name like '猴%'

-- 5、查询没学过“张三”老师课的学生的学号、姓名(重点)
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)

-- 6、查询学过“张三”老师所教的所有课的同学的学号、姓名(重点)
SELECT s_id, s_name
FROM Student
WHERE s_id in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)

-- 7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名(重点)
SELECT st.*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '01'
AND st.s_id in (SELECT s_id FROM Score WHERE c_id = '02')

-- 8、查询课程编号为“02”的总成绩(不重点)
SELECT sum(s_score) 'total'
FROM Score
WHERE c_id = '02'

-- 9、查询所有课程成绩小于60分的学生的学号、姓名
SELECT *
FROM Student 
WHERE s_id not in (
SELECT s_id
FROM Score
WHERE s_score > 60
)

-- 10.查询没有学全所有课的学生的学号、姓名(重点)
SELECT st.*
FROM Student st
LEFT JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(sc.c_id) < (SELECT count(c_id) FROM Course) 

-- 11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名(重点)
SELECT
	DISTINCT st.*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id in (SELECT c_id FROM Score WHERE s_id = '01')
AND st.s_id <> '01'

-- 12.查询和“01”号同学所学课程完全相同的其他同学的学号(重点)
SELECT s_id
FROM Score
WHERE s_id <> '01'
GROUP BY s_id
HAVING GROUP_CONCAT(c_id) = (SELECT GROUP_CONCAT(c_id) FROM Score WHERE s_id = '01')


-- 13、查询没学过"张三"老师讲授的任一门课程的学生姓名 和47题一样(重点,能做出来)
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(重点)
SELECT
	st.s_id, st.s_name,
	avg(sc.s_score)
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(IF(sc.s_score < 60, 1, null)) >= 2


-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息(和34题重复,不重点)
SELECT
	*
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '01' AND sc.s_score < 60
ORDER BY sc.s_score desc

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(重重点与35一样)
SELECT
	s_id,
	max(case when c_id = '01' then s_score else null end) '01',
	max(case when c_id = '02' then s_score else null end) '02',
	max(case when c_id = '03' then s_score else null end) '03',
	avg(s_score)
FROM Score
GROUP BY s_id
ORDER BY avg(s_score) desc

-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 (超级重点)
SELECT
	sc.c_id, co.c_name,
	max(sc.s_score) 'max',
	min(sc.s_score) 'min',
	avg(sc.s_score) 'avg',
	avg(case when sc.s_score >= 60 then 1 else 0 end) '及格率',
	avg(case when sc.s_score >= 70 then 1 else 0 end) '中等率',
	avg(case when sc.s_score >= 80 then 1 else 0 end) '优良率',
	avg(case when sc.s_score >= 90 then 1 else 0 end) '优秀率'
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
GROUP BY sc.c_id

-- 19、按各科成绩进行排序,并显示排名(重点row_number)
SELECT
 *,
 ROW_NUMBER() over(ORDER BY s_score desc) 'r'
FROM Score

-- 20、查询学生的总成绩并进行排名(不重点)
SELECT
	s_id,
	sum(s_score)
FROM Score
GROUP BY s_id
ORDER BY sum(s_score) desc

-- 21 、查询不同老师所教不同课程平均分从高到低显示(不重点)
SELECT
	sc.c_id,
	te.t_name,
	avg(s_score)
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
GROUP BY sc.c_id
ORDER BY avg(sc.s_score) desc

-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(重要 25类似)
SELECT
	st.*,
	a.s_score,
	a.r
FROM Student st
INNER JOIN (
SELECT
	s_id, s_score,
	ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score) a ON st.s_id = a.s_id AND r in (2, 3)

-- 23、使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称(重点和18题类似)
SELECT
	sc.c_id, co.c_name,
	sum(case when sc.s_score >= 0 and sc.s_score < 60 then 1 else 0 end) '[<60]',
	sum(case when sc.s_score >= 60 and sc.s_score < 70 then 1 else 0 end) '[70-60]',
	sum(case when sc.s_score >= 70 and sc.s_score < 85 then 1 else 0 end) '[85-70]',
	sum(case when sc.s_score >= 85 and sc.s_score <= 100 then 1 else 0 end) '[100-85]'
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
GROUP BY sc.c_id

-- 24、查询学生平均成绩及其名次(同19题,重点)
SELECT
 s_id,
 avg(s_score),
 ROW_NUMBER() over(ORDER BY avg(s_score) desc) 'r'
FROM Score
GROUP BY s_id 

-- 25、查询各科成绩前三名的记录(不考虑成绩并列情况)(重点 与22题类似)
SELECT
	c_id,
	max(case when a.r = 1 then a.s_score else null end) 'NO.1',
	max(case when a.r = 2 then a.s_score else null end) 'NO.2',
	max(case when a.r = 3 then a.s_score else null end) 'NO.3'
FROM(
SELECT
	*,
	ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score) a
GROUP BY c_id


-- 26、查询每门课程被选修的学生数(不重点)
SELECT
	count(distinct s_id) 'num'
FROM Score
GROUP BY c_id

-- 27、 查询出只有两门课程的全部学生的学号和姓名(不重点)
SELECT
	st.s_id, st.s_name
FROM Student st 
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING count(sc.c_id) = 2

-- 28、查询男生、女生人数(不重点)
SELECT
	s_sex,
	count(s_id)
FROM Student
GROUP BY s_sex

-- 29 查询名字中含有"风"字的学生信息(不重点)
SELECT	*
FROM Student
WHERE s_name like '%风%'

-- 31、查询1990年出生的学生名单(重点year)
SELECT *
FROM Student
WHERE YEAR(s_birth) = '1990'

-- 32、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩(不重要)
SELECT
	st.s_id, st.s_name,
	avg(sc.s_score) 'avg'
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
GROUP BY st.s_id
HAVING avg(sc.s_score) >= 85

-- 33、查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列(不重要)
SELECT
	c_id, avg(s_score)
FROM Score
GROUP BY c_id
ORDER BY avg(s_score), c_id desc

-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数(不重点)
SELECT st.s_name, sc.s_score
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
INNER JOIN Course co ON sc.c_id = co.c_id
WHERE co.c_name = '数学' AND sc.s_score < 60

-- 35、查询所有学生的课程及分数情况(重点)
SELECT
	s_id,
	max(case when c_id = '01' then s_score else null end) '01',
	max(case when c_id = '02' then s_score else null end) '02',
	max(case when c_id = '03' then s_score else null end) '03'
FROM Score
GROUP BY s_id 

-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数(重点)
SELECT
	st.s_name, co.c_name, sc.s_score
FROM Student st
INNER JOIN Score sc on st.s_id = sc.s_id
INNER JOIN Course co on sc.c_id = co.c_id
WHERE sc.s_score > 70

-- 37、查询不及格的课程并按课程号从大到小排列(不重点)
SELECT
	c_id,
	s_score
FROM Score
WHERE s_score < 60
ORDER BY c_id desc

-- 38、查询课程编号为03且课程成绩在80分以上的学生的学号和姓名(不重要)
SELECT
	st.s_id, st.s_name
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
WHERE sc.c_id = '03' AND sc.s_score > 80

-- 39、求每门课程的学生人数(不重要)
SELECT
	c_id,
	count(s_id)
FROM Score
GROUP BY c_id

-- 40、查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩(重要top)
SELECT
	st.s_name, sc.s_score
FROM Student st
INNER JOIN Score sc ON st.s_id = sc.s_id
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
ORDER BY sc.s_score desc
limit 1

-- 41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (重点)
SELECT
	s1.s_id, s1.c_id, s2.c_id, s1.s_score
FROM Score s1
INNER JOIN Score s2 ON s1.s_id = s2.s_id
AND s1.c_id <> s2.c_id AND s1.s_score = s2.s_score

-- 42、查询每门功成绩最好的前两名(同22和25题)
SELECT *
FROM
(
SELECT
	*,
	ROW_NUMBER() over(PARTITION BY c_id ORDER BY s_score desc) 'r'
FROM Score)a
WHERE a.r in (1,2)

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列(不重要)
SELECT
	c_id, count(s_id)
FROM Score
GROUP BY c_id
HAVING count(s_id) > 5
ORDER BY count(s_id), c_id

-- 44、检索至少选修两门课程的学生学号(不重要)
SELECT s_id
FROM Score
GROUP BY s_id
HAVING count(c_id) >= 2

-- 45、 查询选修了全部课程的学生信息(重点划红线地方)
SELECT
	st.*
FROM Score sc
INNER JOIN Student st on st.s_id = sc.s_id
GROUP BY sc.s_id
HAVING count(c_id) = (SELECT count(1) FROM Course)

-- 47、查询没学过“张三”老师讲授的任一门课程的学生姓名
SELECT s_id, s_name
FROM Student
WHERE s_id not in (
SELECT sc.s_id
FROM Score sc
INNER JOIN Course co ON sc.c_id = co.c_id
INNER JOIN Teacher te ON co.t_id = te.t_id
WHERE te.t_name = '张三'
)

-- 48、查询两门以上不及格课程的同学的学号及其平均成绩
SELECT
	s_id, avg(s_score)
FROM Score
GROUP BY s_id
HAVING count(if(s_score < 60, 1, null)) >= 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值