数据库查询(一)

简介: SQL查询教程概览:介绍统计查询(如count(), avg()等,注意空值和distinct的区别)、分组查询(group by和having的应用)、子查询(非关联子查询和比较运算符的使用)及分页查询(limit用于结果限制)。还提及了union、intersect和except操作以及交叉表查询。

一、统计查询

count(属性名),max(属性名),min(属性名):  适用于:数值,字符,日期

avg(属性名),sum(属性名):只用于数值

  • 聚集函数统计时忽略空值
  • 统计时如果希望忽略重复值,则需要在属性前加distinct
  • where中不能使用聚集函数

1.工资的平均值

select avg(salary)

from emp

2.提成的平均值(自动会忽略空值)

select avg(comm_pct)

from emp

3.有多少种提成的值( count:查询属性种有多少个值,不是种,重复值也算)

select count(comm_pct)--错误

from emp


select count(distinct comm_pct)

from emp

4.count(*) : 统计结果有多少行

select count(*) from emp


二、分组查询 :

  • group by 属性名1,属性名2,.......
  • 当group by后有多个属性,表示将查询结果按照属性从左到右的顺序一次分组
  • group by 要写在where之后,order by 之前


1.每个部门的平均工资

select dept_id avg(salary)

from emp

group by dept_id


2.查询2号部门的平均工资

select dept_id avg(salary)

from emp

where dept_id=2;


   select dept_id avg(salary)

   from emp

   where dept_id=2;

如果select子句的属性列表中有聚集函数,则其他属性要么出现在gropy by中要么出现在聚集函数中


3.每个部分每个职位的平均工资

select dept_id,title,avg(salary)

from emp

group by dept_id,title

order by dept_id;


4.查询部门平均工资高于1000的部门及其平均工资

having:用于对分组的筛选,只能跟在group by之后不能单独使用,在分组之后完成筛选

where:用于对元组的筛选


select dept_id,avg(salary)

from emp

where avg(salary)>1500  错误

group by dept_id;



select dept_id,avg(salary)

from emp

group by dept_id

having avg(salary)>1500 ;



完整的select语句顺序写法:

select .....

from...

where

group by

having

order by


三、子查询


1.查询工资最低的员工

select *

from emp

where salary = min(salary) --错误  where 不能使用聚集函数


select *

from emp

where salary =(select min(salary) from emp)


子查询最好不用order by语句

非关联子查询:子查询先于父查询执行,只执行一次


2.查询职位和Molly相同的员工

select *

from emp

where title =(select title from emp where name='Molly')

and name <>'Molly' ×


select *

from emp e1,emp e2

where e1.title=e2.title and e2.name ='Molly' and e1.name<>'Molly'


当有两个Molly时,子查询会报错

=,>,<>=,<=,<>单值比较运算符只能引导返回值为单行的子查询


select *

from emp

where title in(select title from emp where name='Molly')

and name <>'Molly'


select e1.*

from emp e1 inner join emp e2

on e1.title=e2.title

where e2.name='Molly' and e1.name<>'Molly'


2.比2号部门所有员工工资都高的员工

select *

from emp

where salary >(select salary from emp where dept_id=2)   ×


第一种:

select *

from emp

where salary >all(select salary from emp where dept_id=2)


第二种:

select *

from emp

where salary >(select max(salary) from emp where dept_id=2)


3.谁是领导

select *

from emp

where id in select manager_id from emp


4.不是领导

select *

from emp

where id not in (select manager_id from emp) ×


select *

from emp

where id not in (select manager_id

                           from emp where manager_id is not null )


5.部门平均工资比2号部门平均工资高的部门编号和平均工资

select dept_id,avg(salary)

from emp

where avg(salary)>(select avg(salary) from emp where dept_id =2)

group by dept_id   ×


select dept_id,avg(salary)

from emp

group by dept_id

having avg(salary)>(select avg(salary) from emp where dept_id =2)



Union all:保留重复元组

Union:去除结果中重复元组

交:intersect

差:except/minus


分页查询

limit 写在order by之后

limit n,m

n表示从第几个开始取,下标从0开始。n值可以省略,默认表示开始从第0个开始取

m表示取多少个



查询工资最高的前10名:

select *

from emp

order by salary desc

limit 0,10


交叉表查询

派生表查询

相关文章
|
4月前
|
人工智能 安全 机器人
无代码革命:10分钟打造企业专属数据库查询AI机器人
随着数字化转型加速,企业对高效智能交互解决方案的需求日益增长。阿里云AppFlow推出的AI助手产品,借助创新网页集成技术,助力企业打造专业数据库查询助手。本文详细介绍通过三步流程将AI助手转化为数据库交互工具的核心优势与操作指南,包括全场景适配、智能渲染引擎及零代码配置等三大技术突破。同时提供Web集成与企业微信集成方案,帮助企业实现便捷部署与安全管理,提升内外部用户体验。
499 12
无代码革命:10分钟打造企业专属数据库查询AI机器人
|
6月前
|
Cloud Native 关系型数据库 分布式数据库
|
6月前
|
并行计算 关系型数据库 MySQL
如何用 esProc 将数据库表转储提速查询
当数据库查询因数据量大或繁忙变慢时,可借助 esProc 将数据导出为文件进行计算,大幅提升性能。以 MySQL 的 3000 万行订单数据为例,两个典型查询分别耗时 17.69s 和 63.22s。使用 esProc 转储为二进制行存文件 (btx) 或列存文件 (ctx),结合游标过滤与并行计算,性能显著提升。例如,ctx 并行计算将原查询时间缩短至 0.566s,TopN 运算提速达 30 倍。esProc 的简洁语法和高效文件格式,特别适合历史数据的复杂分析场景。
|
7月前
|
SQL 关系型数据库 MySQL
如何优化SQL查询以提高数据库性能?
这篇文章以生动的比喻介绍了优化SQL查询的重要性及方法。它首先将未优化的SQL查询比作在自助餐厅贪多嚼不烂的行为,强调了只获取必要数据的必要性。接着,文章详细讲解了四种优化策略:**精简选择**(避免使用`SELECT *`)、**专业筛选**(利用`WHERE`缩小范围)、**高效联接**(索引和限制数据量)以及**使用索引**(加速搜索)。此外,还探讨了如何避免N+1查询问题、使用分页限制结果、理解执行计划以及定期维护数据库健康。通过这些技巧,可以显著提升数据库性能,让查询更高效流畅。
|
7月前
|
数据库 Python
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
|
7月前
|
数据库
【YashanDB知识库】数据库用户所拥有的权限查询
【YashanDB知识库】数据库用户所拥有的权限查询
|
7月前
|
存储 运维 监控
百万指标,秒级查询,零宕机——时序数据库 TDengine 在 AIOps 中的硬核实战
本篇文章详细讲述了七云团队在运维平台中如何利用 TDengine 解决海量时序数据存储与查询的实际业务需求。内容涵盖了从数据库选型、方案落地到业务挑战及解决办法的完整过程,特别是分享了升级 TDengine 3.x 时的实战经验,给到有需要的小伙伴参考阅读。
240 1
|
7月前
|
缓存 NoSQL 关系型数据库
WordPress数据库查询缓存插件
这款插件通过将MySQL查询结果缓存至文件、Redis或Memcached,加速页面加载。它专为未登录用户优化,支持跨页面缓存,不影响其他功能,且可与其他缓存插件兼容。相比传统页面缓存,它仅缓存数据库查询结果,保留动态功能如阅读量更新。提供三种缓存方式选择,有效提升网站性能。
125 1
|
7月前
|
数据库

热门文章

最新文章