--SQL之DQL的学习
--1、基本查询语句。
--格式: select子句 from子句
-- select colName[,colName.......] from tableName;
--练习1:查询员工表emp中的 员工姓名,员工职位,员工入职日期和员工所在部门。
select ename,job,hiredate,deptno from emp;
--练习2:查询部门表中的所有信息
select * from dept;
select deptno,dname,loc from dept;
--2、给列起别名
--练习1:查询员工姓名和员工职位。分别起别名,姓名和职位。
select ename as "姓名" ,job as "职位" from emp;
--3、where子句
--作用:在增删改查时,起到条件限制的作用。
--练习1:查询员工表中部门号为10和20的员工的编号,姓名,职位,工资
select empno,ename,job,sal from emp where deptno=10 or deptno =20;
--练习2:查询员工表中部门号不是10和20的员工的所有信息。
select * from emp where deptno<>10 and deptno<>20;
--合作为条件写法:in|not in (集合元素,使用逗号分开);
-- 注意:同一个字段有多个值的情况下使用。
-- in 相当于 or
-- not in 相当于 and
--修改上面两个练习题
select empno,ename,job,sal from emp where deptno in(10,20);
select * from emp where deptno not in (10,20);
--all|any与集合连用:
--练习1:查询员工中工资大于集合(1500,1750,2000)中所有元素的员工信息
select * from emp where sal>all(select sal from emp where sal in (1500,1750,2000);
--练习2:查询员工中工资大于集合(1500,1750,2000)中任意一个元素的员工信息
select * from emp where sal>any(select sal from emp where sal in (1500,1750,2000);
--练习3:查询员工allen,blake,clark三个人的工资
select * from emp where sal>all(select sal from emp where ename in ('allen','blake','clark'));
--范围查询:colName between val1 and val2;
--练习1:查询工资大于等于1500并且小于等于2500的员工的所有信息。
select * from emp where sal>=1500 and sal<=2500;
--练习2:查询工资小于2000和工资大于2500的所有员工信息。
select * from emp where sal<2000 or sal>2500;
--修改上述两个练习
select * from emp where sal between 1500 and 2500;
select * from emp where sal not between 1500 and 2500;
--7.查询工资大于1250,有奖金的员工
select * from emp where sal>1250 and (comn!=0 or comn is not null);
--7.1查询工资小于2000,并且没有奖金的人的信息
select * from emp where sal<2000 and (comn =0 or comm is null);
--8.查询部门20和30,工资在1500-3000之间的员工信息
select * from emp where sal between 1500 and 3000 and (deptno=20 or deptno=30);
select * from emp where sal between 1500 and 3000 and deptno in(20,30);
--模糊查询:like
--通配符:% 表示0或0个以上字符。
-- _: 表示匹配一个字符
--格式: colName like value;
--练习1:查询员工姓名第二个字母为a或A的员工信息。
insert into emp (empno,ename,deptno)
values(9000,'james',40);
select ename,job,sal,comm,deptno from emp where ename like '_A%';
--练习2:查询员工姓名中有a和s的员工信息。
select ename,job,sal,comm,deptno from emp
where ename like '%a%' and ename like '%s%';
select ename,job,sal,comm,deptno from emp
where ename like '%a%s%' or ename like '%s%a%';
排序:
当在查询表中数据时,记录较多,有可能需要进行排序,此时可以使用 order by子句。
位置语法:
Select..from tableName[where 子句][order by 子句]
注意:可以通过一个或多个字段来排序
格式:order by colName [ASC|DESC][,colName1....[ASC|DESC]];
排序规则:ASC:升序
DESC:降序
默认是升序排序
--练习1.查询员工表中所有信息,按照部门号升序排序
select * from emp order by deptno ASC;
--练习2.查询员工的编号,姓名,工资,奖金,部门号,按照员工编号升序,部门号降序排序
select empno,ename,sal,comn,deptno from emp order by empno ASC,deptno DESC;
--练习3.查询30号部门的员工的姓名,工资,部门号,按照工资降序
select ename,sal,deptno from emp where deptno=30 order by sal DESC;
去重:有时我们需要对重复的记录进行去重操作.比如,查询表中有哪些职位,此时,一种职位只需要显示一条记录就够。
关键字:distinct
位置:必须写在select关键字后
--练习1.查看员工表中有哪些部门号
select distinct job from emp;
分组查询与分组函数(聚合函数)
有时我们可能需要查询表中记录总数,或者查询表中每个部门的总工资,平均工资,总人数。这种情况需要对表中的数据进行分组统计。需要group by子句
位置:
Select..from tName[where 条件] [group by 子句][order by 子句]
用法:group by filed1,[filed2]
注意:在分组查询时,select子句中的字段,除了聚合函数外,只能写分组字段。
--如果没有group by子句还使用了聚合函数就是分组查询,及整张表为一组。
聚合函数:
count(filed):统计指定字段的记录数
sum(filed):统计指定字段的和
avg(filed):统计指定字段的平均值
max(filed):统计指定字段的最大值
min(filed):统计指定字段中的最小值
PS:聚合函数会忽略null值
因此有时需要使用函数:ifnull();
函数ifnull(filed,value)
逻辑:如果field字段对应的值不是null,就使用filed的值,如果是null,就使用value.
--练习5:查询30号部门的总人数,平均奖金
Select count(*)“人数”,avg(ifnull(comn,0))from emp where deptno=30;
--练习6:查询每个部门的总人数,总工资,平均工资,最高工资,和最低工资(算平均时记得加ifnull)
select deptno, ctcount(*),sun(sal),avg(ifnull(sal,0)),max(sal),
min(sal)from emp group by deptno;
--练习7:查询每个部门的每种职位的平均工资,工资的和,
select deptno,job,avg(ifnull(sal,0)),sum(sal) from emp group by deptno,job order by deptno ASC;
多字段分组:
Filed1,filed2
10 500
20 600
30 700
注意:多字段分组时,最多分组的数目为filed1*filed2[*filed3..]
having子句:
在分组查询时,有的时候可能需要再次使用条件进过滤,这个时候不能where子句,应使用having子句。having子句后可以使用聚合函数
位置:位于group by子句后
--having子句:用于分组查询的再次过滤
--练习1:查询部门的平均工资大于2000的的部门号,平均工资,最高工资,最低工资和工资之和
select deptno,avg(ifnull(sal,0)),max(sal),min(sal),
sum(sal)from emp group by deptno having avg(ifnull(sal,0))>2000 order by deptno DESC;
子句总结:
一个基本的查询语句包含的子句有:
select子句from 子句where子句 group by 子句 having子句 order by子句
一个完整的查询语句:
Select ..from..[where..][group by..][having..][order by..]
执行顺序:
先执行from语句
再执行where子句
然后group by子句
再次having子句
之后select子句
最后order by子句