MySQL入门到精通经典50题,看这一篇就够了

news2024/11/25 12:49:00

MySQL入门到精通经典50题学习笔记

  • pdf获取方式,公众号:每天都要努力coding
  • 回复:mysql经典50题

文章目录

    • MySQL入门到精通经典50题学习笔记
    • @[toc]
          • MySQL安装教程详解
          • 数据预览
          • sql建表语句
          • 1.查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
          • 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
          • 3.(略)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
          • 4.(略)查询姓“张”的老师个数
          • 5.查询没学过张三老师课的学生的学号和姓名
          • 6.查询学过张三老师所教的所有课程的同学的学号和姓名
          • 7.查询学过01课程并且学过02课程的学生
          • 8.(略)查询学过02课程的总成绩
          • 9.查询所有课程成绩小于60的学生
          • 10.查询没有学全所有课的学生学号、姓名(student为主表)
          • 11.查询至少有一门课与学号为“01”的学生所学课程相同的学生学号、姓名
          • 12.查询和“01”学号学生所学课程完全相同的学生学号
          • 13.查询没学过"张三"老师讲授的任一门课程的学生姓名
          • 15.查询两门及其以上不及格的同学的学号、姓名和平均成绩
          • 16.(略)检索01课程分数小于60 ,按课程分数降序排列学生信息
          • 17.按平均成绩从高到低显示所有学生的所有课程的成绩和平均成绩
          • 查询各科成绩最高分、最低分、平均分、及格率、中等率、优良率、优秀率
          • 19.按各科成绩进行排序,并显示排名
          • 20.(略)查询学生的总成绩并进行排名
          • 21.(略)查询不同老师所教不同课程平均分从高到低展示
          • 22.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
          • 23.使用分段来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
          • 24.查询学生平均成绩以及排名
          • 25.查询各科成绩前三名的记录(不考虑成绩并列情况)
          • 26.(略)查询每门课程被选修的学生数
          • 27.(略)查询出只有两门课程的全部学生的学号和姓名
          • 32.(略)查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
          • 34.(略)查询课程名称为"数学",且分数低于60分的学生姓名和分数
          • 35.查询所有学生的课程及分数情况
          • 36.查询任何一门课程成绩在70分以上的姓名、课程名称和分数
          • 37.(略)查询不及格的课程并按课程号从大到小排列
          • 38.(略)查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
          • 40.查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩
          • 41.查询某个不同课程但成绩相同的学生编号、课程编号、学生成绩
          • 42.查询每一门课程最好的前两名
          • 43.统计每门课程的学生选修人数(超过5人的课程才统计)
          • 44.检索至少选修两门课程的学生学号
          • 45.查询选修了全部课程的学生信息
          • 46.(略)查询各学生的年龄
          • 47.(略)查询本周过生日的学生
          • 48.查询下周过生日的学生
          • 49.(略)查询本月过生日的学生
          • 50.查询下月过生日的学生
          • 47.(略)查询本周过生日的学生
          • 48.查询下周过生日的学生
          • 49.(略)查询本月过生日的学生
          • 50.查询下月过生日的学生

MySQL安装教程详解
数据预览
  • 在这里插入图片描述
sql建表语句
  • teacher表
/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80030
 Source Host           : localhost:3306
 Source Schema         : learn

 Target Server Type    : MySQL
 Target Server Version : 80030
 File Encoding         : 65001

 Date: 11/12/2022 12:24:34
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`  (
  `TId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `Tname` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('01', '张三');
INSERT INTO `teacher` VALUES ('02', '李四');
INSERT INTO `teacher` VALUES ('03', '王五');

SET FOREIGN_KEY_CHECKS = 1;
  • student表
