SQL select总结(基于选课系统)

news2024/10/5 19:13:47

表详情:

学生表:
请添加图片描述

学院表:
请添加图片描述

学生选课记录表:

请添加图片描述

课程表:

请添加图片描述

教师表:

请添加图片描述

查询:

1. 查全表

-- 01. 查询所有学生的所有信息
-- 方法一:会更复杂,进行了两次查询,第一次查询这个表的列,第二次根据查到的列再查每列的数据
select * from teachers;
-- 方法二:直接查询该实体所有属性
select stu_id
	, stu_name
    , stu_sex
    , stu_birth
    , stu_addr
    , col_id 
 from students;

2. 投影(select)和别名(alias / as)

-- 02. 查询学生的学号、姓名和籍贯(投影和别名)	-	别名:alias
select stu_id as 学号
	, stu_name as 姓名
    , stu_addr as 籍贯
 from students;

-- 03. 查询所有课程的名称及学分(投影和别名)
select cou_name as 名称
	 , cou_credit as 学分
  from courses;

3. 数据筛选(where 加 and / or 加 分支 加 比较符)

-- 04. 查询所有女学生的姓名和出生日期(数据筛选)
select stu_name
	 , stu_birth
  from students
 where stu_sex = 0;

-- 05. 查询籍贯为“四川成都”的女学生的姓名和出生日期(数据筛选)
select stu_name
	 , stu_birth
  from students
 where stu_sex = 0 and stu_addr = '四川成都';

-- 06. 查询籍贯为“四川成都”或者性别是女的学生(数据筛选)
select stu_name
	 , stu_birth
  from students
 where stu_sex = 0 or stu_addr = '四川成都';

-- 07. 查询所有80后学生的姓名、性别和出生日期(数据筛选)
select stu_name
  , stu_sex
  , stu_birth
from students
where stu_birth >= '1980-1-1' and stu_birth <= '1989-12-31';
  
select stu_name
   , stu_sex
   , stu_birth
from students
where stu_birth between '1980-1-1' and '1989-12-31';
   
-- 将01改为男女
	-- MySQL方言
	select stu_name as 姓名
		   , if(stu_sex,'男','女') as 性别
		   , stu_birth as 出生日期
		from students
	   where stu_birth between '1980-1-1' and '1989-12-31';
 
	-- 标准SQL
    select stu_name as 姓名
		 , case stu_sex when 1 then '男' when  0 then '女' else '未知' end as 性别
	  from students
	 where stu_birth between '1980-1-1' and '1989-12-31';
    
    
-- 08. 查询学分大于2分的课程名称和学分(数据筛选)
select cou_name
	 , cou_credit
  from courses
 where cou_credit > 2;
 
-- 09. 查询学分是奇数的课程的名称和学分(数据筛选)
select cou_name
	 , cou_credit
  from courses
 where cou_credit%2 <> 0;
 
-- 10. 查询选择选了1111的课程考试成绩在90分以上的学生学号(数据筛选)
select stu_id
  from records
 where cou_id = 1111 and score > 90;

-- 11. 查询名字叫“杨过”的学生的姓名和性别
select stu_name
	 , stu_sex
  from students
 where stu_name = '杨过';

4.模糊查询(通配符和正则)

-- 12. 查询姓“杨”的学生姓名和性别(模糊查询)
-- wild card - 通配符 - % - 零个或任意多个字符
select stu_name
	 , stu_sex
  from students
 where stu_name like '杨%';

-- 13. 查询姓“杨”名字两个字的学生姓名和性别(模糊查询)
-- wild card - 通配符 - _ - 精确匹配一个字符
select stu_name
	 , stu_sex
  from students
 where stu_name like '杨_';

-- 14. 查询姓“杨”名字三个字的学生姓名和性别(模糊查询)
select stu_name
	 , stu_sex
  from students
 where stu_name like '杨__';
 
-- 15. 查询名字中有“不”字或“嫣”字的学生的姓名(模糊查询)
select stu_name
  from students
 where stu_name like '%不%' or stu_name like '%嫣%';
 
 select stu_name
  from students
 where stu_name regexp '[\\u4e00-\\u9fa5]*?[不嫣][\\u4e00-\\u9fa5]*?';

