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) –学生编号,课程编号,分数

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  
st.*,
sc.s_score AS '语文',
sc2.s_score AS '数学'
FROM 
student st
 LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
 LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
WHERE sc.s_score>sc2.s_score

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

SELECT  
st.*,
sc.s_score AS '语文',
sc2.s_score AS '数学
FROM 
student st
 LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
 LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
WHERE sc.s_score < sc2.s_score

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

SELECT
st.s_id,
st.s_name,
ROUND (AVG(sc.s_score),2)
FROM  student st 
JOIN score sc ON st.s_id=sc.s_id
GROUP BY st.s_id HAVING AVG(sc.s_score)>=60;


SELECT
st.s_id,
st.s_name,
ROUND(AVG(sc.s_score),2)
FROM  student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING AVG(sc.s_score)>=60;

– 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
– (包括有成绩的和无成绩的)

SELECT st.s_id, st.s_name,CASE WHEN AVG(sc.s_score) IS  NULL  THEN '无' ELSE ROUND (AVG(sc.s_score) ,2) END AS '平均分'
FROM  student st
LEFT JOIN score sc
ON sc.s_id=st.s_id
GROUP BY st.s_id 
HAVING AVG(sc.s_score)<60
OR AVG(sc.s_score) IS NULL

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

SELECT  st.s_id,st.s_name,c,s
FROM student st
LEFT JOIN
(SELECT sc.s_id,COUNT(sc.c_id) c,SUM(sc.s_score)   s 
FROM score sc
GROUP BY 
sc.s_id) d ON st.s_id=d.s_id

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

SELECT 
COUNT(t.t_name)
FROM teacher t
GROUP BY 
t.t_name 
HAVING
t.t_name LIKE '李%'

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

SELECT  st.*
FROM student st
WHERE st.s_id IN
(SELECT sc.s_id FROM score sc WHERE sc.c_id IN 
(SELECT c.c_id FROM course c,teacher t WHERE c.t_id=t.t_id AND t.t_name='张三'))

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

SELECT  st.*
FROM student st
WHERE st.s_id NOT  IN
(SELECT sc.s_id FROM score sc WHERE sc.c_id IN 
(SELECT c.c_id FROM course c,teacher t WHERE c.t_id=t.t_id AND t.t_name='张三'))

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

SELECT st.*
FROM student st
WHERE st.s_id IN(
SELECT sc.s_id  FROM score sc WHERE sc.c_id='01')  AND 
st.s_id IN (
SELECT sc.s_id  FROM score sc WHERE sc.c_id='02' )

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

SELECT st.*
FROM student st
WHERE st.s_id IN(
SELECT sc.s_id  FROM score sc WHERE sc.c_id='01')  AND 
st.s_id NOT IN (
SELECT sc.s_id  FROM score sc WHERE sc.c_id='02' ) 

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

SELECT st.*
FROM student st
LEFT JOIN score sc1 ON sc1.s_id=st.s_id AND sc1.c_id='01'
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
LEFT JOIN score sc3 ON sc3.s_id=st.s_id AND sc3.c_id='03'
WHERE sc1.s_score IS NULL OR sc2.s_score IS NULL OR sc3.s_score IS NULL

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

SELECT st.*
FROM
student st
WHERE st.s_id IN(
SELECT
DISTINCT sc.s_id
FROM score sc
WHERE
sc.c_id IN (
SELECT sc.c_id
FROM score sc
WHERE sc.s_id = '01'))

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

SELECT  st.* 
FROM student st 
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id
HAVING GROUP_CONCAT(sc.c_id) = 
(
SELECT  
GROUP_CONCAT(sc2.c_id) 
FROM student st2
LEFT JOIN score sc2 
ON sc2.s_id=st2.s_id
WHERE st2.s_id ='01'
)

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

SELECT  st.*
FROM student st
WHERE st.s_id NOT  IN
(SELECT sc.s_id FROM score sc WHERE sc.c_id IN 
(SELECT c.c_id FROM course c,teacher t WHERE c.t_id=t.t_id AND t.t_name='张三'))

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

