27. 查询出只有两门课程的全部学生的学号和姓名
SELECT st.s_id, st.s_name FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id GROUP BY st.s_id HAVING COUNT( 1 ) = 2
28. 查询男生、女生人数
SELECT s_sex, COUNT(1) FROM student GROUP BY s_sex
29. 查询名字中含有"德"字的学生信息
SELECT * FROM student WHERE s_name LIKE '%德%'
30. 查询同名同性学生名单,并统计同名人数
select st.s_name,st.s_sex,count(1) from student st group by st.s_name,st.s_sex having cou
31. 查询1990年出生的学生名单
SELECT st.* FROM student st WHERE st.s_birth LIKE '1990%';
32. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c.c_id, c_name, AVG( sc.s_score ) AS scoreAvg FROM course c INNER JOIN score sc ON sc.c_id = c.c_id GROUP BY c.c_id ORDER BY scoreAvg DESC, c.c_id ASC;
33. 查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT st.s_id, st.s_name, ( CASE WHEN AVG( sc.s_score ) IS NULL THEN 0 ELSE AVG( sc.s_score ) END ) scoreAvg FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id GROUP BY st.s_id HAVING scoreAvg > '85';
34. 查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT * FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.s_score < 60 INNER JOIN course c ON c.c_id = sc.c_id AND c.c_name = '数学';
35. 查询所有学生的课程及分数情况
SELECT * FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id LEFT JOIN course c ON c.c_id = sc.c_id ORDER BY st.s_id, c.c_name;
36. 查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT st.s_id,st.s_name,c.c_name,sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id LEFT JOIN course c ON c.c_id = sc.c_id WHERE st.s_id IN ( SELECT st2.s_id FROM student st2 LEFT JOIN score sc2 ON sc2.s_id = st2.s_id GROUP BY st2.s_id HAVING MIN( sc2.s_score )>= 70 ORDER BY st2.s_id )
37. 查询不及格的课程
SELECT st.s_id, c.c_name, st.s_name, sc.s_score FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.s_score < 60 INNER JOIN course c ON c.c_id = sc.c_id
38. 查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT st.s_id, st.s_name, sc.s_score FROM student st INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id = '01' AND sc.s_score >= 80;
39. 求每门课程的学生人数
SELECT c.c_id, c.c_name, COUNT( 1 ) FROM course c INNER JOIN score sc ON sc.c_id = c.c_id GROUP BY c.c_id;
40. 查询选修"死亡歌颂者"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT st.*, sc.s_score FROM student st INNER JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id INNER JOIN teacher t ON t.t_id = c.t_id AND t.t_name = '死亡歌颂者' ORDER BY sc.s_score DESC LIMIT 0,1;
41. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT st.s_id, st.s_name, sc.c_id, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id LEFT JOIN course c ON c.c_id = sc.c_id WHERE ( SELECT COUNT( 1 ) FROM student st2 LEFT JOIN score sc2 ON sc2.s_id = st2.s_id LEFT JOIN course c2 ON c2.c_id = sc2.c_id WHERE sc.s_score = sc2.s_score AND c.c_id != c2.c_id )>1;
42. 查询每门功成绩最好的前两名
SELECT a.* FROM ( SELECT st.s_id, st.s_name, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '01' ORDER BY sc.s_score DESC LIMIT 0, 2 ) a UNION ALL SELECT b.* FROM ( SELECT st.s_id, st.s_name, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '02' ORDER BY sc.s_score DESC LIMIT 0, 2 ) b UNION ALL SELECT c.* FROM ( SELECT st.s_id, st.s_name, c.c_name, sc.s_score FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id INNER JOIN course c ON c.c_id = sc.c_id AND c.c_id = '03' ORDER BY sc.s_score DESC LIMIT 0, 2 ) c;
写法 2
SELECT a.s_id, a.c_id, a.s_score 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;
43. 统计每门课程的学生选修人数(超过5人的课程才统计)
- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c.c_id, COUNT( 1 ) FROM score sc LEFT JOIN course c ON c.c_id = sc.c_id GROUP BY c.c_id HAVING COUNT( 1 ) > 5 ORDER BY COUNT( 1 ) DESC, c.c_id ASC;
44. 检索至少选修两门课程的学生学号
SELECT st.s_id FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id GROUP BY st.s_id HAVING COUNT( 1 )>= 2;
45. 查询选修了全部课程的学生信息
SELECT st.* FROM student st LEFT JOIN score sc ON sc.s_id = st.s_id GROUP BY st.s_id HAVING COUNT( 1 )=( SELECT COUNT( 1 ) FROM course)
46. 查询各学生的年龄
SELECT st.*, TIMESTAMPDIFF( YEAR, st.s_birth, NOW()) FROM student st
47. 查询本周过生日的学生
SELECT st.* FROM student st WHERE WEEK ( NOW())+ 1 = WEEK ( DATE_FORMAT( st.s_birth, '%Y%m%d' ))
48. 查询下周过生日的学生
SELECT st.* FROM student st WHERE WEEK ( NOW())+ 1 = WEEK ( DATE_FORMAT( st.s_birth, '%Y%m%d' ));
49. 查询本月过生日的学生
SELECT st.* FROM student st WHERE MONTH ( NOW())= MONTH ( DATE_FORMAT( st.s_birth, '%Y%m%d' ));
50. 查询下月过生日的学生
SELECT st.* FROM student st WHERE MONTH ( TIMESTAMPADD( MONTH, 1, NOW()))= MONTH ( DATE_FORMAT( st.s_birth, '%Y%m%d' ));
【阿里巴巴开发手册】
内容偏向基础适合各个阶段人员的学习与巩固,如果对您还有些帮助希望给博主点个赞