select stu_name
  from students
 where stu_name like '%不%'
 union
select stu_name
  from students
 where stu_name like '%嫣%';
 
-- 16. 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
select stu_name
  from students
 where stu_name regexp '[杨林][\\u4e00-\\u9fa5]{2}';

5. 空值和去重(三值逻辑和trim)

-- 17. 查询没有录入籍贯的学生姓名(空值处理)
-- 三值逻辑 - true / false / unknown

select stu_name
  from students
 where stu_addr is null;
 
-- 如果遇到空字符串
-- trim - 修剪字符串左右两端指定的字符(默认修建空格)
update students set stu_addr = ' ' where stu_id = 3011;
 
select stu_name
  from students
 where stu_addr is null or trim(stu_addr) = '';

-- 18. 查询录入了籍贯的学生姓名(空值处理)
select stu_name
  from students
 where stu_addr is not null and trim(both ' ' from stu_addr) <> '';
 
-- 19. 查询学生选课的所有日期(去重)
select distinct sel_date
  from records ;
  
-- 20. 查询学生的籍贯(空值处理和去重)
select distinct stu_addr
  from students
 where stu_addr is not null and trim(stu_addr) <> '';

6. 排序

-- 21. 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- 排序 asc (默认)升序 desc 降序
select stu_name
	 , stu_birth
  from students
 where stu_sex = 1
order by stu_birth asc, stu_id desc;

7. 日期函数和数值函数

-- 查询现在的日期时间
	-- select current_timestamp();	2023-04-12 09:15:58
	-- select now();	2023-04-12 09:14:16
	-- select curdate();    2023-04-12
	-- select curtime();	09:15:24
-- 日期差
	-- datediff(大日期,小日期) 两个日期相差的天数
	-- timestampdiff(单位,小日期,大日期)
-- 22. 将上面查询中的生日换算成年龄(日期函数、数值函数)
select stu_name as 姓名
	 , datediff(curdate(),stu_birth) div 365 as 年龄
  from students
 where stu_sex = 1
order by stu_birth asc;

select stu_name as 姓名
	 , timestampdiff(year,stu_birth,curdate()) as 年龄
  from students
 where stu_sex = 1
order by stu_birth desc;

8. 聚合函数(最大/方差/平均…coalesce)

-- 有多条数据 最后只给出一条数据
-- 规约 reduce / aggregate 聚合
-- python中的规约: filter / map / reduce (max/min/sum/avg/count)

总体方差:
σ 2 = 1 N ∑ i = 1 N ( x i − μ ) 2 \sigma^{2} = \frac{1}{N} \sum_{i=1}^{N} (x_{i} - \mu)^{2} σ2=N1i=1N(xiμ)2
总体标准差:
σ = 1 N ∑ i = 1 N ( x i − μ ) 2 \sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_{i} - \mu)^{2}} σ=N1i=1N(xiμ)2
样本方差:
s 2 = 1 n − 1 ∑ i = 1 n ( x i − x ˉ ) 2 s^{2} = \frac{1}{n - 1} \sum_{i=1}^{n} (x_{i} - \bar{x})^{2} s2=n11i=1n(xixˉ)2
样本标准差:
s = 1 n − 1 ∑ i = 1 n ( x i − x ˉ ) 2 s = \sqrt{\frac{1}{n - 1} \sum_{i=1}^{n} (x_{i} - \bar{x})^{2}} s=n11i=1n(xixˉ)2

markdown写数学公式:

https://www.zybuluo.com/codeep/note/163962

-- SQL聚合函数:
-- max / min / avg / sum / count
-- stddev_pop(总体标准差)
-- stddev_samp(样本标准差)
-- var_pop(总体方差)
-- var_samp(样本方差)
-- 23. 查询年龄最大的学生的出生日期(聚合函数)
select min(stu_birth)
  from students;

