数据库查询(一)

简介: 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


交叉表查询

派生表查询

相关文章
|
20天前
|
数据库 JavaScript SQL
❤Nodejs 第八章(操作本地数据库优化查询为分页查询方式)
【4月更文挑战第8天】在Node.js中,本章讲述了如何优化本地数据库查询以实现分页。首先,添加了前端分页参数`pageNum`(页码)和`pageSize`(每页条数)。接着,通过打印`req.query`来验证参数是否正确传递。初始查询示例为`SELECT * FROM user WHERE age = 18 LIMIT 0, 10`。当改变分页参数时,查询能相应更新。在实现动态偏移量`offset`时,起初因误添加`' AND' : ' WHERE'`导致错误,修正后使用`LIMIT`和计算出的`offset`进行分页。
50 5
❤Nodejs 第八章(操作本地数据库优化查询为分页查询方式)
|
2天前
|
SQL 关系型数据库 MySQL
MySQL数据库的约束+进阶版新增与查询-2
MySQL数据库的约束+进阶版新增与查询
12 1
|
2天前
|
关系型数据库 MySQL 测试技术
MySQL数据库的约束+进阶版新增与查询-1
MySQL数据库的约束+进阶版新增与查询
12 1
|
5天前
|
SQL 数据库
SQL数据库基础语法-查询语句
SQL数据库基础语法-查询语句
|
5天前
|
存储 监控 Apache
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
网易的灵犀办公和云信利用 Apache Doris 改进了大规模日志和时序数据处理,取代了 Elasticsearch 和 InfluxDB。Doris 实现了更低的服务器资源消耗和更高的查询性能,相比 Elasticsearch,查询速度提升至少 11 倍,存储资源节省达 70%。Doris 的列式存储、高压缩比和倒排索引等功能,优化了日志和时序数据的存储与分析,降低了存储成本并提高了查询效率。在灵犀办公和云信的实际应用中,Doris 显示出显著的性能优势,成功应对了数据增长带来的挑战。
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
|
10天前
|
SQL 关系型数据库 MySQL
【MySQL-5】DDL的数据库操作:查询&创建&删除&使用(可cv代码+演示图)
【MySQL-5】DDL的数据库操作:查询&创建&删除&使用(可cv代码+演示图)
|
11天前
|
存储 SQL 缓存
构建高效的矢量数据库查询:查询语言与优化策略
【4月更文挑战第30天】本文探讨了构建高效矢量数据库查询的关键点,包括设计简洁、表达性强的查询语言,支持空间操作、函数及索引。查询优化策略涉及查询重写、索引优化、并行处理和缓存机制,以提升查询效率和准确性。这些方法对处理高维空间数据的应用至关重要,随着技术进步,矢量数据库查询系统将在更多领域得到应用。
|
11天前
|
SQL 缓存 监控
如何在数据库查询中使用参数化查询?
【4月更文挑战第30天】如何在数据库查询中使用参数化查询?
22 1
|
11天前
|
存储 SQL 关系型数据库
mysql查询数据库表大小怎么操作
mysql查询数据库表大小怎么操作
|
12天前
|
缓存 关系型数据库 MySQL
研优化数据库查询性能
研优化数据库查询性能
24 0