SQL)
学会SQL语言的能干什么一个方向 ETL工程师ETL 英文名叫 Extract-Transform-Load 将数据从来源端经过抽取extract、转换transform、加载load⾄⽬的端的过程将各种来源比如服务器数据库文件。各种不同的数据结构各个业务部门内部外部 的杂乱无章的数据通过一系列的SQL及规范经过加工最后将结果汇总到显示需要的数据库的过程掌握SQL加上相关行业业务经验的积累就能成为一个合格的ETL工程师SQL语句SQL语言的分类总结 分为创建语句查询语句增删改语句控制用户权限的语句查询1.查询表中的所有列SELECT * FROM 表名1 , 表名2select * from student练习查询雇员表employee2.简单的查询查询表中的各个列SELECT 列名1列名2,... FROM 表名1 , 表名2,...select student_no,student_name from student练习查询部门表department列出各个字段3.按条件查询SELECT * FROM 表名1where列名1某个值 and(or ) 列名2某个值select student_no,student_name from student where student_no1练习查询部门表department id 2 的记录select student_no,student_name from student where student_no1练习查询部门表department id 1 的记录select student_no,student_name,student_sex from student where student_no1 and student_sex女练习查询部门表department id 1 的记录 并且部门名称后勤部的记录select student_no,student_name,student_sex from student where student_no1orstudent_sex女练习查询部门表department id 1 的记录或者部门名称贵州贵阳的记录select student_no,student_name from student wherenotstudent_no1练习查询部门表department id 不等于1 的记录select student_no,student_name from student where student_no!1between 最小值 and 最大值语法select * from 表名 where 列名 between 最小值 and 最大值select * from student where student_no between 1 and 3;练习查询部门表department id 在2,4之间 的记录 用betweenselect * from student where student_no1 and student_no3;select * from 表名 where 列名is null判断这一列有空值select * from student where money is null;in 取值范围语法select * from 表名 where 列名 in 值1值2…select * from student where student_no in (1,3)练习查询部门表department id 在2,4之间 的记录 inunion合并两个或多个select语句结果集(去重)select student_name from student where student_name like %民%unionselect student_name from student where student_name like %刚%union all合并两个或多个select语句结果集不去重select student_name from studentunion allselect student_name from studentselect * from student where student_name like %王%练习查询部门表department dept_name 包含 部 这个字的记录select * from student where student_name like 王%练习查询部门表department dept_name 以 研字开头 的记录为每一列指定别名 asSELECT 列名1 as b.dataFROM 表名1 as bselect * from student a where a.student_no1练习查询部门表department 为部门表起个别名 , 为department中的dept_name 起名为 nameselect count(*) from student练习 计算部门表department的行数实际应用如下图显示有多少条记录就用到了countselect sum(money) from student练习 计算部门表money 的总和select avg(money) from student练习 计算部门表money 的平均值select max(money) from student练习 计算部门表money 的最大值select min(money) from student练习 计算部门表money 的最小值min分组GROUP BY 关键字可以根据一个或多个字段对查询结果进行分组。GROUP BY子句必须出现在FROM和WHERE子句之后。 在GROUP BY关键字之后是一个以逗号分隔的列或表达式的列表这些是要用作为条件来对行进行分组。按照student_sex分组select student_sex from student a group by a.student_sexGROUP BY经常与聚合函数一起使用如SUMAVGMAXMIN和COUNT。SELECT子句中使用聚合函数来计算有关每个分组的信息。统计男女生人数select student_sex,count(*) from student agroup bya.student_sex练习统计员工表employee 男女生的总人数select a.sex,count(*) from employee a where 11 GROUP BY a.sex练习按班级分组求平均成绩select bj,avg(score) 平均成绩 from score_bj_student GROUP BY bj排序 order by按照student id倒序排列select * from student aorder bystudent_nodescselect * from student a order by student_no desc,money asc练习: department 按money倒序排列实际应用按照ID倒序排列ID是自增的所以按照ID倒序排列以后就能把新增的记录放在列表的最前边。练习 学生成绩表score_bj_student 里 按班级分组求平均成绩并按平均成绩倒序排列第一步分组select bj,avg(score) 平均成绩 from score_bj_student group by bj第二步排序select bj,avg(score) 平均成绩 from score_bj_student GROUP BY bj order by 平均成绩 descHAVING可使用HAVING子句过滤GROUP BY子句返回的分组统计性别大于1的数据select student_sex,count(*) from student a group by a.student_sexhavingcount(*)1练习 员工表employee 按性别分组计算分组结果数量1的记录练习 score_bj_student 找出平均成绩大于80分的班级第一步分组select bj,avg(score) 平均成绩 from score_bj_student GROUP BY bj第二部使用havingselect bj,avg(score) 平均成绩 from score_bj_student GROUP BY bj HAVING 平均成绩80limit关键字的用法LIMIT [offset,] rows offset指定要返回的第一行的偏移量rows第二个指定返回行的最大数目。初始行的偏移量是0(不是1)。1.m代表从m1条记录行开始检索n代表取出n条数据。(m可设为0)SELECT * FROM 表名 limit m,n如SELECT * FROM student limit 8,5;表示从第7条记录行开始算取出5条数据2.若只给出m则表示从第1条记录行开始算一共取出m条如SELECT * FROM student limit 6;主要用于分页练习找出平均成绩大于80的前三名班级select bj,avg(score) 平均成绩 from score_bj_student GROUP BY bj HAVING 平均成绩80 order by 平均成绩 desc LIMIT 3