数据库查询(一)

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


交叉表查询

派生表查询

相关文章
|
19天前
|
存储 数据库
Union All:数据库查询的得力助手
Union All:数据库查询的得力助手
|
6天前
|
SQL 数据库 Python
Django框架数据库ORM查询操作(6)
【7月更文挑战第6天】```markdown Django ORM常用数据库操作:1) 查询所有数据2) 根据ID查询 3) 精确查询 4) 分页排序
12 1
|
10天前
|
SQL 自然语言处理 网络协议
【Linux开发实战指南】基于TCP、进程数据结构与SQL数据库:构建在线云词典系统(含注册、登录、查询、历史记录管理功能及源码分享)
TCP(Transmission Control Protocol)连接是互联网上最常用的一种面向连接、可靠的、基于字节流的传输层通信协议。建立TCP连接需要经过著名的“三次握手”过程: 1. SYN(同步序列编号):客户端发送一个SYN包给服务器,并进入SYN_SEND状态,等待服务器确认。 2. SYN-ACK:服务器收到SYN包后,回应一个SYN-ACK(SYN+ACKnowledgment)包,告诉客户端其接收到了请求,并同意建立连接,此时服务器进入SYN_RECV状态。 3. ACK(确认字符):客户端收到服务器的SYN-ACK包后,发送一个ACK包给服务器,确认收到了服务器的确
|
13天前
|
存储 缓存 关系型数据库
如何优化数据库查询?
【7月更文挑战第2天】如何优化数据库查询?
18 1
|
14天前
|
SQL 关系型数据库 MySQL
MySQL数据库—DQL查询语句(一篇教会你快速找到想要的数据)
MySQL数据库—DQL查询语句(一篇教会你快速找到想要的数据)
|
16天前
|
存储 NoSQL MongoDB
mongdb如何查询数据库表的创建时间
【6月更文挑战第29天】mongdb如何查询数据库表的创建时间
20 2
|
9天前
|
SQL 存储 数据库
MSSQL数据库性能调优实战:索引、查询与并发控制的深度剖析
在数据库管理领域,Microsoft SQL Server(MSSQL)的性能调优是保障业务高效运行的核心任务
|
11天前
|
SQL 关系型数据库 MySQL
Navicate,数据库,Mysql,改表,4月29日Finished - Unsuccessfully,导出数据不妨,右键,备份一下Mysql数据库的内容,你想导入和导出数据不如,用查询的方式去做
Navicate,数据库,Mysql,改表,4月29日Finished - Unsuccessfully,导出数据不妨,右键,备份一下Mysql数据库的内容,你想导入和导出数据不如,用查询的方式去做
|
11天前
|
小程序 数据库
【微信小程序-原生开发】实用教程15 - 列表的排序、搜索(含云数据库常用查询条件的使用方法,t-search 组件的使用)
【微信小程序-原生开发】实用教程15 - 列表的排序、搜索(含云数据库常用查询条件的使用方法,t-search 组件的使用)
10 0
|
11天前
|
SQL Java 数据库连接
Java中实现优化的数据库访问和查询性能
Java中实现优化的数据库访问和查询性能