-- 24. 查询年龄最小的学生的出生日期(聚合函数)
select max(stu_birth)
  from students;

-- 25. 查询编号为1111的课程考试成绩的最高分(聚合函数)
select max(score)
  from records
 where cou_id = 1111;
 
-- 26. 查询学号为1001的学生考试成绩的最低分(聚合函数)
select min(score)
  from records
 where cou_id = 1111;

-- 27. 查询学号为1001的学生考试成绩的平均分和标准差(聚合函数)
select avg(score)
	 , stddev_pop(score)
     , var_pop(score)
  from records
 where stu_id = 1001;

-- 28. 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分(聚合函数)
-- ifnull 是MySQL的方言
select avg(ifnull(score,0))
  from records
 where stu_id = 1001;
 
-- 标准SQL的函数 coalesce(值1,值2,值3,...) 返回第一个非空的值
select avg(coalesce(score,0))
  from records
 where stu_id = 1001;
 
-- count(*) 返回所有的行数
 select sum(score) / count(*)
   from records
  where stu_id = 1001;

9. 分组和聚合函数(having/rollup/取整)

-- 29. 查询男女学生的人数(分组和聚合函数)
select case stu_sex when 1 then '男' else '女' end as 性别
	 , count(*) as 人数
  from students
 group by stu_sex;

-- 30. 查询每个学院学生人数(分组和聚合函数)
-- with rollup 总计 rollup维度上卷
select col_id as 学院
	 , count(*) as 学生人数
  from students
 group by col_id
  with rollup;

-- 31. 查询每个学院男女学生人数(分组和聚合函数)
select col_id as 学院
	 , stu_sex as 性别
     , count(*) as 学生人数
  from students
 group by col_id, stu_sex
  with rollup;

-- 32. 查询选课学生的学号和平均成绩(分组和聚合函数)
-- round() 保留几位小数, ceil向大取整, floor向小取整
select stu_id
	 , round(avg(score),1)
	 , ceil(avg(score))
     , floor(avg(score))
  from records
 group by stu_id;

-- 33. 查询平均成绩大于等于90分的学生的学号和平均成绩(分组和聚合函数)
-- 分组之前的筛选数据用where子句;分组之后的筛选数据用having子句
select stu_id
	 , round(avg(score),1)
  from records
 group by stu_id
having avg(score) >= 90;
  
-- 34. 查询所有课程成绩大于80分的同学的学号(分组和聚合函数)
select stu_id
  from records
 group by stu_id
having min(score) > 80;

10. 嵌套查询(定义变量/any/all)

-- 35. 查询年龄最大的学生的姓名(嵌套查询)
-- 嵌套查询:把一个查询结果作为另外一个查询的一部分来使用,也称为子查询。
select min(stu_birth)
  from students;
select stu_name
  from students
 where stu_birth = '1985-04-17';

-- 定义变量:
	-- 赋值方法1:用户自定义变量前面要有@,给变量赋值前面要加set
	 set @min_birth = (select min(stu_birth) from students);
	-- 赋值方法2:海象运算符
	 select @min_birth := (select min(stu_birth) from students);
	 
	 select stu_name
	  from students
	 where stu_birth = @min_birth;

-- 一步到位
select stu_name
  from students
 where stu_birth = (select min(stu_birth) 
					  from students);

-- 36. 查询选了两门以上的课程的学生姓名(嵌套查询/分组/数据筛选)
-- 符合要求的学号
select stu_id
  from records
 group by stu_id
having count(score) > 2;

-- 通过学号查名字
select stu_name
  from students
 where stu_id in (select stu_id
					from records
				   group by stu_id
                  having count(*) > 2);
-- any 用any后的任意一个值和前面做对比
-- all 前面和后面的所有相等
select stu_name
  from students
 where stu_id = any (select stu_id
					   from records
				      group by stu_id
                     having count(*) > 2);

11. 连接查询(交叉连接/内连接/自然连接删除线格式 )

请添加图片描述

-- 37. 查询学生的姓名、生日和所在学院名称(连接查询)
-- from后面写多个表,是from后面表的笛卡尔积

