本sql语句练习题目取自:https://blog.csdn.net/fashion2014/article/details/78826299
SQL语句均为自写,今后会持续发布SQL练习,用于自己记小笔记,练习时建议别看答案。
数据表:
-- 建表
-- 学生表
CREATE TABLE `Student` (
`s_id` VARCHAR ( 20 ) COMMENT '学生编号',
`s_name` VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT '学生姓名',
`s_birth` VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT '出生年月',
`s_sex` VARCHAR ( 10 ) NOT NULL DEFAULT '' COMMENT '学生性别',
PRIMARY KEY ( `s_id` )
) COMMENT = '学生表';
-- 课程表
CREATE TABLE `Course` (
`c_id` VARCHAR ( 20 ) COMMENT '课程编号',
`c_name` VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT '课程名称',
`t_id` VARCHAR ( 20 ) NOT NULL COMMENT '教师编号',
PRIMARY KEY ( `c_id` )
) COMMENT = '课程表';
-- 教师表
CREATE TABLE `Teacher` ( `t_id` VARCHAR ( 20 ) COMMENT '教师编号', `t_name` VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT '教师姓名', PRIMARY KEY ( `t_id` ) ) COMMENT = '教师表';
-- 成绩表
CREATE TABLE `Score` (
`s_id` VARCHAR ( 20 ) COMMENT '学生编号',
`c_id` VARCHAR ( 20 ) COMMENT '课程编号',
`s_score` INT ( 3 ) COMMENT '分数',
PRIMARY KEY ( `s_id`, `c_id` )
) COMMENT = '成绩表';
测试数据:
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"课程成绩高的学生的信息及课程分数
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
-- 6、查询"李"姓老师的数量
-- 7、查询学过"张三"老师授课的同学的信息
-- 8、查询没学过"张三"老师授课的同学的信息
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
-- 11、查询没有学全所有课程的同学的信息
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
-- 19、按各科成绩进行排序,并显示排名
-- 20、查询学生的总成绩并进行排名
-- 21、查询不同老师所教不同课程平均分从高到低显示
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
-- 24、查询学生平均成绩及其名次
-- 25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的
-- 26、查询每门课程被选修的学生数
-- 27、查询出只有两门课程的全部学生的学号和姓名
-- 28、查询男生、女生人数
-- 29、查询名字中含有"风"字的学生信息
-- 30、查询同名同性学生名单,并统计同名人数
-- 31、查询1990年出生的学生名单
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
-- 35、查询所有学生的课程及分数情况;
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
-- 37、查询不及格的课程
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
-- 39、求每门课程的学生人数
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
-- 42、查询每门功成绩最好的前两名
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数
-- 44、检索至少选修两门课程的学生学号
-- 45、查询选修了全部课程的学生信息
-- 46、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
-- 47、查询本周过生日的学生
-- 48、查询下周过生日的学生
-- 49、查询本月过生日的学生
-- 50、查询下月过生日的学生
题解如下:
-- 1-13题(后面题暂时没写,后面慢慢写)
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
/*
SELECT
Student.s_id AS '学生编号',
Student.s_name AS '学生姓名',
Student.s_birth AS '出生日期',
Student.s_sex AS '性别',
Score_01.c_id AS '课程编号',
Score_01.s_score AS '01课程成绩',
Score_02.c_id AS '课程编号',
Score_02.s_score AS '02课程成绩'
FROM
Student
LEFT JOIN ( SELECT * FROM Score WHERE c_id = '01' ) Score_01 ON Student.s_id = Score_01.s_id
LEFT JOIN ( SELECT * FROM Score WHERE c_id = '02' ) Score_02 ON Score_01.s_id = Score_02.s_id
WHERE
Score_01.s_score > Score_02.s_score
*/
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
/*
SELECT
Student.s_id AS '学生编号',
Student.s_name AS '学生姓名',
Student.s_birth AS '出生日期',
Student.s_sex AS '性别',
Score_01.c_id AS '课程编号',
Score_01.s_score AS '01课程成绩',
Score_02.c_id AS '课程编号',
Score_02.s_score AS '02课程成绩'
FROM
Student
LEFT JOIN ( SELECT * FROM Score WHERE c_id = '01' ) Score_01 ON Student.s_id = Score_01.s_id
LEFT JOIN ( SELECT * FROM Score WHERE c_id = '02' ) Score_02 ON Score_01.s_id = Score_02.s_id
WHERE
Score_01.s_score < Score_02.s_score
*/
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
/*
SELECT
*
FROM
(
SELECT
Student.s_id AS '学生编号',
Student.s_name AS '学生姓名',
AVG( Score.s_score ) AS '平均成绩'
FROM
Student
JOIN Score ON Student.s_id = Score.s_id
GROUP BY
Student.s_id
) avgScore
WHERE
平均成绩 > 60
*/
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
/*
SELECT
*
FROM
(
SELECT
Student.s_id AS '学生编号',
Student.s_name AS '学生姓名',
AVG( Score.s_score ) AS '平均成绩'
FROM
Student
LEFT JOIN Score ON Student.s_id = Score.s_id
GROUP BY
Student.s_id
) avgScore
WHERE
平均成绩 < 60 UNION
SELECT
Student.s_id,
Student.s_name,
0 AS s_score
FROM
Student
WHERE
Student.s_id NOT IN ( SELECT DISTINCT s_id FROM Score )
*/
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
/*
SELECT
Student.s_id AS '学生编号',
Student.s_name AS '学生姓名',
count( Score.c_id ) AS '选课总数',
sum( Score.s_score )
FROM
Student
LEFT JOIN Score ON Student.s_id = Score.s_id
GROUP BY
student.s_id,
student.s_name
*/
-- 6、查询"李"姓老师的数量
/*
SELECT
COUNT( * )
FROM
Teacher
WHERE
Teacher.t_name LIKE '李%'
*/
-- 7、查询学过"张三"老师授课的同学的信息
/*第一种解决方法
SELECT
Student.s_id as '学生编号',Student.s_name as '学生姓名',Student.s_birth as '学生生日',Student.s_sex as '学生性别'
FROM
Student
JOIN (
SELECT
Score.s_id
FROM
Score
JOIN ( SELECT Course.c_id FROM Course JOIN Teacher ON Course.t_id = Teacher.t_id WHERE Teacher.t_name = '张三' ) Cid ON Score.c_id = Cid.c_id
WHERE
Score.c_id = Cid.c_id
) Sid ON Student.s_id = Sid.s_id
*/
/*第二种解决方法
SELECT
stu.s_id as '学生编号',stu.s_name as '学生姓名',stu.s_birth as '学生生日',stu.s_sex as '学生性别'
FROM
student stu
JOIN score sco ON stu.s_id = sco.s_id
WHERE
sco.c_id IN ( SELECT c_id FROM course WHERE t_id = ( SELECT t_id FROM teacher WHERE t_name = '张三' ) );
*/
-- 8、查询没学过"张三"老师授课的同学的信息
/*
SELECT
s_id AS '学生编号',
s_name AS '学生姓名',
s_birth AS '学生生日',
s_sex AS '学生性别'
FROM
Student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
Score
WHERE
Score.c_id = ( SELECT c_id FROM Course WHERE t_id = ( SELECT t_id FROM Teacher WHERE t_name = '张三' ) )
)
*/
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
/*
SELECT
s_id AS '学生编号',
s_name AS '学生姓名',
s_birth AS '出生日期',
s_sex AS '性别'
FROM
student
WHERE
student.s_id IN (
SELECT
sc1.s_id
FROM
( SELECT * FROM Score WHERE c_id = 01 ) sc1
JOIN ( SELECT * FROM Score WHERE c_id = 02 ) sc2 ON sc1.s_id = sc2.s_id
)
*/
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
/*
SELECT
s_id AS '学生编号',
s_name AS '学生姓名',
s_birth AS '出生日期',
s_sex AS '性别'
FROM
Student
WHERE
s_id IN ( SELECT s_id FROM Score WHERE s_id NOT IN ( SELECT s_id FROM Score WHERE c_id = 02 ) AND c_id = 01 )
*/
-- 11、查询没有学全所有课程的同学的信息
/*解决方法1:找出有三门课程的同学然后排除掉
SELECT
s_id AS '学生编号',
s_name AS '学生姓名',
s_birth AS '出生日期',
s_sex AS '性别'
FROM
student
WHERE
s_id NOT IN (
SELECT
sc1.s_id
FROM
( SELECT * FROM score WHERE c_id = 01 ) sc1
JOIN ( SELECT * FROM score WHERE c_id = 02 ) sc2 ON sc2.s_id = sc1.s_id
JOIN ( SELECT * FROM score WHERE c_id = 03 ) sc3 ON sc2.s_id = sc3.s_id
)
*/
/*解决方法2:首先查出课程总数,然后将课程数为总课程数的筛选出来排除掉
SELECT
*
FROM
student
WHERE
s_id NOT IN ( SELECT s_id FROM score t1 GROUP BY s_id HAVING count( * ) = ( SELECT count( DISTINCT c_id ) FROM course ) )
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
/*
SELECT
s_id AS '学生编号',
s_name AS '学生姓名',
s_birth AS '出生日期',
s_sex AS '性别'
FROM
Student
WHERE
s_id IN ( SELECT DISTINCT s_id FROM Score WHERE c_id IN ( SELECT c_id FROM Score WHERE s_id = '01' ) )
*/
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
/* 解决方案:首先找出学过的课程数跟01号学生学过的课程数相同的学生,然后第二大步(首先查找出01学生学过的课程,再过滤01同学学过的课程得到01同学没学过的课程,
然后将学过01同学没学过的课程的同学剔除掉)
SELECT
*
FROM
Student
WHERE
s_id IN (
SELECT
s_id
FROM
score
GROUP BY
s_id
HAVING
count( s_id ) = ( SELECT count( c_id ) FROM score WHERE s_id = '01' )
)
AND s_id NOT IN (
SELECT DISTINCT
s_id
FROM
score
WHERE
c_id IN ( SELECT c_id FROM score WHERE c_id NOT IN ( SELECT c_id FROM Score WHERE s_id = '01' ) GROUP BY s_id )
)
*/
*/
本节仅1-13题题解