当前位置:   article > 正文

sql50题带建表语句带详细答案(sql高阶函数详解)_create table course( 'id' varchar(2) primary key,

create table course( 'id' varchar(2) primary key, 'name' varchar(5), 'age' v

1. sql基础

1545658351688.png)

建表语句

--建表
--学生表
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

1.1 主表&从表关系

  • 主表中有的主键对应的列值,从表中可以没有和这个列值对应的数据,

    主表中没有的主键对应的列值,从表中一定不能有这个列值对应的数据

  • join连接关系
    在这里插入图片描述

  • 先分组,再排序
    在这里插入图片描述

1.2 mysql语句顺序

  • 查询书写格式

    select...(avg(),sum())from...left join...on...
    	where...group by...
    	order by...asc/desc
    	limit ...
    
    • 1
    • 2
    • 3
    • 4
  • 执行顺序

    form...on...left join...where...
    	group by...
    	avg(),sum()...having...
    	select...
    	order by...asc/desc
    	limit...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 列别名, 不要在条件中使用, 因为类别名在select后面
    • 表别名可以在条件中使用, from后面是表别名

在这里插入图片描述

1.3 window系统mysql运行远程访问

  • 截断表 truncate table xxx;

  • window系统mysql修改权限

    mysql -uroot -proot
    use mysql;
    create
    
    
    • 1
    • 2
    • 3
    • 4

2. sql50题详细答案

1. 查询"01"课程比"02"课程成绩高的学生的信息及课程分数

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 
SELECT a.*, b.s_score AS 01_score, c.s_score AS 02_score FROM 
	student a JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
	LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02' OR c.c_id = NULL WHERE b.s_score > c.s_score;
	
	-- 或者
	SELECT * (SELECT a.*, b.s_score AS 01_score, c.s_score AS 02_score FROM 
	student a JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
	LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02' OR c.c_id = NULL) d WHERE d.01_01_score > d.02_score;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.1 多表查询规律

  • 要先把学生表,成绩表关联起来—关键就是s_id 和 c_id
  • 学生表a join 成绩表b 得到学生的语文成绩, 再left join on 成绩表c 得到语文, 数学成绩完整的表
  • 再得到一张大表的基础上 语文成绩大于数学成绩

1.2 什么时候用left join , 什么时候不用

  • 成绩表score只是有单一的成绩, 姓名&学生编号会重复, student表–>score表是一对多
  • 如果需求是多科成绩之间的比较, 就需要student left join score
  • 如果不涉及多科, 就不需要left join了,
  • 涉及几科, student就left join几次score, 每次关联就取一科成绩

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

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

select a.* ,b.s_score as 01_score,c.s_score as 02_score from 
    student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL 
     join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
    student b 
    join score a on b.s_id = a.s_id
    GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60;
  • 1
  • 2
  • 3
  • 4
  • 5
  • sql查询语句结构
    在这里插入图片描述

  • sql语句执行顺序:

    • 无作为连接 先from–>where–>group by–>having–>avg , sum --> select–>order by

      有连接查询 form–>on–>join–>where–>group by–>avg,sum–>having–>select–>distinct–>order by–>limit
      在这里插入图片描述

  • having 后面跟的条件, 不要写select 中起的别名, 要写起别名之前的原名

    • 因为先group by xxx having xxx, 再select xxx起别名
  • 原表的别名, 后面都可以使用, 因为是先from 表名 别名

  • 根据平均成绩排序: order by 的条件,

    • 一定要写别名, 因为最后才排序的, 最后已经将查询结果揉成一张大表了,
    • 大表的字段就是select 后面的字段 , 所以就不认识原名了, 只认识别名了
  • 模糊查询

    • _ 代表一个
    • % 代表多个
    • rlike ‘[9]’ 含有9的
    • like关键字
  • 排序

    • order by xxx asc/desc : 全局排序, 只能有一个reduce
    • sort by xxx asc/desc: map端排序, 局部排序
    • distribute by 分区字段 + sort by 排序字段 asc/desc : 分区排序
    • cluster by 字段: 分区字段&排序字段相同时使用 --> 只能倒序,也就是降序

4. 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
        -- (包括有成绩的和无成绩的)
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
    student b 
    left join score a on b.s_id = a.s_id
    GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60
    union all
select a.s_id,a.s_name,0 as avg_score from 
    student a 
    where a.s_id not in (
                select distinct s_id from score);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • sql语句别名
    • 想添加一列并且给列赋值 select 0 as avg_score, 查出的数据后面添加一列avg_score,列值均为0
    • 想给原表中的一列起别名 select round(avg(a.s_score),2) as avg_score–> 平均值赋值给了avg_score(新列值)

以上原理是一样的, 就是可以在原有查询数据基础上,添加列明&列值

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

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score --4
	from student a ---1
    left join score b on a.s_id=b.s_id--2
    GROUP BY a.s_id,a.s_name;---3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 收获一定要注意思路和查询顺序
    • 先把两张表结合起来
    • 再分组,
    • 再求b.c_id的count 和b.s_score的sum
    • sum()是每个组中的s_score的和, count()是求每个组中c_id的个数

6. 查询"李"姓老师的数量

-- 6、查询"李"姓老师的数量
select count(t.t_id) from teacher t where t.t_name like '%李%' group by t.t_id;
  • 1
  • 2

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

-- 7、查询学过"张三"老师授课的同学的信息
SELECT stu.* FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id WHERE sco.c_id = (SELECT c.c_id FROM course c WHERE c.t_id=(SELECT t.t_id FROM teacher t WHERE t.t_name='张三'));
  • 1
  • 2

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

-- 8、查询没学过"张三"老师授课的同学的信息
SELECT s.* FROM student s WHERE s.s_id NOT IN 
(
	SELECT stu.s_id FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id 
	WHERE sco.c_id =(SELECT c.c_id FROM course c LEFT JOIN teacher t ON c.t_id=t.t_id WHERE 	t.t_name='张三') 
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT a.* FROM student a 
	LEFT JOIN score b ON a.s_id = b.s_id 
		LEFT JOIN score c ON a.s_id = c.s_id 
			WHERE b.c_id = '01' AND c.c_id ='02';
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息	
SELECT * FROM student WHERE s_id IN
	(SELECT s_id FROM score WHERE c_id = '01') AND
		s_id NOT IN (SELECT s_id FROM score WHERE c_id = '02')	
  • 1
  • 2
  • 3
  • 4

10.1 判断为null

where xxx is null 或者where xxx is not null

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

-- 11、查询没有学全所有课程的同学的信息
SELECT a.* FROM student a LEFT JOIN score b ON a.s_id = b.s_id GROUP BY a.s_id
	HAVING a.s_id NOT IN 
		(SELECT s_id AS cts FROM score GROUP BY s_id HAVING COUNT(*) = 3);
  • 1
  • 2
  • 3
  • 4

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

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT * FROM student WHERE s_id IN
(SELECT s_id FROM score WHERE c_id IN
	(SELECT c_id FROM score WHERE s_id = '01') GROUP BY s_id);
  • 1
  • 2
  • 3
  • 4

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

-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
SELECT * FROM student WHERE s_id IN
(SELECT a.s_id FROM 
	(SELECT s_id, COUNT(*) AS cts FROM score WHERE c_id IN
		(SELECT c_id FROM score WHERE s_id = '01') GROUP BY s_id
			HAVING cts = (SELECT COUNT(*) FROM score WHERE s_id = '01')) a WHERE a.s_id !='01')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 
SELECT s_name FROM student WHERE s_id NOT IN  
(SELECT s_id FROM score WHERE c_id =
	(SELECT c_id FROM course WHERE t_id = 
		(SELECT t_id FROM teacher WHERE t_name = '张三')));
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT a.s_id, a.s_name, AVG(b.s_score) FROM student a 
	LEFT JOIN score b ON a.s_id = b.s_id WHERE b.s_score < 60 OR b.s_score IS NULL
		GROUP BY a.s_id;		
  • 1
  • 2
  • 3
  • 4

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

-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息		
SELECT a.*, b.c_id, b.s_score FROM student a 
	LEFT JOIN score b ON a.s_id = b.s_id 
		WHERE b.c_id = '01' AND b.s_score < 60 
			ORDER BY b.s_score DESC;		
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 
SELECT tempA.*, tempB.avgs FROM 
	(SELECT a.*, b.s_score AS '01', c.s_score AS '02', d.s_score AS '03' FROM student a
			LEFT JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
				LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02'
					LEFT JOIN score d ON a.s_id = d.s_id AND d.c_id = '03') tempA LEFT JOIN	
    (SELECT a.s_id, ROUND(AVG(b.s_score),2) AS avgs FROM student a 
		LEFT JOIN score b ON a.s_id = b.s_id 
			GROUP BY a.s_id 
				ORDER BY avgs DESC) tempB ON tempA.s_id = tempB.s_id;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率         
//及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT s.c_id AS 课程ID, c.c_name AS 课程NAME, ROUND(MAX(s.s_score),2) AS 最高分, ROUND(MIN(s.s_score),2) AS 最低分, ROUND(AVG(s.s_score),2) AS 平均分,
	ROUND((SELECT SUM(CASE WHEN b.s_score >= 60 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
	(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 及格率,
	ROUND((SELECT SUM(CASE WHEN b.s_score >= 70 AND b.s_score < 80 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
	(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 中等率,
	ROUND((SELECT SUM(CASE WHEN b.s_score >= 80 AND b.s_score < 90 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
	(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 优良率,
	ROUND((SELECT SUM(CASE WHEN b.s_score >= 90 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
	(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 优秀率
	FROM score s LEFT JOIN course c ON s.c_id = c.c_id
	GROUP BY s.c_id;	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

18.1 myql流程控制函数之case

  • 基本语法

    select xxx, xxx, -- 一定要有逗号
    	case 
    		when 条件1 then 条件1查询结果对应的列值,
    		when 条件2 then 条件2查询结果对应的列值,
             else 不符合以上条件对应的列值,
             end 查询结果对应的列名 -- 一定不可以有逗号
        from 表名;     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • case when … else… end… 就是把查询结果单独赋值,单独赋列名

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

-- 19、按各科成绩进行排序,并显示排名
 -- 成绩相同不并列
SELECT ss01.*, @rank1 := @rank1 + 1 AS rank
	FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '01' ORDER BY s_score DESC) ss01,
	(SELECT @rank1 := 0) r
UNION ALL
SELECT ss02.*, @rank2 := @rank2 + 1 AS rank
	FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '02' ORDER BY s_score DESC) ss02,
	(SELECT @rank2 := 0) r
UNION ALL
SELECT ss03.*, @rank3 := @rank3 + 1 AS rank
	FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '03' ORDER BY s_score DESC) ss03,
	(SELECT @rank3 := 0) r;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

19.1 rank函数

  • 语法

    -- 1,2,3 顺序排名
    select @rank := @rank + 1 as rank from (select * from 表名 where xxx=xxx order by xxx desc) 表名, (select @rank := 0) r
    -- 1,1,2 并列排名
    select t1.*,
    		(case when @avgScore = t1.s_score then @rank 
    		      when @avgScore := t1.s_score THEN @rank := @rank+1 end) as rank
    		from (SELECT * FROM score WHERE c_id= '01' ORDER BY s_score DESC) t1,
    		(SELECT @rank := 0, @avgScore := 0)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • union all使用

  • order by & union all 使用

  • rank使用

19.2 :=和=区别

  • := 代表赋值,
  • = 代表判断

20. 查询学生的总成绩并进行排名 ?? 并列排名

-- 20、查询学生的总成绩并进行排名 ?? 并列排名
SELECT s.s_id, s.sums, @rank := @rank + 1 AS 总成绩排名
	FROM (SELECT s_id, SUM(s_score) AS sums FROM score GROUP BY s_id ORDER BY sums DESC) s,
	(SELECT @rank := 0) r
  • 1
  • 2
  • 3
  • 4

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

-- 21、查询不同老师所教不同课程平均分从高到低显示
SELECT c.t_id, c.c_id, c.c_name,t.t_name, AVG(s.s_score) AS avgs FROM score s 
	LEFT JOIN course c ON s.c_id = c.c_id
	LEFT JOIN teacher t ON c.t_id = t.t_id
	GROUP BY t.t_id
	ORDER BY avgs DESC
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

22. 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(采取的是1,1,2的方式进行排名)

--  22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(采取的是1,1,2的方式进行排名)
SELECT student.*,tt.c_id,tt.s_score,tt.rank FROM 
	(	
		SELECT t1.*,
		(CASE WHEN @avgScore = t1.s_score THEN @rank1 
		      WHEN @avgScore := t1.s_score THEN @rank1 := @rank1+1 END) AS rank 
		FROM (SELECT * FROM score WHERE c_id= '01' ORDER BY s_score DESC) t1,(SELECT @rank1:=0,@avgScore:=0)r
	UNION ALL
		SELECT t1.*,
		(CASE WHEN @avgScore = t1.s_score THEN @rank2 
		      WHEN @avgScore := t1.s_score THEN @rank2 := @rank2+1 END) AS rank 
		FROM (SELECT * FROM score WHERE c_id= '02' ORDER BY s_score DESC) t1,(SELECT @rank2:=0,@avgScore:=0)r
	UNION ALL
		SELECT t1.*, 
		(CASE WHEN @avgScore = t1.s_score THEN @rank3
		      WHEN @avgScore := t1.s_score THEN @rank3 := @rank3+1 END) AS rank 
		FROM (SELECT * FROM score WHERE c_id= '03' ORDER BY s_score DESC) t1,(SELECT @rank3:=0,@avgScore:=0)r	
	) tt LEFT JOIN student ON tt.s_id = student.s_id 
	WHERE tt.rank = 2 OR tt.rank = 3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT c.*,
	(SELECT COUNT(*) FROM score s WHERE s.s_score >= 85 AND s.s_score <= 100 AND s.c_id = c.c_id) AS `[100-85]`,
	CONCAT(
		ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 85 AND s.s_score <= 100 AND s.c_id = c.c_id)/
		(SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2),
	'%') AS '[100-85]占比',
	(SELECT COUNT(*) FROM score s WHERE s.s_score >= 70 AND s.s_score < 85 AND s.c_id = c.c_id) AS `[85-70]`,
	CONCAT(
		ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 70 AND s.s_score < 85 AND s.c_id = c.c_id)/
		(SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2),
	'%') AS '[85-70]占比',
	(SELECT COUNT(*) FROM score s WHERE s.s_score >= 60 AND s.s_score < 70 AND s.c_id = c.c_id) AS '[70-60]',	
	CONCAT(
		ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 60 AND s.s_score < 70 AND s.c_id = c.c_id)/
		(SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2),
	'%') AS '[70-60]占比',
	(SELECT COUNT(*) FROM score s WHERE s.s_score >= 0 AND s.s_score < 60 AND s.c_id = c.c_id) AS '[60-0]',	
	CONCAT(
		ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 0 AND s.s_score < 60 AND s.c_id = c.c_id)/
		(SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2),
	'%') AS '[60-0]占比'
	FROM course c;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

23.1 concat()函数

  • 字符串拼接函数

  • SELECT CONCAT (s_score,‘分’) as 成绩 FROM score;

    字符串拼接, 查出来的结果拼接一个字符串

24. 查询学生平均成绩及其名次 采取的是(1,1,2)的方式排序

-- 24、查询学生平均成绩及其名次 采取的是(1,1,2)的方式排序
SELECT temp.*, 
	(CASE WHEN @avgs = temp.avgs THEN @rank
	      WHEN @avgs := temp.avgs THEN @rank := @rank + 1 END) AS rank FROM 
	(SELECT s.s_id, ROUND(AVG(s.s_score),2) AS avgs FROM score s 
		LEFT JOIN student stu ON s.s_id = stu.s_id 
		GROUP BY s.s_id ORDER BY avgs DESC) temp,
	(SELECT @avgs := NULL, @rank := 0) r;	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • rank 112排名

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

-- 25、查询各科成绩前三名的记录 
select * from score a where 3>(select count(*) from score where a.c_id = c_id and s_score > a.s_score ) order by a.c_id,s_score desc
  • 1
  • 2

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

-- 26、查询每门课程被选修的学生数
SELECT s.c_id, COUNT(*) AS 选修人数 FROM score s GROUP BY s.c_id;
  • 1
  • 2

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

-- 27、查询出只有两门课程的全部学生的学号和姓名 
SELECT s.s_id, s.s_name FROM student s LEFT JOIN score sco ON s.s_id = sco.s_id
	GROUP BY s.s_id HAVING COUNT(*) = 2;
  • 1
  • 2
  • 3

28、查询男生、女生人数

-- 28、查询男生、女生人数
SELECT s.s_sex, COUNT(*) AS nums FROM student s GROUP BY s.s_sex
  • 1
  • 2

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

-- 29、查询名字中含有"风"字的学生信息
SELECT s.* FROM student s WHERE s.s_name LIKE '%风%'
  • 1
  • 2

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

-- 30、查询同名同性学生名单,并统计同名人数
SELECT s.s_name, COUNT(*) AS nums FROM student s GROUP BY s.s_name HAVING nums > 1
  • 1
  • 2

30.1 统计同名同姓学生名单:

  • 按照姓名分组,
  • 求count()
  • count() 大于1的就是同名同姓的人数

30.2 substr(列名 from n [for nums])

  • 截取字符串的函数
  • substr(列名 from 第几个字符 [for 要截取几个字符])

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

-- 31、查询1990年出生的学生名单
SELECT * FROM student WHERE SUBSTR(s_birth FROM 1 FOR 4) = '1990'
  • 1
  • 2

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

-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select s.c_id, round(avg(s.s_score),2) as avgs from score s group by s.c_id order by avgs desc, s.c_id asc;

  • 1
  • 2
  • 3
  • 32题: 注意, order by avgs 是正确的, order by s.avgs是错误的, group by s.c_id后就不能s.xxx

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

-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT stu.s_id, stu.s_name, ROUND(AVG(s.s_score),2) AS avgs FROM score s LEFT JOIN student stu
	ON s.s_id = stu.s_id
	GROUP BY s.s_id
	HAVING avgs >= 85
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 
SELECT stu.s_name, s.s_score FROM score s LEFT JOIN student stu 
	ON s.s_id = stu.s_id 
	WHERE s.c_id = (SELECT c_id FROM course WHERE c_name = '数学')
	AND s.s_score < 60	
  • 1
  • 2
  • 3
  • 4
  • 5

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

-- 35、查询所有学生的课程及分数情况;
SELECT stu.*, c.c_name, s.s_score FROM score s
	LEFT JOIN course c ON s.c_id = c.c_id
	LEFT JOIN student stu ON s.s_id = stu.s_id	
  • 1
  • 2
  • 3
  • 4

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

-- 36、查询任何一门课程成绩在70分以上的学生的姓名、课程名称和分数; 
SELECT stu.*, c.c_name, s.s_score FROM score s 
	LEFT JOIN course c ON s.c_id = c.c_id 
	LEFT JOIN student stu ON s.s_id = stu.s_id 
	WHERE s.s_score > 70
  • 1
  • 2
  • 3
  • 4
  • 5

37. 查询不及格的课程

-- 37、查询不及格的课程 
SELECT s.s_id, s.c_id, c.c_name,s.s_score FROM score s 
	LEFT JOIN course c ON s.c_id = c.c_id 
	WHERE s.s_score < 60
  • 1
  • 2
  • 3
  • 4

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

-- 38、查询课程编号为01且课程成绩大于等于80分的学生的学号和姓名;
SELECT stu.s_id, stu.s_name, s.c_id, s.s_score FROM score s 
	LEFT JOIN student stu ON s.s_id = stu.s_id
	WHERE s.c_id = '01' AND s.s_score >= 80	
  • 1
  • 2
  • 3
  • 4

39. 求每门课程的学生人数

-- 39、求每门课程的学生人数 
SELECT c_id, COUNT(s_id) AS cts FROM score GROUP BY c_id
  • 1
  • 2

40. 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩(可能包含相同成绩)

-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩(可能包含相同成绩)
SELECT stu.*, s.c_id, MAX(s.s_score) AS max_score FROM score s 
	LEFT JOIN student stu ON s.s_id = stu.s_id
	WHERE c_id = (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))_id	
  • 1
  • 2
  • 3
  • 4

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

-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
SELECT a.*
FROM score a 
LEFT JOIN score b
ON a.s_id = b.s_id
WHERE a.c_id <> b.c_id
AND a.s_score = b.s_score
GROUP BY a.c_id;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

-- 42、查询每门功成绩最好的前两名
SELECT 
temp.* 
FROM 
	(SELECT 
	s1.*, 
	@rank1 := @rank1 + 1 AS rank
	FROM (
		SELECT a.*
		FROM score a
		WHERE a.c_id = '01' ORDER BY a.s_score DESC) s1,
	(SELECT @rank1 := 0) r	
	UNION ALL
	SELECT 
	s2.*, 
	@rank2 := @rank2 + 1 AS rank
	FROM (
		SELECT a.*
		FROM score a
		WHERE a.c_id = '02' ORDER BY a.s_score DESC) s2,
	(SELECT @rank2 := 0) r
	UNION ALL
	SELECT 
	s3.*, 
	@rank3 := @rank3 + 1 AS rank
	FROM (
		SELECT a.*
		FROM score a
		WHERE a.c_id = '03' ORDER BY a.s_score DESC) s3,
	(SELECT @rank3 := 0) r) temp 
WHERE temp.rank = 1 OR temp.rank = 2;		
	
-- 简单方法, 不明白
SELECT 
* 
FROM score s1 
WHERE 2 > (SELECT COUNT(*) FROM score WHERE c_id = s1.c_id AND s_score > s1.s_score)
ORDER BY c_id, s_score DESC
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

43. 统计每门课程的学生选修人数(超过5人的课程才统计)

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。
	-- 要求输出课程号和选修人数,查询结果按人数降序排列,
	-- 若人数相同,按课程号升序排列 
SELECT
s.c_id,
COUNT(*) AS cts
FROM score s
GROUP BY s.c_id
HAVING cts > 5
ORDER BY cts DESC, s.c_id ASC;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

-- 44、检索至少选修两门课程的学生学号 
SELECT 
s.s_id,
COUNT(*) AS cts
FROM score s
GROUP BY s.s_id
HAVING cts >= 2;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

-- 45、查询选修了全部课程的学生信息 
SELECT
* 
FROM student 
WHERE s_id IN
	(SELECT 
	s.s_id
	FROM score s 
	GROUP BY s.s_id
	HAVING COUNT(*) = 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • month(日期类型的数据),可以取整–>比较大小&加减乘除
  • weekofyear(xxx), 一年中的第几周

46. 查询各学生的年龄

-- 46、查询各学生的年龄
    -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT 
*,
(CASE WHEN MONTH(s_birth) > MONTH(NOW()) THEN YEAR(NOW()) - YEAR(s_birth) -1
	WHEN MONTH(s_birth) < MONTH(NOW()) THEN YEAR(NOW()) - YEAR(s_birth)
	WHEN MONTH(s_birth) = MONTH(NOW()) AND DAY(s_birth) > DAY(NOW()) THEN YEAR(NOW()) - YEAR(s_birth) - 1
	WHEN MONTH(s_birth) = MONTH(NOW()) AND DAY(s_birth) < DAY(NOW()) THEN YEAR(NOW()) - YEAR(s_birth)
	ELSE NULL END) AS age
FROM student;		
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

47. 查询本周过生日的学生

-- 47、查询本周过生日的学生
SELECT 
*
FROM student
WHERE WEEKOFYEAR(s_birth) = WEEKOFYEAR(NOW())
  • 1
  • 2
  • 3
  • 4
  • 5

48. 查询下周过生日的学生

-- 48、查询下周过生日的学生
SELECT 
*
FROM student
WHERE (WEEKOFYEAR(s_birth) - WEEKOFYEAR(NOW()) = 1);
  • 1
  • 2
  • 3
  • 4
  • 5

49. 查询本月过生日的学生

-- 49、查询本月过生日的学生
SELECT 
*
FROM student
WHERE MONTH(s_birth) = MONTH(NOW());
  • 1
  • 2
  • 3
  • 4
  • 5

50. 查询下月过生日的学生

-- 50、查询下月过生日的学生
SELECT 
*
FROM student
WHERE (MONTH(s_birth) - MONTH(NOW()) = 1)
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/278527
推荐阅读
相关标签
  

闽ICP备14008679号