-- 方法1:笛卡尔积
select stu_name
	 , stu_birth
     , col_name
     , students.col_id
     , colleges.col_id
  from students,colleges
 where students.col_id = colleges.col_id;

-- 方法2:cross join交叉连接
select stu_name
	 , stu_birth
     , col_name
     , students.col_id
     , colleges.col_id
  from students cross join colleges
 where students.col_id = colleges.col_id;

-- 方法3:内连接
select stu_name
	 , stu_birth
     , col_name
  from students inner join colleges
 where students.col_id = colleges.col_id;
 
-- 方法4:自然连接
-- 自然连接会根据两张表的同名列进行匹配
-- 自然连接没有同名列,同名列都合成一个列了
select stu_name
	 , stu_birth
     , col_name
  from students natural join colleges;

-- 38. 查询学生姓名、课程名称以及成绩(连接查询)
-- 笛卡尔积
select stu_name
	 , cou_name 
     , score
  from students, records, courses
 where students.stu_id = records.stu_id and records.cou_id = courses.cou_id;
 
-- 自然连接
select stu_name
	 , cou_name 
     , score
  from students natural join records natural join courses
 where score is not null;

12. 分页查询

-- 39. 上面的查询结果按课程和成绩排序取前5条数据(分页查询)
-- limit和offset是方言
select stu_name
	 , cou_name 
     , score
  from students natural join records natural join courses
 where score is not null
 order by cou_id asc, score desc
 limit 5;

-- 40. 上面的查询结果按课程和成绩排序取第6-10条数据(分页查询)
select stu_name
	 , cou_name 
     , score
  from students natural join records natural join courses
 where score is not null
 order by cou_id asc, score desc
 limit 5
offset 5;
 
-- 41. 上面的查询结果按课程和成绩排序取第11-15条数据(分页查询)
select stu_name
	 , cou_name 
     , score
  from students natural join records natural join courses
 where score is not null
 order by cou_id asc, score desc
 limit 5
offset 10;

13. 嵌套和连接查询

-- 42. 查询选课学生的姓名和平均成绩(嵌套查询和连接查询)
select stu_name
	 , avg_score
  from students
	   natural join (select stu_id
						  , round(avg(score),1) as avg_score
					   from records
					  group by stu_id) as temp;

-- 43. 查询学生的姓名和选课的数量(嵌套查询和连接查询)
select stu_name
	 , total1
     , total2
  from students
	   natural join (select stu_id
						  , count(*) as total1
                          , count(score) as total2
					   from records
					  group by stu_id) as temp;

14. 外连接

-- 外连接
	-- 左外连接(left outer join / left join):左表(写到join左边的表)取到所有的数据,不满足连表条件的地方填空值。
	-- 右外连接(right outer join / right join):右表(写到join右边的表)取到所有的数据,不满足连表条件的地方填空值。
    -- 全外连接(full outer join / full join):左右两张表都要取到所有的数据,不满足连表条件的地方填空值。(MySQL不支持)
		-- 可以通过左外连接union右外连接
-- 44. 查询每个学生的姓名和选课数量(左外连接和嵌套查询)
-- 选课表里没有她们的数据 自然连接和内连接不好用
select stu_name
	 , total1
     , total2
  from students
	   left join (select stu_id
					   , count(*) as total1
					   , count(score) as total2
					from records
				   group by stu_id) as temp
			on students.stu_id = temp.stu_id;

-- 45. 查询没有选课的学生的姓名(左外连接和数据筛选)
-- 查是哪几个没有选课(用空值填的)
select stu_name
	 , rec_id
  from students left join records
			on students.stu_id = records.stu_id;
-- 通过空值查没选课的名字
select stu_name
  from students left join records
			on students.stu_id = records.stu_id
 where rec_id is null;

SQL执行顺序

请添加图片描述

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

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

相关文章

C语言实现扫雷教学

