【MySQL】测试题03

news2024/12/23 23:18:48

文章目录

      • 1、创建数据库
      • 2、使用数据库
      • 3、创建数据表
        • 【3.1】创建学生信息表Student
        • 【3.2】创建课程信息表Course
        • 【3.3】创建教师信息表Teacher
        • 【3.4】创建成绩信息表Score
      • 4、添加数据
        • 【4.1】向学生student表添加数据
        • 【4.2】向课程course表添加数据
        • 【4.3】向教师信息teacher表添加数据
        • 【4.4】向成绩score表添加数据
      • 5、查询数据练习

1、创建数据库

CREATE DATABASE IF NOT EXISTS `exam`;

2、使用数据库

USE exam;

3、创建数据表

【3.1】创建学生信息表Student

CREATE TABLE IF NOT EXISTS `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`)
);

【3.2】创建课程信息表Course

CREATE TABLE IF NOT EXISTS `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);

【3.3】创建教师信息表Teacher

CREATE TABLE IF NOT EXISTS `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);

【3.4】创建成绩信息表Score

CREATE TABLE IF NOT EXISTS `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);

4、添加数据

【4.1】向学生student表添加数据

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','女');

在这里插入图片描述

【4.2】向课程course表添加数据

INSERT INTO Course VALUES('01' , '语文' , '02'); 
INSERT INTO Course VALUES('02' , '数学' , '01'); 
INSERT INTO Course VALUES('03' , '英语' , '03');

在这里插入图片描述

【4.3】向教师信息teacher表添加数据

INSERT INTO Teacher VALUES('01' , '张三'); 
INSERT INTO Teacher VALUES('02' , '李四'); 
INSERT INTO Teacher VALUES('03' , '王五');

在这里插入图片描述

【4.4】向成绩score表添加数据

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);

在这里插入图片描述

5、查询数据练习

01 查询“01”课程比“02”课程成绩高的学生的信息及课程分数

  本质:行运算:比较运算
  方法:1、直接比较:子查询 2、弄成两列:自关联(注意显式连接,提升查询速度)
  备注:任一科成绩缺失不予比较

  1】直接比较:子查询

SELECT s.*,sc.s_score
FROM (
SELECT sc1.s_id,sc1.s_score
FROM score sc1
WHERE sc1.c_id='01'
AND sc1.s_score>(SELECT s_score FROM score WHERE s_id=sc1.s_id AND c_id='02')) sc JOIN student s ON sc.s_id=s.s_id;

  2】弄成两列:自关联(注意显式连接,提升查询速度)

SELECT s.*,sc3.s_score 
FROM
	(
	SELECT sc1.s_id,sc1.s_score 
	FROM
		( SELECT s_id, s_score FROM score WHERE c_id = '01' ) sc1
		JOIN ( SELECT s_id, s_score FROM score WHERE c_id = '02' ) sc2 ON sc1.s_id = sc2.s_id AND sc1.s_score > sc2.s_score 
	) sc3
	JOIN student s ON sc3.s_id = s.s_id

  3】直接自关联

SELECT s.*,sc1.s_score
FROM student s 
JOIN score sc1 ON s.s_id=sc1.s_id AND sc1.c_id='01'
JOIN score sc2 ON s.s_id=sc2.s_id AND sc2.c_id='02' AND sc1.s_score>sc2.s_score

02 查询“01”课程比“02”课程成绩低的学生的信息及课程分数(题目 1 是成绩高)

  本质:行运算:比较运算
  方法:1、直接比较:子查询 2、弄成两列:自关联(注意显式连接,提升查询速度)
  备注:任一科成绩缺失不予比较

  1】直接比较:子查询

SELECT s.*,sc.s_score
FROM (
SELECT sc1.s_id,sc1.s_score
FROM score sc1
WHERE sc1.c_id='01'
AND sc1.s_score<(SELECT s_score FROM score WHERE s_id=sc1.s_id AND c_id='02')) sc JOIN student s ON sc.s_id=s.s_id

  2】弄成两列:自关联(注意显式连接,提升查询速度)

SELECT s.*,sc3.s_score 
FROM
	(
	SELECT sc1.s_id,sc1.s_score 
	FROM
		( SELECT s_id, s_score FROM score WHERE c_id = '01' ) sc1
		JOIN ( SELECT s_id, s_score FROM score WHERE c_id = '02' ) sc2 ON sc1.s_id = sc2.s_id AND sc1.s_score < sc2.s_score 
	) sc3
	JOIN student s ON sc3.s_id = s.s_id

  3】直接自关联

SELECT s.*,sc1.s_score
FROM student s 
JOIN score sc1 ON s.s_id=sc1.s_id AND sc1.c_id='01'
JOIN score sc2 ON s.s_id=sc2.s_id AND sc2.c_id='02' AND sc1.s_score<sc2.s_score

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

  本质:行聚合,后having筛选
  方法:直接聚合,后having对聚合函数进行筛选

SELECT sc.s_id,s.s_name,ROUND(AVG(sc.s_score),0) AS score
FROM score sc JOIN student s ON sc.s_id=s.s_id
GROUP BY sc.s_id
HAVING score>=60

  1】附加题:总分超过200分的同学

SELECT sc.s_id,s.s_name,SUM(sc.s_score) AS score
FROM score sc JOIN student s ON sc.s_id=s.s_id
GROUP BY sc.s_id
HAVING score>200

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

  本质:行聚合,后having筛选
  方法:直接聚合,聚合后用having筛选,注意主表
  备注:1、主表是学生表,没有成绩的需要包含在内,故left join 2、having可看作聚合函数的where:为空判断,比较运算,子查询 3、用ifnull处理空/left join情况

  1】null 判断

SELECT s.s_id,s.s_name,ROUND(AVG(sc.s_score),0) AS score
FROM student s
LEFT JOIN score sc ON s.s_id=sc.s_id
GROUP BY s.s_id
HAVING score IS NULL OR score < 60

  2】ifnull 函数:更兼容

SELECT s.s_id,s.s_name,ROUND(AVG(IFNULL(sc.s_score,0)),0) AS score
FROM student s
LEFT JOIN score sc ON s.s_id=sc.s_id
GROUP BY s.s_id
HAVING score < 60

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

  本质:行聚合
  方法:直接聚合,注意对象范围
  备注:主表是学生表,需考虑没有选课or没有成绩的情况

SELECT s.s_id,s.s_name,
COUNT(DISTINCT sc.c_id) AS c_num,
SUM(sc.s_score) AS score
FROM student s LEFT JOIN score sc ON s.s_id=sc.s_id
GROUP BY s.s_id

  如果需要聚合后筛选,则使用if和ifnull函数
  if和ifnull是好函数,处理null情况非常方便。
  注:ifnull搭配值运算函数使用,if函数搭配count函数

SELECT s.s_id,s.s_name,
IF(sc.s_id IS NULL,0,COUNT(DISTINCT sc.c_id)) AS c_num,
SUM(IFNULL(sc.s_score,0)) AS score
FROM student s LEFT JOIN score sc ON s.s_id=sc.s_id
GROUP BY s.s_id

06 查询“李”姓老师的数量

  本质:行筛选聚合
  方法:筛选计数
  问题:like是最优的吗

  1】like通配符

SELECT COUNT(DISTINCT t_id) AS t_num
FROM Teacher
WHERE t_name LIKE ‘李%’

  2】截取姓氏,然后判断

SELECT COUNT(DISTINCT t_id) AS t_num
FROM Teacher
WHERE SUBSTR(t_name,1,1)=‘李’

07 查询学过张三老师授课的同学的信息

  本质:行筛选:逐级
  方法:表关联or子查询
  1】逐级子查询,这样更好理解

SELECT s.*
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
WHERE c_id IN(SELECT c_id FROM Course WHERE t_id IN(SELECT t_id FROM Teacher WHERE t_name=‘张三’))

  2】表关联

SELECT s.*
FROM score sc
JOIN course c ON c.c_id=sc.c_id
JOIN teacher t ON t.t_id=c.t_id AND t.t_name=‘张三’
JOIN student s ON sc.s_id=s.s_id

08 找出没有学过张三老师课程的学生

  本质:反向行筛选
  方法:按照学过筛选,最后取相反情况left join 或right join,且is null

  1】表关联

SELECT s.*
FROM score sc
JOIN course c ON c.c_id=sc.c_id
JOIN teacher t ON t.t_id=c.t_id AND t.t_name=‘张三’
RIGHT JOIN student s ON sc.s_id=s.s_id
WHERE sc.s_id IS NULL

  1】not in,这个好理解

SELECT *
FROM student
WHERE s_id NOT IN(
SELECT sc.s_id
FROM score sc
JOIN course c ON c.c_id=sc.c_id
JOIN teacher t ON t.t_id=c.t_id AND t.t_name=‘张三’
)

09 查询学过编号为 01,并且学过编号为 02 课程的学生信息

  本质:行筛选:取交集
  方法:自关联 or 子查询

  1】自关联

SELECT s.*
FROM Score sc1
JOIN Score sc2 ON sc1.s_id=sc2.s_id AND sc1.c_id=‘01’ AND sc2.c_id=‘02’
JOIN Student s ON sc1.s_id=s.s_id

  1】子查询

SELECT s.*
FROM Score sc1
JOIN Student s ON sc1.s_id=s.s_id
WHERE sc1.c_id=‘01’
AND sc1.s_id IN(SELECT s_id FROM Score sc2 WHERE sc1.s_id=sc2.s_id AND sc2.c_id=‘02’)

10 查询学过 01 课程,但是没有学过 02 课程的学生信息(注意和上面9题目的区别)

  本质 行筛选:取交集
  方法 子查询,不能使用自关联,因为一条cid一条记录,不能用否判断

SELECT s.*
FROM Score sc1
JOIN Student s ON sc1.s_id=s.s_id
WHERE sc1.c_id=‘01’
AND sc1.s_id NOT IN(SELECT s_id FROM Score sc2 WHERE sc1.s_id=sc2.s_id AND sc2.c_id=‘02’)

11 查询没有学完全部课程的同学的信息

  本质:行聚合,后having筛选
  方法 直接聚合,注意表对象范围
  备注 having可看作聚合函数的where:为空判断,比较运算,子查询

SELECT s.*
FROM student s
LEFT JOIN score sc ON sc.s_id=s.s_id
GROUP BY s.s_id
HAVING COUNT(DISTINCT sc.c_id)<(SELECT COUNT(*) FROM course)

  发现筛选,先筛选出学完全部课程的同学

SELECT *
FROM Student
WHERE s_id NOT IN(SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(c_id)=(SELECT COUNT(*) FROM Course))

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

  本质 行筛选:子查询
  方法 1、学号01所学课程c_id01 2、学号不等于01且c_id in c_id01

SELECT s.*
FROM score sc
JOIN student s ON sc.s_id=s.s_id AND sc.s_id<>‘01’
WHERE sc.c_id IN(SELECT c_id FROM score WHERE s_id=‘01’)
GROUP BY s.s_id

13 查询和 01 同学学习的课程完全相同的同学的信息

#本质 行筛选:子查询
#方法 1、学号01所学课程 2、和01所学相同课程且课程数等于01课程数
#备注 关键是思考方法
#group_concat()函数用法
#group_concat([DISTINCT] 字 段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])

  1】方法1 课程相同的数量相同

SELECT s.*
FROM score sc
JOIN student s ON sc.s_id=s.s_id

  课程数量相等

WHERE sc.s_id IN(SELECT s_id FROM Score WHERE s_id <>‘01’ GROUP BY s_id HAVING COUNT(DISTINCT c_id)=(SELECT COUNT(DISTINCT c_id) FROM score WHERE s_id=‘01’))

  课程相同的数量相同

AND c_id IN(SELECT c_id FROM score WHERE s_id=‘01’)
GROUP BY s.s_id
HAVING COUNT(DISTINCT sc.c_id)=(SELECT COUNT(DISTINCT c_id) FROM score WHERE s_id=‘01’)

  2】使用group_concat()函数,如果记录数比较大,建议采用方法1

SELECT s.*
FROM student s
JOIN score sc ON s.s_id=sc.s_id AND sc.s_id<>‘01’
GROUP BY s.s_id
HAVING GROUP_CONCAT(sc.c_id ORDER BY sc.c_id)=
(SELECT GROUP_CONCAT(c_id ORDER BY c_id) FROM score WHERE s_id=‘01’ GROUP BY s_id)

14 查询没有修过张三老师讲授的任何一门课程的学生姓名

#本质 反向行筛选
#方法 修过张三老师任一门课即不符合要求
#备注 注意表对象是全体学生,主表是学生表,因对s_id筛选,故不用再和score关联

SELECT s_name
FROM student
WHERE s_id NOT IN(
SELECT s_id FROM score
WHERE c_id IN(SELECT c_id FROM course WHERE t_id=(SELECT t_id FROM teacher WHERE t_name=‘张三’)))

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

#本质 行筛选并聚合
#方法 1、行筛选:及格与否 2、求不及格课程数 3、having筛选
#备注 修过才会有及格与否之说,故score为主表

  1】方式1

SELECT sc.s_id,s.s_name,ROUND(AVG(sc.s_score),0) AS avg_score
FROM score sc
JOIN student s ON sc.s_id=s.s_id
GROUP BY sc.s_id
HAVING SUM(IF(sc.s_score<60,1,0))>=2

  2】方式2 查询表更小

SELECT sc.s_id,s.s_name,ROUND(AVG(sc.s_score),0) AS avg_score
FROM score sc
JOIN student s ON sc.s_id=s.s_id AND sc.s_score<60
GROUP BY sc.s_id
HAVING COUNT(*)>=2

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

#本质 行筛选并排序
#方法 分数<60、分数降序
#备注 排序函数区别 1、row_number()over:123 2、rank()over():113 3、dense_rank()over():112

SELECT s.*
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
WHERE c_id=‘01’
AND s_score<60
ORDER BY s_score DESC

17 按平均成绩从高到低(降序)显示所有学生的所有课程的成绩以及平均成绩

#本质 表关联并排序
#方法 找到主表,找到排序字段
#备注 如何将排序字段和其他字段关联是关键点,这里的平均成绩是所选课程的平均成绩

  1】纵表

SELECT s.s_name,c.c_name,sc.s_score,sc2.avg_score
FROM Student s
JOIN Course c
LEFT JOIN Score sc ON s.s_id=sc.s_id AND c.c_id=sc.c_id
LEFT JOIN (SELECT s_id,ROUND(AVG(s_score),0) AS avg_score FROM Score GROUP BY s_id) sc2 ON s.s_id=sc2.s_id
ORDER BY sc2.avg_score DESC

  2】横表

SELECT s.s_name,
MAX(CASE sc.c_id WHEN ‘01’ THEN sc.s_score END) AS ‘语文’,
MAX(CASE sc.c_id WHEN ‘02’ THEN sc.s_score END) AS ‘数学’,
MAX(CASE sc.c_id WHEN ‘03’ THEN sc.s_score END) AS ‘英语’,
sc2.avg_score AS ‘平均成绩’
FROM Student s
JOIN Course c
LEFT JOIN Score sc ON s.s_id=sc.s_id AND c.c_id=sc.c_id
LEFT JOIN (SELECT s_id,ROUND(AVG(s_score),0) AS avg_score FROM Score GROUP BY s_id) sc2 ON s.s_id=sc2.s_id
GROUP BY s.s_name,sc2.avg_score
ORDER BY sc2.avg_score DESC

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

#本质 表关联并聚合
#方法 主表course,left join 成绩表score,求最值、均值及分组
#备注 注意小数位数及百分率符号

SELECT c.c_id AS ‘课程id’,c.c_name AS ‘课程name’,
MAX(sc.s_score) AS ‘最高分’,
MIN(sc.s_score) AS ‘最低分’,
ROUND(AVG(sc.s_score),2) AS ‘平均分’,
CONCAT(ROUND(SUM(IF(sc.s_score>=60,1,0))/COUNT(sc.s_id)*100,2),‘%’) AS ‘及格率’,
CONCAT(ROUND(SUM(IF(sc.s_score>=70 AND sc.s_score<80,1,0))/COUNT(sc.s_id)*100,2),‘%’) AS ‘中等率’,
CONCAT(ROUND(SUM(IF(sc.s_score>=80 AND sc.s_score<90,1,0))/COUNT(sc.s_id)*100,2),‘%’) AS ‘优良率’,
CONCAT(ROUND(SUM(IF(sc.s_score>=90,1,0))/COUNT(sc.s_id)*100,2),‘%’) AS ‘优秀率’
FROM Course c
LEFT JOIN Score sc ON c.c_id=sc.c_id
GROUP BY c.c_id

19 按照各科成绩进行排序,并且显示排名

#本质 分组排序
#方法 选择排序依据并排名

SELECT c.c_name,s.s_name,sc.s_score,
ROW_NUMBER() OVER(PARTITION BY c.c_name ORDER BY sc.s_score DESC) AS ‘排名’
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
JOIN Course c ON sc.c_id=c.c_id

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

#本质 行聚合后排序
#方法 sum、排序

SELECT s.s_name,SUM(sc.s_score) AS ‘总成绩’,
ROW_NUMBER()OVER(ORDER BY SUM(sc.s_score) DESC) AS ‘排名’
FROM Student s
LEFT JOIN Score sc ON s.s_id=sc.s_id
GROUP BY s.s_name

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

#本质 表关联后聚合再排序
#方法 主表是教师表teacher和课程表course笛卡尔积,left join 成绩表score,求均值后order by

SELECT t.t_name,c.c_name,ROUND(AVG(sc.s_score),2) AS ‘平均分’
FROM Teacher t
JOIN Course c ON t.t_id=c.t_id
LEFT JOIN Score sc ON c.c_id=sc.c_id
GROUP BY t.t_name,c.c_name
ORDER BY AVG(sc.s_score) DESC

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

#本质 行排序后筛选
#方法 所有课程成绩排名,再取2和3名

SELECT r.c_name,r.rank_num,s.s_name,r.s_score
FROM
(SELECT c.c_name,sc.s_id,sc.s_score,
ROW_NUMBER()OVER(PARTITION BY c.c_name ORDER BY sc.s_score DESC) AS rank_num
FROM Course c
LEFT JOIN Score sc ON c.c_id=sc.c_id)r
JOIN Student s ON r.s_id=s.s_id AND r.rank_num IN(2,3)

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

#本质 行分组聚合
#方法 先列基本:科目、成绩,后分组计数

SELECT sc.c_id,c.c_name,
SUM(IF(sc.s_score>=85,1,0)) AS ‘[100-85]人数’,
SUM(IF(sc.s_score>=70 AND sc.s_score<85,1,0)) AS ‘[85-70]人数’,
SUM(IF(sc.s_score>=60 AND sc.s_score<70,1,0)) AS ‘[70-60]人数’,
SUM(IF(sc.s_score<60,1,0)) AS ‘[0-60]人数’,
CONCAT(ROUND(SUM(IF(sc.s_score>=85,1,0))/COUNT(*)100,2),‘%’) AS ‘[100-85]百分比’,
CONCAT(ROUND(SUM(IF(sc.s_score>=70 AND sc.s_score<85,1,0))/COUNT(
)100,2),‘%’) AS ‘[85-70]百分比’,
CONCAT(ROUND(SUM(IF(sc.s_score>=60 AND sc.s_score<70,1,0))/COUNT(
)100,2),‘%’) AS ‘[70-60]百分比’,
CONCAT(ROUND(SUM(IF(sc.s_score<60,1,0))/COUNT(
)*100,2),‘%’) AS ‘[0-60]百分比’
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id
GROUP BY sc.c_id

24 查询学生的平均成绩及名次

#本质 行聚合后排序
#方法 求平均成绩,后排序

SELECT s.s_name,ROUND(AVG(sc.s_score),2) AS ‘总成绩’,
ROW_NUMBER()OVER(ORDER BY AVG(sc.s_score) DESC) AS ‘排名’
FROM Student s
LEFT JOIN Score sc ON s.s_id=sc.s_id
GROUP BY s.s_name

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

#本质 行排序后筛选
#方法 各科成绩排序,取前3

SELECT r.c_name,r.rank_num,s.s_name,r.s_score
FROM
(SELECT c.c_name,sc.s_id,sc.s_score,
ROW_NUMBER()OVER(PARTITION BY c.c_name ORDER BY sc.s_score DESC) AS rank_num
FROM Course c
LEFT JOIN Score sc ON c.c_id=sc.c_id)r
JOIN Student s ON r.s_id=s.s_id AND r.rank_num<=3

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

#本质 行聚合
#方法 成绩表按科目对s_id计数

SELECT c.c_name,COUNT(DISTINCT sc.s_id) AS s_num
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id
GROUP BY c.c_name

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

#本质 行聚合
#方法 成绩表按学生对c_id计数

SELECT s.s_id,s.s_name
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
GROUP BY s.s_id
HAVING COUNT(DISTINCT sc.c_id)=2

28 查询男女生人数

#本质 行筛选聚合
#方法 学生表student按性别分组计数

SELECT s_sex,COUNT(DISTINCT s_id) AS ‘人数’
FROM Student
GROUP BY s_sex

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

#本质 模糊筛选
#方法

SELECT *
FROM Student
WHERE s_name LIKE ‘%风%’

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

#本质 分组聚合
#方法 分组计数后having筛选

SELECT s1.s_name,s1.s_sex,s2.num AS ‘同名人数’
FROM Student s1
JOIN (SELECT s_name,COUNT() AS num FROM Student GROUP BY s_name HAVING COUNT()>=2)s2 ON s1.s_name=s2.s_name
GROUP BY s1.s_name,s1.s_sex
HAVING COUNT(*)>=2

31 查询 1990 年出生的学生信息

#本质 行筛选
#方法 获取出生年份,并筛选
#备注 DATE()可以将varchar转成日期型 YEAR()函数返回一个指定日期or时间的年份值,范围为1000到9999,如果日期为零,YEAR()函数返回0

SELECT *
FROM Student
WHERE YEAR(DATE(s_birth))=1990

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

#本质 行聚合后排序
#方法 对课程求均值,后排序
#备注 order by后可跟聚合函数

SELECT c.c_name,ROUND(AVG(sc.s_score),2) AS ‘平均分’
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id
GROUP BY c.c_id
ORDER BY AVG(sc.s_score) DESC,c.c_id ASC

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

#本质 行聚合后筛选
#方法 对学生求成绩均值,后筛选

SELECT s.s_id,s.s_name,ROUND(AVG(sc.s_score),2) AS ‘平均成绩’
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
GROUP BY s.s_id
HAVING AVG(sc.s_score)>=85

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

#本质 行筛选
#方法 按要求筛选,用到子查询

SELECT s.s_name,sc.s_score
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
WHERE c_id=(SELECT c_id FROM Course WHERE c_name=‘数学’)
AND s_score<60

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

#本质 表关联后聚合
#方法 学生表与课程表笛卡尔积,获取所有学生及所有课程,然后关联成绩表获取成绩,最后聚合 注:成绩表为窄表,故外层需要聚合

SELECT s.s_name,
SUM(CASE c.c_name WHEN ‘语文’ THEN sc.s_score ELSE 0 END) AS ‘语文’,
SUM(CASE c.c_name WHEN ‘数学’ THEN sc.s_score ELSE 0 END) AS ‘数学’,
SUM(CASE c.c_name WHEN ‘英语’ THEN sc.s_score ELSE 0 END) AS ‘英语’,
SUM(sc.s_score) AS ‘总分’
FROM Student s
JOIN Course c
LEFT JOIN Score sc ON s.s_id=sc.s_id AND c.c_id=sc.c_id
GROUP BY s.s_name

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

#本质 行筛选
#方法 成绩表筛选>70

SELECT s.s_name,c.c_name,sc.s_score
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
JOIN Course c ON sc.s_id=c.c_id
WHERE s_score>70

37 查询不及格的课程

#本质 行筛选

SELECT sc.c_id,c.c_name,sc.s_score
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id
WHERE sc.s_score<60

38 查询课程编号为 01 且课程成绩大于等于 80 的学生的学号和姓名

#本质 行筛选

SELECT s.s_id,s.s_name
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
WHERE c_id=‘01’
AND s_score>=80

39 每门课程的学生人数

#本质 行聚合

SELECT c.c_name,COUNT(DISTINCT sc.s_id) AS ‘人数’
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id
GROUP BY c.c_name

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

#本质 行筛选后聚合
#方法 张三老师所授课程中,成绩最高(max、order by、row_number()over)的学生 1、max需要关联表匹配 2、order by和limit 1配合使用,推荐 3、row_number()over需要外层表限制rank_num=1

SELECT s.*,s_score
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
WHERE c_id=(SELECT c_id FROM Course WHERE t_id=(SELECT t_id FROM Teacher WHERE t_name=‘张三’))
ORDER BY s_score DESC
LIMIT 1

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

#本质 行筛选
#方法 符合条件的成绩:按成绩分组,对课程id计数且>2

SELECT *
FROM Score
WHERE s_score IN(SELECT s_score FROM Score GROUP BY s_score HAVING COUNT(DISTINCT c_id)>=2)

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

#本质 行排序后筛选
#方法 这里分组取前两名,需窗口函数row_number()over

SELECT r.*
FROM(
SELECT c_name,s_id,s_score,
ROW_NUMBER()OVER(PARTITION BY c_name ORDER BY s_score DESC) AS rank_num
FROM Score sc
JOIN Course c ON sc.c_id=c.c_id)r
WHERE r.rank_num<=2

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

#本质 行聚合后排序

SELECT c_id,COUNT(DISTINCT s_id) AS ‘人数’
FROM Score
GROUP BY c_id
ORDER BY ‘人数’ DESC,c_id ASC

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

#本质 行聚合后筛选

SELECT s_id
FROM Score
GROUP BY s_id
HAVING COUNT(DISTINCT c_id)>=2

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

#本质 行聚合后筛选

SELECT s.*
FROM Score sc
JOIN Student s ON sc.s_id=s.s_id
GROUP BY s.s_id
HAVING COUNT(DISTINCT c_id)=(SELECT COUNT(*) FROM Course)

46 查询各学生的年龄:按照出生日期来算,当前月日 < 出生年月的月日则,年龄减 1

#本质 行筛选:日期-年月日
#方法 年月日获取year()、month()、day()

SELECT *,
CASE
WHEN MONTH(NOW())<MONTH(DATE(s_birth)) THEN
YEAR(NOW())-YEAR(DATE(s_birth))-1
WHEN MONTH(NOW())=MONTH(DATE(s_birth)) AND DAY(NOW())<DAY(DATE(s_birth)) THEN
YEAR(NOW())-YEAR(DATE(s_birth))-1
ELSE
YEAR(NOW())-YEAR(DATE(s_birth))
END AS age
FROM Student

47 查询本周过生日的学生

#本质 行筛选:日期-周
#方法 周获取week()

SELECT *
FROM Student
WHERE WEEK(DATE(s_birth))=WEEK(NOW())

48 查询下周过生日的学生

#本质 行筛选:日期-周
#方法 周获取week()

SELECT *
FROM Student
WHERE WEEK(DATE(s_birth))=WEEK(NOW())+1

49 查询本月过生的同学

#本质 行筛选:日期-月

SELECT *
FROM Student
WHERE MONTH(DATE(s_birth))=MONTH(NOW())

50 查询下月过生的同学

#本质 行筛选:日期-月

SELECT *
FROM Student
WHERE MONTH(DATE(s_birth))=MONTH(NOW())+1

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

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

相关文章

【动手学深度学习】softmax回归的从零开始实现(PyTorch版本)(含源代码)

目录&#xff1a;softmax回归的从零开始实现一、理论基础1.1 前言1.2 分类问题1.3 网络架构1.4 全连接层的参数开销1.5 softmax运算1.6 小批量样本的矢量化1.7 损失函数1.7.1 对数似然1.7.2 softmax及其导数1.7.3 交叉熵损失1.8 信息论基础1.8.1 熵1.8.2 信息量1.8.3 重新审视交…

19 02-检索满足客户端定义的状态掩码的DTC列表

诊断协议那些事儿 诊断协议那些事儿专栏系列文章&#xff0c;19服务作为UDS中子功能最多的服务&#xff0c;一共有28种子功能&#xff0c;本文将介绍常用的19 02服务&#xff1a;根据状态掩码读取DTC列表。 关联文章&#xff1a; 19服务List 19 01-通过状态掩码读取DTC数目 …

详细教程。2022年滁州市明光市、来安县等各地区高新技术企业申报

安徽省大力鼓励企业申报高新技术企业&#xff0c;于高企申报也有很多奖补。滁州市企业申报奖补政策发布&#xff0c;企业可以根据自身情况申请奖补&#xff0c;奖补金额为10万元至30万元不等&#xff0c;明光市&#xff0c;凤阳县等各地区奖补申请可以通过市级机关办理。 下面小…

跟艾文学编程《Python数据可视化》(01)基于Plotly的动态可视化绘图

作者&#xff1a;艾文&#xff0c;计算机硕士学位&#xff0c;企业内训讲师和金牌面试官&#xff0c;公司资深算法专家&#xff0c;现就职BAT一线大厂。邮箱&#xff1a;1121025745qq.com博客&#xff1a;https://wenjie.blog.csdn.net/内容&#xff1a;跟艾文学编程《Python数…

2022-11-21 mysql列存储引擎-架构实现缺陷梳理-P2

摘要: 收集现有代码的糟糕实现&#xff0c;前事不忘后事之师&#xff0c;把这些烂东西定死在耻辱柱上以免再次发生 糟糕的设计: 一. DGMaterializedIterator::GetNextPackrow 函数实现: int DimensionGroupMaterialized::DGMaterializedIterator::GetNextPackrow(int dim, int…

【Linux系统】第一篇:基础指令篇

文章目录一、Linux中的文件二、Linux用户三、Linux基本指令ls指令pwd命令cd指令touch指令mkdir指令rmdir指令rm 指令man指令cp指令mv指令cat指令tac指令more指令less指令head指令tail指令管道重定向date指令cal指令find指令which指令alias指令whereis指令grep指令wc指令sort指令…

Node的web编程(二)

一、JSON数据 1、定义 JavaScript Object Notation&#xff0c;是一种轻量级的前后端数据交换的格式(数据格式)。 2、特点 &#xff08;1&#xff09;容易阅读和编写 &#xff08;2&#xff09;语言无关性 &#xff08;3&#xff09;便于编译、解析 3、语法要求 &#…

Mac m1配置flutter开发环境

Mac m1配置flutter开发环境 文章目录Mac m1配置flutter开发环境一、下载Android Studio二、下载flutter sdk三、新建flutter project四、使用在线环境进行Flutter开发Dart在线运行环境Flutter在线运行环境一、下载Android Studio 进入官网下载&#xff0c;选择苹果芯片版本。 …

【Spring(三)】熟练掌握Spring的使用

有关Spring的所有文章都收录于我的专栏&#xff1a;&#x1f449;Spring&#x1f448; 目录 一、前言 二、通过静态工厂获取对象 三、通过实例工厂获取对象 四、通过FactoryBean获取对象 五、Bean配置信息重用 六、Bean创建顺序 七、Bean对象的单例和多例 八、Bean的生命周期 九…

Weblogic SSRF 漏洞(CVE-2014-4210)分析

Weblogic SSRF 漏洞是一个比较经典的SSRF 漏洞案例&#xff0c;该漏洞存在于 http://127.0.0.1:7001/uddiexplorer/SearchPublicRegistries. jsp 页面中&#xff0c;如图 1-1 所示图 1-1 Weblogic SSRF 漏洞Weblogic SSRF 漏洞可以通过向服务端发送以下请求参数进行触发&#x…

ARFoundation系列讲解 - 70 HumanBodyTracking3D

---------------------------------------------- 视频教程来源于网络,侵权必删! --------------------------------------------- 一、简介 HumanBodyTracking3D(身体跟踪3D)案例,当设备检查到人体时,会返回检测到人体关节点的3D空间位置(需要在iOS 13或更高版本的A12…

瞪羚优化算法(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客 &#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜…

Java集合类——ArrayList(扩容机制)

线性表 线性表是n个相同类型元素的有限序列&#xff0c;逻辑上连续物理上不一定是连续的&#xff0c;存储结构上分为顺序存储和链式存储&#xff0c;常见的线性表有&#xff1a;顺序表&#xff0c;链表&#xff0c;栈&#xff0c;队列…… ArrayList 数据结构 ArrayList&am…

赋值运算符重载,取地址及const取地址操作符重载

赋值运算符重载1.运算符重载2.赋值运算符重载3.取地址及const取地址操作符重载如果一个类中什么成员都没有&#xff0c;那么该类简称为空类。而空类中其实并不是真的什么都没有&#xff0c;任何类在什么都不写时&#xff0c;编译器会自动生成以下6个默认成员函数。构造函数&…

同花顺_代码解析_技术指标_V,W

本文通过对同花顺中现成代码进行解析&#xff0c;用以了解同花顺相关策略设计的思想 目录 V&R VMA VMACD VOSC VPT VR VRFS VRSI VSTD W&R WVAD V&R 波动区间 用来衡量该股的市场波动风险.即95%的概率波动区间. 行号 1 n -> 250 2 x -> 收…

【考研英语语法】状语从句精讲

一、状语从句概述 &#xff08;一&#xff09;状语从句的含义 状语从句&#xff0c;指的就是一个句子作状语&#xff0c;表达“描述性的信息”&#xff0c;补充说明另一个句子&#xff08;主句&#xff09;。描述性的信息有很多种&#xff0c;可以描述时间、地点、原因、结果…

Web大学生网页成品HTML+CSS音乐吧 7页

⛵ 源码获取 文末联系 ✈ Web前端开发技术 描述 网页设计题材&#xff0c;DIVCSS 布局制作,HTMLCSS网页设计期末课程大作业 | 音乐网页设计 | 仿网易云音乐 | 各大音乐官网网页 | 明星音乐演唱会主题 | 爵士乐音乐 | 民族音乐 | 等网站的设计与制作 | HTML期末大学生网页设计作…

Django开发笔记

Django开发笔记Django学习1. Django安装path()函数2. 创建项目2.1 终端命令创建2.2 pycharm创建项目3. App4. 创建页面4.1 再写一个页面4.2 模板---Templates4.3 静态文件4.3.1 创建static目录4.3.2 静态文件的引用5. 模板语法案例&#xff1a;伪联通新闻中心6. 请求和响应案例…

KT148A语音芯片按键版本一对一触发播放常见的问题集锦FAQ_V4

1.1 有3个IO&#xff0c;都是一样的功能吗&#xff1f;从配置文件的说明来看&#xff0c;功能是键控发声&#xff0c;那么3个IO都只能是键控发声吗&#xff1f;还是可以有选择地某个IO对应播放那段语音&#xff1f;三个按键有什么区别&#xff1f;他们和语音号是如何对应的&…

[附源码]java毕业设计校园环境保护监督系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…