/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80030
 Source Host           : localhost:3306
 Source Schema         : learn

 Target Server Type    : MySQL
 Target Server Version : 80030
 File Encoding         : 65001

 Date: 11/12/2022 12:24:27
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `SId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `Sname` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `Sage` datetime NULL DEFAULT NULL,
  `Ssex` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('01', '赵雷', '1990-01-01 00:00:00', '男');
INSERT INTO `student` VALUES ('02', '钱电', '1990-12-21 00:00:00', '男');
INSERT INTO `student` VALUES ('03', '孙⻛', '1990-12-20 00:00:00', '男');
INSERT INTO `student` VALUES ('04', '李云', '1990-12-06 00:00:00', '男');
INSERT INTO `student` VALUES ('05', '周梅', '1991-12-01 00:00:00', '⼥');
INSERT INTO `student` VALUES ('06', '吴兰', '1992-01-01 00:00:00', '⼥');
INSERT INTO `student` VALUES ('07', '郑⽵', '1989-01-01 00:00:00', '⼥');
INSERT INTO `student` VALUES ('09', '张三', '2017-12-20 00:00:00', '⼥');
INSERT INTO `student` VALUES ('10', '李四', '2017-12-25 00:00:00', '⼥');
INSERT INTO `student` VALUES ('11', '李四', '2012-06-06 00:00:00', '⼥');
INSERT INTO `student` VALUES ('12', '赵六', '2013-06-13 00:00:00', '⼥');
INSERT INTO `student` VALUES ('13', '孙七', '2014-06-01 00:00:00', '⼥');

SET FOREIGN_KEY_CHECKS = 1;

  • course表
/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80030
 Source Host           : localhost:3306
 Source Schema         : learn

 Target Server Type    : MySQL
 Target Server Version : 80030
 File Encoding         : 65001

 Date: 11/12/2022 12:24:11
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course`  (
  `CId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `Cname` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `TId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('01', '语⽂', '02');
INSERT INTO `course` VALUES ('02', '数学', '01');
INSERT INTO `course` VALUES ('03', '英语', '03');

SET FOREIGN_KEY_CHECKS = 1;
  • sc表
/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80030
 Source Host           : localhost:3306
 Source Schema         : learn

 Target Server Type    : MySQL
 Target Server Version : 80030
 File Encoding         : 65001

 Date: 11/12/2022 12:24:21
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc`  (
  `SId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `CId` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
  `score` decimal(18, 1) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES ('01', '01', 80.0);
INSERT INTO `sc` VALUES ('01', '02', 90.0);
INSERT INTO `sc` VALUES ('01', '03', 99.0);
INSERT INTO `sc` VALUES ('02', '01', 70.0);
INSERT INTO `sc` VALUES ('02', '02', 60.0);
INSERT INTO `sc` VALUES ('02', '03', 80.0);
INSERT INTO `sc` VALUES ('03', '01', 80.0);
INSERT INTO `sc` VALUES ('03', '02', 80.0);
INSERT INTO `sc` VALUES ('03', '03', 80.0);
INSERT INTO `sc` VALUES ('04', '01', 50.0);
INSERT INTO `sc` VALUES ('04', '02', 30.0);
INSERT INTO `sc` VALUES ('04', '03', 20.0);
INSERT INTO `sc` VALUES ('05', '01', 76.0);
INSERT INTO `sc` VALUES ('05', '02', 87.0);
INSERT INTO `sc` VALUES ('06', '01', 31.0);
INSERT INTO `sc` VALUES ('06', '03', 34.0);
INSERT INTO `sc` VALUES ('07', '02', 89.0);
INSERT INTO `sc` VALUES ('07', '03', 98.0);

SET FOREIGN_KEY_CHECKS = 1;


1.查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
SELECT
	Student.*,
	01_score.CId,
	1_score,
	02_score.CId,
	2_score 
FROM
	Student
	JOIN ( SELECT SId, CId, score AS 1_score FROM SC WHERE CId = '01' ) AS 01_score ON Student.SId = 01_score.SId
	JOIN ( SELECT SId, CId, score AS 2_score FROM SC WHERE CId = '02' ) AS 02_score ON 01_score.SId = 02_score.SId 
WHERE
	1_score > 2_score;
2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT
	any_value ( sc.SId ) SId,
	any_value ( student.Sname ) Sname,
	avg( sc.score ) score 
FROM
	sc
	JOIN student ON sc.SId = student.SId 
GROUP BY
	sc.SId 
HAVING
	avg( sc.score ) > 60;
3.(略)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
	any_value(student.Sname),
	any_value(student.SId),
	count( sc.CId ) course_count,
	IFNULL( sum( sc.score ), 0 ) all_score
FROM
	student
  left JOIN sc ON student.SId = sc.SId 
GROUP BY
	student.SId;

4.(略)查询姓“张”的老师个数
SELECT count(*)
from teacher
where Tname like '张%';
5.查询没学过张三老师课的学生的学号和姓名
-- 查询学习过张三老师课的学生
SELECT SId
from sc
	INNER JOIN course on course.CId = sc.CId
	INNER JOIN teacher on teacher.TId = course.TId
where Tname =  "张三";

-- 
SELECT
	student.SId,
	student.Sname 
FROM
	student 
WHERE
	SId NOT IN (
        SELECT
        SId 
        FROM
        sc
        INNER JOIN course ON course.CId = sc.CId
        INNER JOIN teacher ON teacher.TId = course.TId 
        WHERE
        Tname = "张三" 
    );

6.查询学过张三老师所教的所有课程的同学的学号和姓名
SELECT
	student.SId,
	student.Sname,
	course.Cname,
	teacher.Tname 
FROM
	sc
	INNER JOIN course ON sc.CId = course.CId
	INNER JOIN teacher ON teacher.TId = course.TId
	INNER JOIN student ON student.SId = sc.SId 
WHERE
	teacher.Tname = "张三";
ORDER BY
	student.SId;
7.查询学过01课程并且学过02课程的学生
-- 查询同时学习过01和02课程的同学
SELECT
	a.SId 
FROM
	( SELECT sc.SId FROM sc WHERE sc.CId = '01' ) AS a
	INNER JOIN ( SELECT sc.SId FROM sc WHERE sc.CId = '02' ) AS b ON a.SId = b.SId;
-- 综合
SELECT
	* 
FROM
	student 
WHERE
	student.SId in (
        SELECT
        a.SId 
        FROM
        ( SELECT sc.SId FROM sc WHERE sc.CId = '01' ) AS a
        INNER JOIN ( SELECT sc.SId FROM sc WHERE sc.CId = '02' ) AS b ON a.SId = b.SId
    );
8.(略)查询学过02课程的总成绩
SELECT
	sum( score ) 
FROM
	sc 
WHERE
	sc.CId = '02';
9.查询所有课程成绩小于60的学生
-- 查询成绩小于60分的学生以及课程数
SELECT
	sc.sid,
	count( sc.CId ) AS acount 
FROM
	sc 
WHERE
	sc.score < 60 
GROUP BY
	sc.SId;

-- 查询每个学生的课程数
SELECT
	sc.sid,
	count( sc.CId ) AS bcount 
FROM
	sc 
GROUP BY
	sc.sid;

-- 汇总
SELECT
	a.sid,
	student.Sname 
FROM
	( SELECT sc.sid, count( sc.CId ) AS acount FROM sc WHERE sc.score < 60 GROUP BY sc.SId ) a
	INNER JOIN ( SELECT sc.sid, count( sc.CId ) AS bcount FROM sc GROUP BY sc.sid ) b ON a.SId = b.sid
	INNER JOIN student ON student.SId = a.sid 
WHERE
	a.acount = b.bcount;
10.查询没有学全所有课的学生学号、姓名(student为主表)
-- 查询所有的课程数量
SELECT count(*) FROM course;
-- 查询每个学生的课程数
SELECT sc.SId, count(sc.CId) from sc GROUP BY sc.SId;
-- 查询课程数小于课程数量的sid
SELECT
	any_value ( student.sid ),
	any_value ( student.sname ) 
FROM
	student
	LEFT JOIN sc ON sc.sid = student.sid 
GROUP BY
	student.sid 
HAVING
	count( DISTINCT sc.cid ) < ( SELECT count( cid ) FROM course );
11.查询至少有一门课与学号为“01”的学生所学课程相同的学生学号、姓名
-- 查询01号学生的课程号 
SELECT sc.CId from sc WHERE sc.SId = '01';
-- 查询课程号在01号学生中的学生
SELECT DISTINCT sc.SId
from sc
WHERE sc.CId in (SELECT sc.CId from sc WHERE sc.SId = '01')
and sc.SId != '01';
-- 汇总
SELECT
	student.SId,
	student.Sname 
FROM
	student
	INNER JOIN (
        SELECT DISTINCT
        sc.SId 
        FROM
        sc 
        WHERE
        sc.CId IN ( SELECT sc.CId FROM sc WHERE sc.SId = '01' ) 
        AND sc.SId != '01' 
    ) a ON a.SId = student.SId;

12.查询和“01”学号学生所学课程完全相同的学生学号
SELECT
	* 
FROM
	student 
WHERE
	SId IN (SELECT SId FROM (
        SELECT * FROM sc a WHERE CId IN ( SELECT CId FROM sc WHERE SId = 01 )) b 
            GROUP BY SId 
            HAVING count( CId ) =(SELECT count( CId ) FROM sc c WHERE SId = 01 )) 
	AND SId != 01;
13.查询没学过"张三"老师讲授的任一门课程的学生姓名
-- 查询张三老师讲授的课程
SELECT CId 
from 
	course, teacher
WHERE
	course.TId = teacher.TId
and 
	teacher.Tname = "张三";

-- 学过张三老师课程的学生
SELECT sc.SId 
from
	sc
	INNER JOIN course on course.CId = sc.CId
	INNER JOIN teacher on teacher.TId = course.TId
	AND teacher.Tname = "张三";


SELECT
	* 
FROM
	student 
WHERE
	SId NOT IN (
        SELECT
        sc.SId 
        FROM
        sc
        INNER JOIN course ON course.CId = sc.CId
        INNER JOIN teacher ON teacher.TId = course.TId 
        AND teacher.Tname = "张三" 
    );

15.查询两门及其以上不及格的同学的学号、姓名和平均成绩
SELECT
	any_value(sc.SId),
	any_value(student.Sname),
	AVG( sc.score ) 
FROM
	sc
	INNER JOIN student ON sc.SId = student.SId 
WHERE
	sc.score < 60 GROUP BY sc.SId HAVING count( DISTINCT sc.CId ) >= 2;

-- 查询每个学生成绩不及格的门数
SELECT SId,count(*) 
from sc
WHERE sc.score < 60
GROUP BY SId;
16.(略)检索01课程分数小于60 ,按课程分数降序排列学生信息
SELECT student.SId,Sname, sc.score
from 
	student
	INNER JOIN sc on student.SId = sc.SId
WHERE
	sc.score < 60 and sc.CId = 01
ORDER BY sc.score DESC;
17.按平均成绩从高到低显示所有学生的所有课程的成绩和平均成绩
SELECT
	sc.sid,
	max( CASE WHEN sc.cid = '01' THEN sc.score ELSE NULL END ) '语文',
	max( CASE WHEN sc.cid = '02' THEN sc.score ELSE NULL END ) '数学',
	max( CASE WHEN sc.cid = '03' THEN sc.score ELSE NULL END ) '英语',
	avg( sc.score ) 
FROM
	sc 
GROUP BY
	sc.sid 
ORDER BY
	avg( sc.score ) DESC;
  1. 查询各科成绩最高分、最低分、平均分、及格率、中等率、优良率、优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT 
	sc.CId,
	course.Cname,
  max(sc.score),
	min(sc.score),
	avg(sc.score),
	sum(CASE WHEN sc.score >= 60 THEN 1 ELSE 0 END) / count(sc.SId) '及格',
	sum(CASE WHEN sc.score >= 70 and sc.score < 80 THEN 1 ELSE 0 END) / count(sc.SId) '中等',
	sum(CASE WHEN sc.score >= 80 and sc.score < 90 THEN 1 ELSE 0 END) / count(sc.SId) '优良',
	sum(CASE WHEN sc.score >= 90 THEN 1 ELSE 0 END) / count(sc.SId) '优秀'
from sc
	INNER JOIN course on course.CId = sc.CId
GROUP BY sc.CId, course.Cname;

19.按各科成绩进行排序,并显示排名
SELECT
	sc.sid,
	sc.cid,
	course.Cname,
	sc.score,
	ROW_NUMBER() over ( ORDER BY sc.CId, sc.score DESC ) 
FROM
	sc
	INNER JOIN course on course.CId = sc.CId;

20.(略)查询学生的总成绩并进行排名
SELECT
	any_value(sc.SId),
	any_value(student.Sname),
	sum(sc.score) grade
from sc
	INNER JOIN student on student.SId = sc.SId
GROUP BY sc.SId
ORDER BY grade desc;

21.(略)查询不同老师所教不同课程平均分从高到低展示
SELECT 
	any_value(teacher.Tname),
	sc.CId,
	avg(sc.score)
from 
	sc
	INNER JOIN course on course.CId = sc.CId
	INNER JOIN teacher on teacher.TId = course.TId
GROUP BY sc.CId
ORDER BY AVG(sc.score) desc;
22.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT 
	any_value(student.Sname),
	any_value(sc.SId),
	sum(sc.score) grade
from sc
	INNER JOIN student on student.SId = sc.SId
GROUP BY sc.SId
ORDER BY grade desc
LIMIT 1, 2;
23.使用分段来统计各科成绩,分别统计各分数段人数:课程ID和课程名称
SELECT 
	sc.CId,
	any_value(course.Cname),
	sum(case WHEN sc.score < 60 THEN 1 else 0 end) '60以下',
	sum(case WHEN sc.score >= 60 and sc.score < 70 THEN 1 else 0 end) '60-70',
	sum(case WHEN sc.score >= 70 and sc.score < 80 THEN 1 else 0 end) '70-80',
	sum(case WHEN sc.score >= 80 and sc.score < 90 THEN 1 else 0 end) '80-90',
	sum(case WHEN sc.score >= 90 THEN 1 else 0 end) '90以上'
from 
	sc
	INNER JOIN course on course.CId = sc.CId
GROUP BY sc.CId;
24.查询学生平均成绩以及排名
SELECT
	sc.SId,
	any_value(student.Sname),
	avg( sc.score ) grade 
FROM
	sc
	INNER JOIN student on student.SId = sc.SId 
GROUP BY
	sc.SId 
ORDER BY
	grade DESC;

25.查询各科成绩前三名的记录(不考虑成绩并列情况)
-- PARTITION BY作用于分组之下的排序,与group by类似,但是有区别
-- 进行每个科目的排序
SELECT
	*,
	ROW_NUMBER() over ( PARTITION BY sc.cid ORDER BY sc.score DESC ) m 
FROM
	sc
	INNER JOIN student stu ON stu.sid = sc.sid

-- 汇总结果
-- 窗口函数
SELECT
	*
FROM
	(
        SELECT
        student.SId, sc.CId, sc.score, student.Sname, student.Sage, student.Ssex, 
        ROW_NUMBER() over ( PARTITION BY sc.cid ORDER BY sc.score DESC ) m 
        FROM
        sc
        INNER JOIN student ON student.sid = sc.sid 
    ) a 
WHERE
	m IN ( 1,2, 3 )

26.(略)查询每门课程被选修的学生数
SELECT 
	sc.CId,
	count(sc.SId)
from sc
GROUP BY sc.CId;
-- 答案
SELECT
	course.cid,
	count( sc.sid ) 
FROM
	sc
	INNER JOIN course ON sc.CId = course.cid 
GROUP BY
	course.cid
27.(略)查询出只有两门课程的全部学生的学号和姓名
SELECT student.SId, any_value(student.Sname)
from student
	INNER JOIN sc on sc.SId = student.SId
GROUP BY student.SId
HAVING count(sc.CId) = 2;


-- 28.(略)查询男生、女生人数
SELECT 
	student.Ssex,
	count(student.Ssex)
from student
GROUP BY student.Ssex;
  1. (略)查询名字中含有"三"字的学生信息
SELECT *
from student
WHERE student.Sname like "%三%";

31.查询1990年出生的学生名单

SELECT *
from student
WHERE year(student.Sage) = 1990;

-- 或者是用like
SELECT
	student.sid,
	student.sname 
FROM
	student 
WHERE
	student.sage LIKE '1990%';
32.(略)查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT student.SId, any_value(student.Sname), avg(sc.score) avg_score
FROM student 
	INNER JOIN sc on sc.SId = student.SId
GROUP BY student.SId
HAVING avg_score >= 85;

33.(略)查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,

-- 按课程号降序排列
SELECT 
	sc.CId, 
	avg(sc.score)
from sc
GROUP BY sc.CId
ORDER BY avg(sc.score) asc, sc.CId DESC;
34.(略)查询课程名称为"数学",且分数低于60分的学生姓名和分数
SELECT student.Sname, sc.score
from student
	INNER JOIN sc on student.SId = sc.SId
	INNER JOIN course on course.CId = sc.CId and course.Cname = "数学"
WHERE
	sc.score < 60;
35.查询所有学生的课程及分数情况
SELECT
	student.sid,
	any_value(student.sname),
	max( CASE WHEN course.cname = '语文' THEN sc.score ELSE NULL END ) '语文',
	max( CASE WHEN course.cname = '数学' THEN sc.score ELSE NULL END ) '数学',
	max( CASE WHEN course.cname = '英语' THEN sc.score ELSE NULL END ) '英语' 
FROM
	student
	INNER JOIN sc ON sc.sid = student.sid
	INNER JOIN course ON sc.CId = course.CId 
GROUP BY
	student.sid;

36.查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT student.SId, student.Sname, course.Cname, sc.score
from student
	INNER JOIN sc on sc.SId = student.SId
	INNER JOIN course on course.CId = sc.CId
WHERE sc.score > 70;
37.(略)查询不及格的课程并按课程号从大到小排列
SELECT
	student.sid,
	student.sname,
	course.cname,
	sc.cid,
	sc.score 
FROM
	sc
	INNER JOIN course ON course.cid = sc.cid
	INNER JOIN student ON student.sid = sc.SId 
WHERE
	sc.score < 60 
ORDER BY
	sc.cid DESC;
38.(略)查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
SELECT student.SId, student.Sname, sc.score
from student
	INNER JOIN sc on sc.SId  = student.SId and sc.CId = 03
WHERE sc.score > 80;


-- 39.(略)求每门课程的学生人数
SELECT any_value(course.Cname), count(DISTINCT course.CId)
from course
	INNER JOIN sc on sc.CId  = course.CId
GROUP BY sc.CId;
40.查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩
SELECT 
	any_value(student.SId), any_value(student.Sname), 
	any_value(sc.CId),any_value(course.Cname),
	max(sc.score)
from student
	INNER JOIN sc on sc.SId = student.SId
	INNER JOIN course on course.CId = sc.CId
	INNER JOIN teacher on teacher.TId = course.TId
WHERE teacher.Tname = "张三";
41.查询某个不同课程但成绩相同的学生编号、课程编号、学生成绩
SELECT
	sid 
FROM
	(SELECT b.sid,b.score 
     FROM sc AS b
     INNER JOIN ( SELECT sid FROM sc GROUP BY sid HAVING count( DISTINCT cid )> 1 ) AS c ON b.sid = c.sid 
     GROUP BY
     b.sid,
     b.score 
    ) AS a 
GROUP BY sid HAVING	count( sid )=1
42.查询每一门课程最好的前两名
SELECT a.CId, a.SId, any_value(a.score)
from sc a
	LEFT JOIN sc b
	on a.CId = b.CId
	and a.score < b.score	
	GROUP BY a.CId, a.SId
HAVING count(a.CId) < 2;
43.统计每门课程的学生选修人数(超过5人的课程才统计)
-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT sc.CId, count(sc.SId) as cnt
from sc
GROUP BY sc.CId
HAVING cnt > 5
ORDER BY cnt DESC, sc.CId ASC;
44.检索至少选修两门课程的学生学号
SELECT sc.SId, any_value(student.Sname), count(sc.CId) cnt
from sc
	INNER JOIN student on student.SId = sc.SId
GROUP BY sc.SId
HAVING cnt >= 2;
45.查询选修了全部课程的学生信息
SELECT sc.SId, any_value(student.Sname), count(sc.CId) cnt
from sc
	INNER JOIN student on student.SId = sc.SId
GROUP BY sc.SId
HAVING cnt = (SELECT count(*) from course);


-- 日期类函数
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT DATE("2022-12-10");
46.(略)查询各学生的年龄
SELECT
	student.Sname,
	TIMESTAMPDIFF(
        YEAR,
        student.Sage,
        CURTIME()) 
FROM
	student;
47.(略)查询本周过生日的学生
SELECT
	* 
FROM
	student 
WHERE 
	WEEK(Sage, 1) = WEEK(NOW(), 1);

-- WEEKOFYEAR() 和 CURDATE()。WEEKOFYEAR() 函数用于获取某个日期所在的周,而 CURDATE() 函数用于获取当前日期。
SELECT * from student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE());
48.查询下周过生日的学生
-- 如果想要查询的是下一周过生日的学生,可以把 CURDATE() 替换为 CURDATE() + INTERVAL 7 DAY
SELECT * from student WHERE 
WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE() + INTERVAL 7 day);
49.(略)查询本月过生日的学生
SELECT * from student
WHERE MONTH(student.Sage) = MONTH(CURDATE());
50.查询下月过生日的学生
SELECT * from student
WHERE MONTH(student.Sage) = MONTH(CURDATE() + INTERVAL 1 MONTH);