本篇博客会讲解&#xff0c;如何使用C语言实现扫雷小游戏。 0.思路及准备工作 使用2个二维数组mine和show&#xff0c;分别来存储雷的位置信息和排查出来的雷的信息&#xff0c;前者隐藏&#xff0c;后者展示给玩家。假设盘面大小是99&#xff0c;这2个二维数组都要开大一圈…

JavaDS——数据结构易错选择题总结

1. 下列关于线性链表的叙述中&#xff0c;正确的是&#xff08; &#xff09; A. 各数据结点的存储空间可以不连续&#xff0c;但它们的存储顺序与逻辑顺序必须一致 B. 各数据结点的存储顺序与逻辑顺序可以不一致&#xff0c;但它们的存储空间必须连续 C. 进行插入与删除时&am…

【数据结构】-快速排序的四种方法实现以及优化

作者&#xff1a;小树苗渴望变成参天大树 作者宣言&#xff1a;认真写好每一篇博客 作者gitee:gitee 如 果 你 喜 欢 作 者 的 文 章 &#xff0c;就 给 作 者 点 点 关 注 吧&#xff01; 快速排序前言一、hoare法&#xff08;左右指针法&#xff09;二、挖坑法三、前后指针法…

Midjourney AI绘画工具使用保姆级教程

系列文章目录 之后补充 文章目录系列文章目录写在前面一、Midjourney是什么&#xff1f;二、使用步骤1.完成Discord注册2.打开Midjourney官网3.开始画图后记写在前面 据悉&#xff0c;自3月30日&#xff0c;Midjourney已叫停免费试用服务&#xff0c;如上图所示。 创始人表示原…

代码随想录_226翻转二叉树、101对称二叉树

leetcode 226. 翻转二叉树 ​​​226. 翻转二叉树 给你一棵二叉树的根节点 root &#xff0c;翻转这棵二叉树&#xff0c;并返回其根节点。 示例 1&#xff1a; 输入&#xff1a;root [4,2,7,1,3,6,9] 输出&#xff1a;[4,7,2,9,6,3,1]示例 2&#xff1a; 输入&#xff1a;r…

【Android开发】App Bundle技术之动态功能模块

前言 自 2021 年 8 月起&#xff0c;Google Play 将开始要求新应用使用 Android App Bundle 进行发布。该格式将取代 APK 作为标准发布格式。虽然这个政策目前还无法影响到国内应用&#xff0c;但是作为Android开发者&#xff0c;对于新的动态还是要有一定的认识。 Android A…

产品经理必读|用户研究方法总结②

随着互联网的发展&#xff0c;用户体验设计逐渐成为了产品设计中不可或缺的一部分&#xff0c;而要进行好的用户体验设计&#xff0c;就需要进行用研。但是&#xff0c;如何选用合适的用研方法&#xff0c;却是很多产品经理和设计师面临的难题。下面&#xff0c;就让我们来探讨…

3.3 泰勒公式

学习目标&#xff1a; 复习微积分基础知识。泰勒公式是微积分的一个重要应用&#xff0c;因此在学习泰勒公式之前&#xff0c;需要复习微积分的基本概念和技能&#xff0c;包括函数的导数和微分、极限、定积分等。可以参考MIT的微积分课程进行复习和加强。 学习泰勒级数和泰勒…

ftp传输文件大小有限制吗 ftp文件传输工具有哪些

这两年&#xff0c;线上办公逐渐常态化&#xff0c;相信大家对ftp这个概念也比较熟悉了。ftp&#xff0c;即文件传输协议&#xff0c;线上办公就是用ftp软件进行文件传输的。那ftp传输文件大小有限制吗,ftp文件传输工具有哪些我们一起来看看。 一、ftp传输文件大小有限制吗 f…

还在手动测试?那是那还不知道Python自动化测试的强大之处

目录&#xff1a;导读 引言 1.关于自动化测试的概述 2.Selenium元素定位实战 写在最后 引言 Python自动化测试是当今广泛使用的自动化测试技术之一。它的简单易学、开放源代码和丰富的第三方库使得其成为程序员和测试人员的首选工具之一。 Python自动化测试不仅可以帮助我…