SELECT st.s_id,st.s_name,AVG(sc.s_score)
FROM student st,score sc
WHERE (SELECT COUNT(*) FROM score sc WHERE  sc.s_id=st.s_id AND sc.s_score<60)>=2
GROUP BY 
st.s_id

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

SELECT
st.*,sc.s_score
FROM student st
LEFT JOIN score sc ON sc.c_id='01'
HAVING
sc.s_score<60
ORDER BY
sc.s_score
DESC

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

SELECT st.s_name,SUM(sc.s_score),AVG(sc.s_score)
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY
sc.s_id
ORDER BY
AVG(sc.s_score)

– 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

SELECT sc.c_id AS '课程ID',c.c_name AS '课程name' ,MAX(sc.s_score) AS '最高分',MIN(sc.s_score) AS '最低分',AVG(sc.s_score) AS '平均分',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=60)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '及格率',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=70 AND sc.s_score<80)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '中等率',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=80 AND sc.s_score<90)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '优良率',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=90)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '优秀率'
FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
GROUP BY
sc.c_id

– 19、按各科成绩进行排序

SELECT * FROM(SELECT * FROM score WHERE score.c_id='01' ORDER BY s_score DESC LIMIT 0,100) a
UNION 
SELECT * FROM(SELECT * FROM score WHERE score.c_id='02' ORDER BY s_score DESC LIMIT 0,100) b
UNION 
SELECT * FROM(SELECT * FROM score WHERE score.c_id='03' ORDER BY s_score DESC LIMIT 0,100) c

– 20、查询学生的总成绩并进行排名

SELECT score.s_id,student.s_name,SUM(score.s_score)
FROM score
LEFT JOIN student ON student.s_id=score.s_id
GROUP BY
score.s_id
ORDER BY
SUM(score.s_score)
DESC

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

SELECT teacher.t_id, teacher.t_name , CAST(AVG(score.s_score) AS DECIMAL(18,2)) avg_score
FROM teacher, course, score
WHERE  teacher.t_id= course.t_id AND course.c_id = score.c_id
GROUP BY teacher.t_id
ORDER BY avg_score DESC

– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT * FROM (
SELECT st.*,sc.s_score 
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
ORDER BY
sc.s_score
DESC
LIMIT 1,2
)a
UNION ALL
SELECT * FROM (
SELECT st.*,sc.s_score 
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='02'
ORDER BY
sc.s_score
DESC
LIMIT 1,2
)b
UNION ALL
SELECT * FROM (
SELECT st.*,sc.s_score 
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='03'
ORDER BY
sc.s_score
DESC
LIMIT 1,2
)c

– 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

SELECT COUNT(*),sc.c_id,c.c_name,
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score<60)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '[0-60]',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=60 AND sc.s_score<70)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '[70-60]',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=70 AND sc.s_score<85)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '[85-70]',
CAST((SELECT COUNT(*)FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score>=85 AND sc.s_score<=100)*100.0/(SELECT COUNT(*) FROM score sc WHERE sc.c_id=c.c_id )AS DECIMAL(18,2))AS '[100-85]'
FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
GROUP BY
sc.c_id

– 24、查询学生平均成绩及其名次

SELECT AVG(score.s_score),score.s_id
FROM student,score
WHERE 
student.s_id=score.s_id
GROUP BY score.s_id

– 25、查询各科成绩前三名的记录

SELECT * FROM (
SELECT st.*,sc.s_score,sc.c_id
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
ORDER BY
sc.s_score 
DESC LIMIT 0,3
) a
UNION ALL
SELECT * FROM (
SELECT st.*,sc.s_score,sc.c_id
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='02'
ORDER BY
sc.s_score 
DESC LIMIT 0,3
) b
UNION ALL
SELECT * FROM (
SELECT st.*,sc.s_score,sc.c_id
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='03'
ORDER BY
sc.s_score 
DESC LIMIT 0,3
)c

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

SELECT course.c_name,COUNT(score.s_id)
FROM score
LEFT JOIN course ON course.c_id=score.c_id
GROUP BY
score.c_id

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

SELECT student.s_id,student.s_name
FROM student,score
WHERE
 score.s_id=student.s_id
 GROUP BY
 score.s_id
