sql语句练习50题
- 1 表名和字段
- 2 测试数据
- 2.1 学生表
- 2.2 课程表
- 2.3 教师表
- 2.4 成绩表
- 3 练习题和sql语句
- 3.1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
- 3.1.1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
- 3.1.2 查询同时存在“01”课程和“02”课程的情况
- 3.1.3 查询存在“01”课程但可能不存在“02”课程的情况(不存在时显示为NULL)
- 3.1.4 查询不存在“01”课程但存在“02”课程的情况
- 3.2 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
- 3.3 查询在score表存在成绩的学生信息
- 3.4 查询所有学生的编号、学生姓名、选课总数、所有课程的总成绩(没有成绩的显示为null)
- 3.4(附)查询有成绩的学生
- 3.5 查询“李”姓老师的数量
- 3.6 查询学过“张三”老师授课的同学的信息
- 3.7 查询没有学全所有课程的同学的信息
- 3.8 查询至少有一门课与学号为“01”的同学所学相同的学生的信息
- 3.9 查询和“01”号的同学学习的课程完全相同的其他同学的信息(GROUP_CONCAT)
- 3.10 查询没有学过“张三”老师任何一门课程的学生姓名
- 3.11 查询两门及以上不及格课程的同学的学号,姓名及其平均成绩
- 3.12 检索“01”课程分数小于60,按分数降序排列的学生信息
- 3.13 按平均成绩从高到低显示所有学生的所有课程成绩以及平均成绩
- 3.14 查询各科成绩最高分、最低分和平均分
- 3.15 按各科成绩进行排序,并显示排名,score重复时也继续排名(@i用法)(to do)
- 3.16 (to do)
- 3.17 统计各科成绩分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占的百分比
- 3.18 查询各科成绩前三名的记录(to do)
- 3.19 查询每门课程被选修的学生数
- 3.20 查询出只选修两门课程的学生学号和姓名
- 3.21 查询男生人数、女生人数
- 3.22 查询名字中含有“风”字的学生信息
- 3.23 查询同名同性学生名单,并统计同名同性人数(to do)
- 3.24 查询1990年出生的学生名单
- 3.25 查询每门课程的平均成绩,结果按平均成绩降序排序,平均成绩相同时,按课程编号升序排序
- 3.26 查询平均成绩大于85的所有学生的学号、姓名和平均成绩
- 3.27 查询课程名称为“数学”,且分数低于60的学生姓名和分数
- 3.28 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
- 3.29 查询任意一门课程成绩在70分以上的同学姓名、课程名称和分数
- 3.30 查询存在不及格的课程
- 3.31 查询课程编号为“01”且课程成绩在80分以上的学生学号和姓名
- 3.32 求每门课程的学生人数
- 3.33 假设成绩不重复,查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩(limit)
- 3.34 假设成绩有重复的情况下,查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩(to do)
- 3.35 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
- 3.36 查询每门课程成绩最好的前两名(to do)
- 3.37 统计每门课程的选修人数,超过五人的课程才进行统计
- 3.38 检索至少选修两门课程的学生学号
- 3.39 查询选修了全部课程的学生的信息
- 3.40 查询各学生的年龄,只按年份来
- 3.41 按照出生日期来算各学生的年龄
- 3.42 查询本周过生日的同学(to do)
- 3.43 查询下周过生日的同学(to do)
- 3.44 查询本月过生日的同学(to do)
- 3.45 查询下月过生日的同学(to do)
1 表名和字段
- 学生表
student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 - 课程表
course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号 - 教师表
teacher(t_id,t_name) --教师编号,教师姓名 - 成绩表
score(s_id,c_id,s_score) --学生编号,课程编号,分数
2 测试数据
2.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`)
);
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' , '女');
2.2 课程表
CREATE TABLE `course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '03');
2.3 教师表
CREATE TABLE `teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');
2.4 成绩表
CREATE TABLE `score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
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);
3 练习题和sql语句
3.1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
3.1.1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数
JOIN
SELECT
st.*,
sc1.s_score AS '语文',
sc2.s_score AS '数学'
FROM
score sc1
LEFT JOIN student st ON sc1.s_id = st.s_id
INNER JOIN score sc2 ON sc1.s_id = sc2.s_id
AND sc1.c_id = '01'
AND sc2.c_id = '02'
WHERE
sc1.s_score > sc2.s_score;
连表
EXPLAIN SELECT
st.*,
sc1.s_score AS '语文',
sc2.s_score AS '数学'
FROM
student st,
score sc1,
score sc2
WHERE
st.s_id = sc1.s_id
AND st.s_id = sc1.s_id
AND st.s_id = sc2.s_id
AND sc1.c_id = '01'
AND sc2.c_id = '02'
AND sc1.s_score > sc2.s_score;
3.1.2 查询同时存在“01”课程和“02”课程的情况
INNER JOIN
SELECT
*
FROM
score sc1
INNER JOIN score sc2 ON sc1.s_id = sc2.s_id
AND sc1.c_id = '01'
AND sc2.c_id = '02';
3.1.3 查询存在“01”课程但可能不存在“02”课程的情况(不存在时显示为NULL)
SELECT
*
FROM
( SELECT * FROM score WHERE c_id = '01' ) sc1
LEFT JOIN ( SELECT * FROM score WHERE c_id = '02' ) sc2 ON sc1.s_id = sc2.s_id;
SELECT
*
FROM
score sc1
LEFT JOIN score sc2 ON sc1.s_id = sc2.s_id
AND sc2.c_id = '02'
WHERE
sc1.c_id = '01';
3.1.4 查询不存在“01”课程但存在“02”课程的情况
SELECT
*
FROM
score
WHERE
s_id NOT IN ( SELECT s_id FROM score WHERE c_id = '01' )
AND c_id = '02';
3.2 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
st.s_id,
st.s_name,
avg.s_avg
FROM
student st
INNER JOIN ( SELECT s_id, AVG( s_score ) AS s_avg FROM score GROUP BY s_id HAVING AVG( s_score ) >= 60 ) avg ON st.s_id = avg.s_id;
SELECT
st.s_id,
st.s_name,
AVG( s_score ) AS s_avg
FROM
student st
INNER JOIN score sc ON st.s_id = sc.s_id
GROUP BY
st.s_id
HAVING
AVG( s_score ) >= 60;
3.3 查询在score表存在成绩的学生信息
思路:score存在的必然会有学生信息,因此score LEFT JOIN student
SELECT
st.*
FROM
score sc
LEFT JOIN student st ON sc.s_id = st.s_id
GROUP BY
sc.s_id;
SELECT DISTINCT
st.*
FROM
student st
INNER JOIN score sc ON st.s_id = sc.s_id;
3.4 查询所有学生的编号、学生姓名、选课总数、所有课程的总成绩(没有成绩的显示为null)
思路:
- 确定使用表范围:学生表、成绩表
- 获取每个学生的选课总数和所有课程的总成绩,在成绩表中对s_id进行聚合
- 与学生表进行拼接,由于没有成绩显示为null,因此学生表作为主表,左连接成绩表
SELECT
st.s_id,
st.s_name,
sc.c_count,
sc.sc_sum
FROM
student st
LEFT JOIN ( SELECT s_id, COUNT( * ) AS c_count, SUM( s_score ) AS sc_sum FROM score GROUP BY s_id ) sc ON st.s_id = sc.s_id;
或者先拼接,再聚合分组筛选
SELECT
st.s_id,
st.s_name,
COUNT( c_id ) AS c_count,
SUM( s_score ) AS sc_sum
FROM
student st
LEFT JOIN score sc ON st.s_id = sc.s_id
GROUP BY
st.s_id;
3.4(附)查询有成绩的学生
思路
- 确定表范围:学生表、成绩表
- 筛选出由课程的s_id
- 利用s_id对学生表进行筛选
SELECT *
FROM student
WHERE s_id IN (
SELECT DISTINCT s_id
FROM score
);
SELECT *
FROM student
WHERE s_id IN(
SELECT s_id
FROM score
GROUP BY s_id
);
SELECT *
FROM student
WHERE EXISTS(
SELECT *
from score
WHERE student.s_id = score.s_id
);
3.5 查询“李”姓老师的数量
思路:
- 确定表范围:教师表
- 筛选“李”姓老师
- count
SELECT
COUNT( * )
FROM
teacher
WHERE
t_name LIKE '李%';
3.6 查询学过“张三”老师授课的同学的信息
SELECT
a.*
FROM
student a
INNER JOIN (
SELECT
a.s_id
FROM
score a
INNER JOIN ( SELECT c_id FROM course a INNER JOIN teacher b ON a.t_id = b.t_id AND b.t_name = '张三' ) b ON a.c_id = b.c_id
) b ON a.s_id = b.s_id;
SELECT
a.*
FROM
student a,
score b,
course c,
teacher d
WHERE
a.s_id = b.s_id
AND b.c_id = c.c_id
AND c.t_id = d.t_id
AND d.t_name = '张三';
3.7 查询没有学全所有课程的同学的信息
思路:先查询课程数。
SELECT
st.*,
COUNT( * ) AS c_count
FROM
student st
LEFT JOIN score sc ON st.s_id = sc.s_id
GROUP BY
st.s_id
HAVING
COUNT( * ) < ( SELECT COUNT( c_id ) FROM course );
3.8 查询至少有一门课与学号为“01”的同学所学相同的学生的信息
SELECT
st.*
FROM
student st
INNER JOIN (
SELECT
*
FROM
score
WHERE
c_id IN ( SELECT sc.c_id FROM student st INNER JOIN score sc ON st.s_id = '01' AND sc.s_id = '01' )
GROUP BY
s_id
) sc ON st.s_id = sc.s_id;
3.9 查询和“01”号的同学学习的课程完全相同的其他同学的信息(GROUP_CONCAT)
SELECT
a.s_id,
a.s_name,
a.s_birth,
a.s_sex
FROM
(
SELECT
a.*,
b.courses
FROM
student a
INNER JOIN ( SELECT s_id, GROUP_CONCAT( c_id ORDER BY c_id ASC ) AS courses FROM score GROUP BY s_id ) b
WHERE
a.s_id = b.s_id
) a
INNER JOIN ( SELECT GROUP_CONCAT( c_id ORDER BY c_id ASC ) AS courses FROM score WHERE s_id = '01' ) b ON a.courses = b.courses;
3.10 查询没有学过“张三”老师任何一门课程的学生姓名
SELECT
student.s_name
FROM
student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
score
WHERE
c_id IN ( SELECT c_id FROM course a INNER JOIN teacher b ON a.t_id = b.t_id AND b.t_name = '张三' )
);
3.11 查询两门及以上不及格课程的同学的学号,姓名及其平均成绩
SELECT
a.s_id,
a.s_name,
AVG( b.s_score ) AS s_avg
FROM
(
SELECT
a.*
FROM
student a
INNER JOIN score b ON a.s_id = b.s_id
WHERE
b.s_score < 60 GROUP BY a.s_id HAVING COUNT( * ) >= 2
) a
INNER JOIN score b
WHERE
a.s_id = b.s_id
GROUP BY
a.s_id
SELECT
b.s_id,
b.s_name,
AVG( a.s_score ) AS s_avg
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
INNER JOIN ( SELECT s_id FROM score WHERE s_score < 60 GROUP BY s_id HAVING COUNT( c_id ) >= 2 ) c ON a.s_id = c.s_id
GROUP BY
a.s_id;
3.12 检索“01”课程分数小于60,按分数降序排列的学生信息
SELECT
b.*,
a.s_score
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
WHERE
a.s_score < 60
AND a.c_id = '01'
ORDER BY
a.s_score DESC
3.13 按平均成绩从高到低显示所有学生的所有课程成绩以及平均成绩
SELECT
a.*,
b.s_avg
FROM
( SELECT a.*, b.s_score FROM student a LEFT JOIN score b ON a.s_id = b.s_id ) a
LEFT JOIN ( SELECT s_id, AVG( s_score ) AS s_avg FROM score GROUP BY s_id ) b ON a.s_id = b.s_id
ORDER BY
b.s_avg DESC;
3.14 查询各科成绩最高分、最低分和平均分
以如下形式显示:课程id,课程name,最高分、最低分,平均分、及格率、中等率、优良率、优秀率
及格为:>=60
中等为:70-80
优良为:80-90
优秀为:90-100
要求输出课程号和选课人数,按课程号升序排列,查询结果按人数降序,若人数相同按课程号升序排列
SELECT
c_id,
MAX( s_score ) AS '最高分',
MIN( s_score ) AS '最低分',
AVG( s_score ) AS '平均分',
COUNT( * ) AS '选课人数',
SUM( CASE WHEN s_score >= 60 THEN 1 ELSE 0 END ) / COUNT( * ) AS '及格率',
SUM( CASE WHEN s_score >= 70 AND s_score < 80 THEN 1 ELSE 0 END ) / COUNT( * ) AS '中等率',
SUM( CASE WHEN s_score >= 80 AND s_score < 90 THEN 1 ELSE 0 END ) / COUNT( * ) AS '优良率',
SUM( CASE WHEN s_score >= 90 THEN 1 ELSE 0 END ) / COUNT( * ) AS '优秀率'
FROM
score
GROUP BY
c_id
ORDER BY
COUNT( * ) DESC,
c_id ASC;
3.15 按各科成绩进行排序,并显示排名,score重复时也继续排名(@i用法)(to do)
3.16 (to do)
3.17 统计各科成绩分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占的百分比
SELECT
a.c_id,
b.c_name,
SUM(CASE WHEN s_score >85 AND s_score <= 100 THEN 1 ELSE 0 END) AS '[100-85]人数',
CONCAT(SUM(CASE WHEN s_score >85 AND s_score <= 100 THEN 1 ELSE 0 END) / COUNT(*) * 100,'%') AS '[100-85]占比',
SUM(CASE WHEN s_score >70 AND s_score <= 85 THEN 1 ELSE 0 END) AS '[85-70]人数',
CONCAT(SUM(CASE WHEN s_score >70 AND s_score <= 85 THEN 1 ELSE 0 END) / COUNT(*) * 100,'%') AS '[85-70]占比',
SUM(CASE WHEN s_score >60 AND s_score <= 70 THEN 1 ELSE 0 END) AS '[70-60]人数',
CONCAT(SUM(CASE WHEN s_score >60 AND s_score <= 70 THEN 1 ELSE 0 END) / COUNT(*) * 100,'%') AS '[70-60]占比',
SUM(CASE WHEN s_score >=0 AND s_score <= 60 THEN 1 ELSE 0 END) AS '[60-0]人数',
CONCAT(SUM(CASE WHEN s_score >= 0 AND s_score <= 60 THEN 1 ELSE 0 END) / COUNT(*) * 100,'%') AS '[60-0]占比'
FROM
score a
LEFT JOIN course b ON a.c_id = b.c_id
GROUP BY
a.c_id;
3.18 查询各科成绩前三名的记录(to do)
3.19 查询每门课程被选修的学生数
SELECT
c_id,
COUNT( s_id ) AS count
FROM
score
GROUP BY
c_id
3.20 查询出只选修两门课程的学生学号和姓名
SELECT
b.s_id,
b.s_name
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
COUNT( a.c_id ) = 2
3.21 查询男生人数、女生人数
SELECT
s_sex,
COUNT( s_id )
FROM
student
GROUP BY
s_sex;
3.22 查询名字中含有“风”字的学生信息
SELECT
*
FROM
student
WHERE
s_name LIKE '%风%';
3.23 查询同名同性学生名单,并统计同名同性人数(to do)
3.24 查询1990年出生的学生名单
SELECT
*
FROM
student
WHERE
YEAR ( s_birth ) = 1990;
3.25 查询每门课程的平均成绩,结果按平均成绩降序排序,平均成绩相同时,按课程编号升序排序
SELECT
c_id,
AVG( s_score ) AS s_avg
FROM
score
GROUP BY
c_id
ORDER BY
AVG( s_score ) DESC,
c_id ASC;
3.26 查询平均成绩大于85的所有学生的学号、姓名和平均成绩
SELECT
b.s_id,
b.s_name,
AVG( a.s_score ) AS s_avg
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
AVG( a.s_score ) > 85;
3.27 查询课程名称为“数学”,且分数低于60的学生姓名和分数
SELECT
a.s_name,
b.s_score
FROM
student a
INNER JOIN score b ON a.s_id = b.s_id
INNER JOIN ( SELECT c_id FROM course WHERE c_name = '数学' ) c ON b.c_id = c.c_id
WHERE
b.s_score < 60;
3.28 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT
*
FROM
student a
LEFT JOIN score b ON a.s_id = b.s_id;
3.29 查询任意一门课程成绩在70分以上的同学姓名、课程名称和分数
SELECT
a.s_name,
c.c_name,
b.s_score
FROM
student a
INNER JOIN score b ON a.s_id = b.s_id
INNER JOIN course c ON b.c_id = c.c_id
WHERE
b.s_score > 70;
3.30 查询存在不及格的课程
SELECT DISTINCT
c_id
FROM
score
WHERE
s_score < 60
3.31 查询课程编号为“01”且课程成绩在80分以上的学生学号和姓名
SELECT
b.s_id,
b.s_name
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
WHERE
a.c_id = '01'
AND a.s_score >= '80';
3.32 求每门课程的学生人数
SELECT
c_id,
COUNT( s_id ) AS cons
FROM
score
GROUP BY
c_id;
3.33 假设成绩不重复,查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩(limit)
SELECT
d.*,
c.s_score
FROM
teacher a
INNER JOIN course b ON a.t_id = b.t_id
INNER JOIN score c ON b.c_id = c.c_id
INNER JOIN student d ON c.s_id = d.s_id
WHERE
a.t_name = '张三'
ORDER BY
c.s_score DESC
LIMIT 1;
3.34 假设成绩有重复的情况下,查询选修“张三”老师所授课程的学生中,成绩最高的学生信息及其成绩(to do)
3.35 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT
a.*
FROM
score a
INNER JOIN score b ON a.s_id = b.s_id
AND a.c_id != b.c_id
AND a.s_score = b.s_score
GROUP BY
a.s_id,
a.c_id
3.36 查询每门课程成绩最好的前两名(to do)
3.37 统计每门课程的选修人数,超过五人的课程才进行统计
SELECT
c_id,
COUNT( s_id ) AS cons
FROM
score
GROUP BY
c_id
HAVING
COUNT( s_id ) > 5;
3.38 检索至少选修两门课程的学生学号
SELECT
s_id
FROM
score
GROUP BY
s_id
HAVING
COUNT( c_id ) >= 2;
3.39 查询选修了全部课程的学生的信息
SELECT
b.*
FROM
score a
LEFT JOIN student b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
COUNT( * ) = ( SELECT COUNT( * ) FROM course )
3.40 查询各学生的年龄,只按年份来
SELECT
s_name,
YEAR ( CURRENT_DATE ) - YEAR ( s_birth ) AS age
FROM
student
3.41 按照出生日期来算各学生的年龄
SELECT
s_name,
TIMESTAMPDIFF( YEAR, s_birth, CURRENT_TIME ) AS age
FROM
student
3.42 查询本周过生日的同学(to do)
3.43 查询下周过生日的同学(to do)
3.44 查询本月过生日的同学(to do)
3.45 查询下月过生日的同学(to do)