select 查询、统计

目录

简单查询

查询多列数据,并自定义列名

order by asc/desc

top n

distinct

where 条件查询

like 配合 % 关键字

not

学习子查询 in

not in 

聚合函数

count  

max      

min      

sum      

avg        

统计汇总 : group by  ,   having

多表查询

 对表使用别名,来简化代码

公用表达式  cte

即席查询(分页查询)

综合练习

查询Employees表中的所有信息

查询Employees表中姓名包含‘华’的所有员工信息

查询Employees表中的所有员工信息,包括员工所属的部门名称。

统计各部门人数,写出SQL语句。

显示人数最多的部门的员工信息


学习类容 :select 查询、统计

  • 简单查询

  • select后跟 * 代表的是所有列,查询表中的所有记录。并显示所有的列名。

例子:

select * from A

结果:

  •  select 后想要查询多个列,多个列名之间用 , 分开。并会显示指定的列名。

例子:

select ID ,city ,heal from A  

结果:

  • 查询中掺杂表达式

例子:

select ID,city ,heal ,双倍的治愈人数=heal*2 from A -- 查询中掺杂表达式

结果:

  • 查询多列数据,并自定义列名

  • 查询多列数据,并自定义列名的几种方式

例子

select  id=ID  ,c=city  ,h=heal ,双倍的治愈人数=heal*2 from A --新列名=原始列名或表达式

select ID ID,city city,heal 双倍治愈人数 from A --原始列名或表达式  新列名

select ID as ID , city as 城市,heal as 治愈人数,heal *2 as 双倍治愈人数 from A --原始列名或表达式  as 新列名

结果:

 总结

  • 新列名=原始列名或表达式
  • 原始列名或表达式  新列名
  • 原始列名或表达式  as 新列名

  • order by asc/desc

  • asc 升序,desc 降序,默认排序为升序,所以asc关键字可以不写

例子:

select * from A order by ID asc --查询A表所有数据,并以ID列为升序进行排序 ,默认排序为升序,所以asc关键字可以不写
  • top n

  • top n 关键字查询符合条件的前n 条记录

例子

select top 10 * from A 
select top 4 ID as id ,city as 城市 from A order by ID asc 
  • distinct

  • distinct 关键字是筛选重复的值,用于返回唯一不同的值。注意的是 distinct 去重时,仅当所有列相同时才认为是重复值!!!!

例子:

-----distinct----distinct 关键字是筛选重复的值,用于返回唯一不同的值。----
select * from A where ID = 1
select distinct ID from A where  ID=1
select distinct ID from A 
select distinct top 4 ID from A 
select distinct top 4 ID , city from A

结果:

  •  仅当所有列相同时才认为是重复值!!!!

例子:

select * from AA
select distinct id,phone  from AA --注意的是 distinct 去重时,仅当所有列相同时才认为是重复值!!!!

可以看看:

  • where 条件查询

  • 常见的搜索条件:< , <=,  =, >= , >, and ,or ,between

例子:

--   =  ---
select * from A where ID=1

--    >  ---
select * from A where ID > 144

--   >=  --
select * from A where ID >= 144
select distinct ID as id ,city as 城市 from A  where ID >= 144 order by ID desc

查看:

 --- <  <= -----同上----


  • and ,or ,between 
  • 注意这里between是包含两端值的!!!

例子:

---- and -----
select * from move 
select * from move where avg_price > 40 and avg_people > 20
select * from move where avg_price > 40 and avg_people > 20 order by avg_people asc

----  or ----
select * from move where avg_people < 20 or avg_people > 45

--- between -----
select * from move where  avg_people between 20 and 30  --注意这里between是包含两端值的!!!

查询:

  • like 配合 % 关键字

  • like为模糊查询,% 为任意长度的任意字符串。

例子:

select * from move
select * from move where avg_people like '%30%'  --注意,%符号里有空格与没有空格是有区别的噢!!!
select * from move where avg_people like '%30%' or avg_people like  '%40%'   --or 的配合使用
select * from move where avg_people like '%30%' and  avg_people like  '%40%' 

运行其中一条:

  •  如果数据表里有为null 或是1个长度为0的字符串(空字符串)

如图:

查询:

select * from S  where name=''   --1个长度为0的字符串(空字符串)
select * from S where sum is null  --为null

结果:

  • not

-------- not -------------- 
select * from S 

select * from S where name is not null 
select * from S where sum is not null   --is not null
select * from S where sum  not like '%1%' --not like '%%'

结果:

  • 学习子查询 in

例子