d
GROUP BY sc.SId
HAVING cnt = (SELECT count(*) from course);

– 日期类函数
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT DATE(“2022-12-10”);




##### 46.(略)查询各学生的年龄

```sql
SELECT
	student.Sname,
	TIMESTAMPDIFF(
        YEAR,
        student.Sage,
        CURTIME()) 
FROM
	student;
47.(略)查询本周过生日的学生
SELECT
	* 
FROM
	student 
WHERE 
	WEEK(Sage, 1) = WEEK(NOW(), 1);

-- WEEKOFYEAR() 和 CURDATE()。WEEKOFYEAR() 函数用于获取某个日期所在的周,而 CURDATE() 函数用于获取当前日期。
SELECT * from student
WHERE WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE());
48.查询下周过生日的学生
-- 如果想要查询的是下一周过生日的学生,可以把 CURDATE() 替换为 CURDATE() + INTERVAL 7 DAY
SELECT * from student WHERE 
WEEKOFYEAR(student.Sage) = WEEKOFYEAR(CURDATE() + INTERVAL 7 day);
49.(略)查询本月过生日的学生
SELECT * from student
WHERE MONTH(student.Sage) = MONTH(CURDATE());
50.查询下月过生日的学生
SELECT * from student
WHERE MONTH(student.Sage) = MONTH(CURDATE() + INTERVAL 1 MONTH);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/86592.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于鸽群算法优化的lssvm回归预测-附代码

基于鸽群算法优化的lssvm回归预测 - 附代码 文章目录基于鸽群算法优化的lssvm回归预测 - 附代码1.数据集2.lssvm模型3.基于鸽群算法优化的LSSVM4.测试结果5.Matlab代码摘要&#xff1a;为了提高最小二乘支持向量机&#xff08;lssvm&#xff09;的回归预测准确率&#xff0c;对…

你的项目需要自动化测试吗?看看这篇文章再说吧

什么是自动化测试&#xff1f; 通过代码的方式&#xff0c;实现测试用例的自动运行&#xff0c;评估运行结果&#xff0c;并对测试结果及异常情况进行记录。 为什么进行自动化测试&#xff1f; 纯手工测试会有好多重复性的操作&#xff0c;浪费时间&#xff0c;而且频繁的回…

央企招聘:中国大唐集团2023年度公开招聘公告

中国大唐集团科学技术研究总院有限公司&#xff08;以下简称科研总院&#xff09;是世界500强企业——中国大唐集团有限公司&#xff08;以下简称集团公司&#xff09;的全资子公司&#xff0c;是集团公司的科技创新中心、战略参谋本部、成果孵化中心、技术服务保障基地和科技人…

Qt+VS+VTK综合开发环境配置

说明 本文旨在介绍一种个人常用的开发环境&#xff0c;主要解决在VS中开发Qt项目的部分问题&#xff0c;以及解决使用基于Qt的VTK库开发时可能遇到的部分问题&#xff0c;并通过合理的配置提升新项目搭建时间 该教程使用版本为VS2017&#xff0c;Qt5.14.2以及VTK8.2&#xff…

git_No.1_版本管控的全流程

文章目录1.获取Git仓库1.1 已存在目录中初始化仓库1.2 克隆一个仓库2. 将变更提交到仓库2.1 查看当前文件状态2.2 跟踪新文件2.3 暂存已修改的文件2.4 忽略文件2.5 查看已暂存和未暂存的修改2.6 提交更新2.7 跳过使用暂存区2.8 移除文件3. 查看提交历史4.远程仓库4.1 查看远程仓…

HTTP介绍

目录 1.什么是HTTP&#xff1f; 2.HTTP的特点 3.HTTP的优点和缺点 4.HTTP请求数据格式 5.HTTP响应数据格式 6.GET请求和POST请求的区别 7.状态码分类说明 8.查看发送的请求数据包方式 1.什么是HTTP&#xff1f; Hyper Text Transfer Protocol,超文本传输协议&#xff…

[036]基于Vue+Nodejs的网上作业批改系统(含源码、数据库、万字课设报告)

文章目录一、项目介绍二、源码获取一、项目介绍 网络作业提交与批改系统&#xff1a;超强的作业批改项目&#xff0c;技术栈是VueNodeMysql&#xff0c;最重要的是有超详细的万字报告&#xff0c;一劳永逸&#xff0c;可冲~ 主要功能介绍&#xff1a; 1.管理员功能模块 管理员…

分享几个宝藏微信小程序

1.有数鸟&#xff1a;了解会员在各平台的消费明细 平时我们看视频听音乐&#xff0c;我们用的很多vip&#xff0c;你还记得你注册了哪些应用吗&#xff1f;有了这个小程序&#xff0c;就可以帮你记录每个平台的付款详情&#xff0c;以及总支出。 当我们添加会员项目时&#xff…

基于java+springboot+mybatis+vue+mysql的乒乓球预约管理系统

项目介绍 随着信息化时代的到来&#xff0c;管理系统都趋向于智能化、系统化&#xff0c;乒乓球馆预约管理系统 也不例外&#xff0c;但目前国内仍都使用人工管理&#xff0c;市场规模越来越大&#xff0c;同时信息量也越来越庞大&#xff0c;人工管理显然已无法应对时代的变…

基于C+++FLTK实现(WinForm)超市收银系统【100010032】

超市收银系统 Supermarket POS system 本次大作业是利用 C语言&#xff0c;基于 FLTK 图形库来设计超市收银系统。首先介绍程序的设计思想&#xff1a; 这套程序完全是基于题目所给的要求逐条逐步设计的。我把程序实现大致分为数据层面和图形层面。 数据层面&#xff1a; 程…

抖音视频怎么拼接?分享一款功能强大的软件

抖音视频怎么拼接&#xff1f;利用软件就很方便&#xff0c;所以抖音视频拼接可以通过操作简单的视频软件来辅助比较简单。就比如万兴喵影&#xff0c;就是一款操作简单、功能齐全的视频编辑器&#xff0c;很适合新手小白和半专业人士。 用万兴喵影做视频拼接&#xff0c;只需要…

扩散模型类似的方式训练text_to_text可不可以

🍿*★,*:.☆欢迎您/$:*.★* 🍿 正文 # 使用 64*64 图 写入 16号字体 16个字 # 每组数据 的 文字 是连续的 # 三张图组成一个训练 # 至少48 个字符训练一次 # 选择好自己的文本数据集

Springboot整合Netty,自定义协议实现

Springboot整合Netty&#xff0c;自定义协议实现 Springboot整合Netty 新建springboot项目&#xff0c;并在项目以来中导入netty包&#xff0c;用fastjson包处理jsonStr。 <!-- netty --><dependency><groupId>io.netty</groupId><artifactId>…

Unity物理系统-物理材质-Collider碰撞体的弹力不够大如何处理

物理材质Physic Material&#xff1b; 当碰撞器发生碰撞的时候&#xff0c;具有该材质的游戏物体就会具有该材质的属性&#xff1b; Step1&#xff1a;在Project窗口下新建Physic Material&#xff08;命名为mat&#xff09;&#xff0c;并更改其弹力参数为1 Step2&#xff1a…

408 考研《操作系统》第二章第七节:死锁和死锁的三种处理策略(预防死锁、避免死锁、检测和解除)

文章目录1.死锁1.1 什么是死锁&#xff1f;1.2 死锁、饥饿、死循环的区别1.3 死锁产生的必要条件1.4 什么时候会发生死锁&#xff1f;1.6 总结2. 死锁的处理策略——预防死锁2.1 破坏互斥条件2.2 破坏不剥夺条件2.3 破坏请求和保持条件2.4 破坏循环等待条件2.5 总结3. 死锁的处…

chatgpt+mirai实现QQ机器人

chatGPTbot 配合mirai机器使用 安装 Java &#xff08;版本必须 > 11&#xff09; 下载Mirai 控制台 https://github.com/iTXTech/mirai-console-loader/releases 手动安装插件mirai-api-http 1.双击mcl.cmd 或./mcl 运行 Mirai Console 生成 plugins 文件夹 2.从 ht…

Android基于开源项目搭建自己的技术堆栈

一、app的整体架构 从较高的层次讲&#xff0c;一个APP的整体架构可以分为两层&#xff0c;即应用层和基础框架层。 1、应用层专注于行业领域的实现&#xff0c;如金融、支付、地图、社交等&#xff0c;它直接面向用户&#xff0c;是用户对产品的第一层感知。 2、基础框架层…

从安装过程品国产Linux操作系统的技术来源与异同之处

作者&#xff1a;IT圈黎俊杰 使用Linux操作系统的第一步无疑是安装&#xff0c;本文通过作者亲手对多个国外主流开源操作系统、多个主流国产操作系统的安装过程进行记录与对比&#xff0c;让大家可以借助安装过程的界面风格、页面布局、功能等&#xff0c;细细的品一下国产Linu…

Unity3D教程:简单的碰撞检测

需求&#xff1a;当立方体Cube碰到地面Plane的时候&#xff0c;输出碰撞物体的名称&#xff0c;则表述检测到立方体碰撞了地面。 1.搭建一个简单的场景。 在新的工程中选择File->new Scene创建新的场景。然后在该场景中添加地板&#xff1a;GameObject->Create Other-&…

Mapstruct类型转换Person ->PersonDTO以及po、vo、dto、request概念

文章目录po、vo、dto、requestPerson ->PersonDTO安装插件引入依赖Maven项目结构图Person 与 PersonDTOConverterpo、vo、dto、request Mapstruct 完成类型转换&#xff01; po&#xff1a;数据持久层对象&#xff0c;用于映射数据库中的表 dto&#xff1a;数据传输对象&a…