HAVING
COUNT(score.c_id)=2

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

SELECT st.s_sex ,COUNT(st.s_sex)
FROM student st
GROUP BY
st.s_sex

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

SELECT st.*
FROM student st
WHERE st.s_name LIKE '%风%'

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

SELECT st.s_name,COUNT(st.s_name)
FROM student st
GROUP BY
st.s_name
HAVING
COUNT(st.s_name)>1

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

SELECT st.s_name
FROM student st
WHERE
YEAR(st.s_birth)=1990

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

SELECT score.s_score,score.c_id
FROM score
GROUP BY
score.c_id 
ORDER BY
AVG(score.s_score) DESC,score.c_id ASC

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

SELECT score.s_id,student.s_name,AVG(score.s_score)
FROM student
LEFT JOIN score ON score.s_id=student.s_id
GROUP BY
score.s_id
HAVING
AVG(score.s_score)>=85

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

SELECT student.s_name,score.s_score
FROM student,course,score
WHERE
course.c_name='数学' AND score.s_score<60 AND student.s_id=score.s_id AND score.c_id=course.c_id

– 35、查询所有学生的课程及分数情况;

SELECT student.*,course.c_name,score.s_score
FROM student,score,course
WHERE
score.c_id=course.c_id AND student.s_id=score.s_id

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

SELECT st.s_name,c.c_name,sc.s_score
FROM student st,score sc,course c
WHERE
st.s_id=sc.s_id AND sc.c_id=c.c_id AND sc.s_score>70

– 37、查询不及格的课程

SELECT st.s_name,c.c_name,sc.s_score
FROM student st,score sc,course c
WHERE
st.s_id=sc.s_id AND sc.c_id=c.c_id AND sc.s_score<60

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

SELECT sc.s_id,st.s_name
FROM student st,score sc
WHERE
st.s_id=sc.s_id AND sc.c_id='01' AND sc.s_score>=80

– 39、求每门课程的学生人数

SELECT COUNT(sc.c_id),sc.c_id
FROM score sc
GROUP BY
sc.c_id

– 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT st.*,sc.s_score
FROM student st,score sc,course c,teacher
WHERE
st.s_id=sc.s_id AND sc.c_id=c.c_id AND teacher.t_id=c.t_id AND teacher.t_name='张三'
ORDER BY
sc.s_score
DESC LIMIT 0,1

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

SELECT m.*
FROM score m,(SELECT score.c_id,score.s_score FROM score GROUP BY score.c_id HAVING COUNT(1)>1 )n
WHERE m.c_id=n.c_id AND m.s_score=n.s_score

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

SELECT * FROM (
SELECT st.s_name, score.c_id,score.s_score 
FROM student st,score
WHERE st.s_id=score.s_id AND score.c_id='01'
ORDER BY
score.s_score
DESC LIMIT 0,2
)a
UNION ALL
SELECT * FROM (
SELECT st.s_name, score.c_id,score.s_score 
FROM student st,score
WHERE st.s_id=score.s_id AND score.c_id='02'
ORDER BY
score.s_score
DESC LIMIT 0,2
)b
UNION ALL
SELECT * FROM (
SELECT st.s_name, score.c_id,score.s_score 
FROM student st,score
WHERE st.s_id=score.s_id AND score.c_id='03'
ORDER BY
score.s_score
DESC LIMIT 0,2
)c

– 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
– 若人数相同,按课程号升序排列

SELECT score.c_id,COUNT(score.s_id)
FROM student st,score
WHERE
score.s_id=st.s_id 
GROUP BY
score.c_id
HAVING 
COUNT(score.s_id)>5
ORDER BY
COUNT(score.s_id) DESC,score.c_id

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

SELECT sc.s_id
FROM student st,score sc
WHERE st.s_id=sc.s_id
GROUP BY
sc.s_id
HAVING
COUNT(sc.c_id)>=2

– 45、查询选修了全部课程的学生信息

SELECT st.*
FROM student st,score sc
WHERE st.s_id=sc.s_id
GROUP BY
sc.s_id
HAVING
COUNT(sc.c_id)=3
Logo

更多推荐