-----学习子查询 in --------
select * from A
select * from move
select * from A where ID in (select  ID from move ) 
select top 10 * from A where ID in (select  ID from move ) 
select distinct * from A where ID in (select   ID from move where ID <5 ) 

结果:

  • not in 

select distinct * from A where ID not in (select   ID from move where ID <5 ) 

结果:


  • 聚合函数

------------- 聚合函数--------
--常用的聚合函数:----
--count   统计数据记录的个数
--max      获取最大值
--min        获取最小值
--sum       求和
--avg        求平均值
 

  • count  

  • 统计数据记录的个数,注意count记录的是所有不为空的数据,

例子:

select count(*)   from A  ---注意count记录的是所有不为空的数据,
select count(city)  as 总记录  from A
select count(avg_people) from move where avg_price=30 
select count(distinct avg_people) from move where avg_price=30 --对于有重复的可以用 distinct 进行去重

结果:

  • max      

  • 获取最大值
select max(EmployeeID) as 最大的员工ID号  from MM 
select * from MM where EmployeeID=(select max(EmployeeID) from MM) 

  • min      

  •  获取最小值
select min(EmployeeID) as 最小的员工ID号   from MM 
select * from MM where  EmployeeID=(select min(EmployeeID) from MM)

  • sum      

  • 求和
select  sum(EmployeeID) from MM

  • avg        

  • 求平均值
select avg(EmployeeID) from MM
  • 统计汇总 : group by  ,   having

  • group by 的作用是:通过一定的规则将一个数据集划分成若干个小的区域,然后针对若干个小区域进行数据处理。并且经常与聚合函数一起使用
  • group by:select指定的每一列都应该出现在group by中,除了对使用了聚合函数的某一列
  • having用于对聚合函数的筛选,可以与聚合函数一起使用。而where则不能与聚合函数一起使用。
select * from move 
select avg(EmployeeID) from MM group by EmployeeID
select name,avg(avg_people) from move group by name  
select  ID,avg(avg_people) from move group by ID having avg(avg_people) >25

运行其中一条语句:

 综合练习:

select ID ,name ,time,avg(avg_people) ,avg_price from move  --指定数据列
where avg_price > 40   --筛选不满足条件的数据行
group by ID ,name ,time, avg_price --对select指定的列进行分组
having avg(avg_people) > 20  ---对聚合函数进行筛选
order by avg_price asc  --以avg_price 进行排序

结果:

  • 多表查询

如图:3张表

  •  对表使用别名,来简化代码

----对表使用别名,来简化代码----
select m.*,q.name from MM m , move q 
where m.EmployeeID=q.ID

 综合练习:

select distinct  a.city,f.name ,cum=sum(f.avg_price*20)  from A a,move f
where  a.ID = f.ID
group by  a.city,f.name

  • 公用表达式  cte

---公用表达式  cte  -----
with cte as 
(
	select name,avg_people=count(*) from move group by name
)
select * from cte where name = '长津湖'
  • 即席查询(分页查询)

select * from phone order by ID 
offset 10 rows   --相对于首行偏移10行,相当于从第11行开始
fetch next 5 rows only   -- 下一步仅 5行数据

结果:

 

  • 综合练习

  • 查询Employees表中的所有信息

select * from Employeess 

结果:

  • 查询Employees表中姓名包含‘华’的所有员工信息

select * from Employeess where Employeess.EmployeeName like '%华%' 

结果:

  • 查询Employees表中的所有员工信息,包括员工所属的部门名称。

  • 要求查询结果如下图:

 代码:

select Employeess.* ,Departments.DepartName 
from Employeess,Departments 
where Employeess.DepartID=Departments.DepartID

结果:

  • 统计各部门人数,写出SQL语句。

代码:

select Departments.DepartID,DepartName ,count(Employeess.DepartID)as 人数 from Departments,Employeess where Employeess.DepartID=Departments.DepartID  
group by Departments.DepartID,DepartName

结果:

  • 显示人数最多的部门的员工信息

  • 如果财务部人数最多,将显示结果如下图

 代码:子查询,因为这两个表中以DepartID为关联,注意的是:all后边跟的是子查询

以这里的 >=  比较运算符为例,>=all 表示大于等于每一个值。换句话说,它表示大于等于最大值

--查询人数最多的部门的员工信息,DepartID,DepartName
select *from Departments 
where DepartID in
(
select DepartID from Employeess group by DepartID  
having count(*) >= ALL (select count(*) from Employeess   group by DepartID) --count(*)表所有列
 )  

结果:


 拜~~~~~~~~~~~~~

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抄代码抄错的小牛马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值