【LeetCode: 剑指 Offer 60. n个骰子的点数 | 数学+ 暴力递归=>记忆化搜索=>动态规划】

&#x1f34e;作者简介&#xff1a;硕风和炜&#xff0c;CSDN-Java领域新星创作者&#x1f3c6;&#xff0c;保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文|经验分享|好用的网站工具分享&#x1f48e;&#x1f48e;&#x1f48e; &#x1f34e;座右…

git的操作使用三大块,常用的命令都在这里简单明了

码云网址&#xff1a;Gitee - 企业级 DevOps 研发效能平台 注册登录 创建仓库 仓库名称&#xff1a;必填&#xff0c;每个仓库都需要有一个名称&#xff0c;同一个码云账号下的仓库名称不能重复 路径&#xff1a;访问远程仓库时会使用到&#xff0c;一般无需手动指定&#xf…

C. Pinkie Pie Eats Patty-cakes(二分)

Problem - C - Codeforces 小粉饼买了一袋不同馅料的馅饼饼!但并不是所有的馅饼饼在馅料上都各不相同。换句话说&#xff0c;这个袋子里有一些馅料相同的馅饼。小粉派一个接一个地吃蛋糕。她喜欢玩&#xff0c;所以她决定不只是吃馅饼蛋糕&#xff0c;而是尽量不经常吃同样馅料…

Android 性能优化——ANR监控与解决

作者&#xff1a;Drummor 1 哪来的ANR ANR(Application Not responding):如果 Android 应用的界面线程处于阻塞状态的时间过长&#xff0c;会触发“应用无响应”(ANR) 错误。如果应用位于前台&#xff0c;系统会向用户显示一个对话框。ANR 对话框会为用户提供强制退出应用的选项…

Mybatis03学习笔记

目录 使用注解开发 设置事务自动提交 mybatis运行原理 注解CRUD lombok使用&#xff08;偷懒神器&#xff0c;大神都不建议使用&#xff09; 复杂查询环境&#xff08;多对一&#xff09; 复杂查询环境&#xff08;一对多&#xff09; 动态sql环境搭建 动态sql常用标签…

Unity Game FrameWork—模块使用—对象池分析

官方说明&#xff1a;提供对象缓存池的功能&#xff0c;避免频繁地创建和销毁各种游戏对象&#xff0c;提高游戏性能。除了 Game Framework 自身使用了对象池&#xff0c;用户还可以很方便地创建和管理自己的对象池。 下图是Demo中用到的对象池&#xff0c;所有的实体以及UI都使…

SpringBoot集成ChatGPT实现AI聊天

前言 ChatGPT已经组件放开了&#xff0c;现在都可以基于它写插件了。但是说实话我还真没想到可以用它干嘛&#xff0c;也许可以用它结合文字语音开发一个老人小孩需要的智能的说话陪伴啥的。 今天我就先分享下SpringBoot结合ChatGPT&#xff0c;先看看对话效果。 一、依…

清明-前端

明天面快手前端&#xff0c;正好借这个机会&#xff0c;做做毕设吧。顺便整理一下前端的面试内容。 何良蓉说&#xff0c;他觉得学的时候开心&#xff0c;玩的时候也开心。我觉得不开心。可能他掌握生活的秘密了吧。 如果他没对我撒谎的话。 看了眼别人的面经&#xff0c;就知…

一键禁用系统防火墙

你也可以通过批处理命令来实现 桌面空白地方右键选择新建记事本将下面代码复制到记事本里&#xff0c;然后保存为.bat类型的文件&#xff1b;保存完成运行即可。 Echo off Echo -------------------------------------------------------------------------- Echo 禁用系统防火…

【CSS】定位 ② ( 静态定位 | 相对定位 )

文章目录一、静态定位二、相对定位1、标准流下的盒子模型代码示例2、相对定位下的盒子模型代码示例一、静态定位 CSS 中的 静态定位 是 默认的定位方式 , 就是无定位 , 设置该定位方式 , 定位盒子不生效 ; 为盒子模型 设置 静态定位 模式 , 该 盒子模型 就会按照标准流的方式 …