当前位置:网站首页>【Oracle 数据库】奶妈式教程day03 排序查询
【Oracle 数据库】奶妈式教程day03 排序查询
2022-06-11 07:22:00 【菜鸟进阶站】
排序分组
1 排序
select 列名,列名,..,列名 from 表名 where 条件表达式 order by 排序列的列名 asc|desc;
asc|desc: asc表示升序,desc表示降序.
举例:查询员工信息,按照员工的工资升序排序
SELECT * FROM emp ORDER BY sal ASC; ---如果是升序排序,asc可以省略
SELECT * FROM emp ORDER BY sal;

举例:查询员工信息,按照员工编号降序排序
SELECT * FROM emp ORDER BY empno DESC;

order by :后面可以跟多个排序列,跟多个排序列时,它的含义:按照第一排序列(紧跟前order by 关键字的列)排序,如果第一个排序字段的值相同时,它会按照第二个排序字段进行排序。
举例:查询员工信息,按照部门编号升序排序,如果部门编号相同时,按照工资的升序排序
SELECT * FROM emp ORDER BY deptno ASC,sal ASC;

举例:查询员工信息,按照部门编号降序排序,如果部门编号相同,按照员工编号升序排序
SELECT * FROM emp ORDER BY deptno DESC,empno;

举例:查询员工的编号、姓名、工作、工资、部门编号,按照部门编号升序排序,如果部门相同时,按照工资降序排序
SELECT empno,ename,job,sal,deptno FROM emp ORDER BY deptno,sal DESC;

注意:order by 后面除了可以跟表中的列名外,还可以跟select 和from之间查询结果的序号
SELECT empno,ename,job,sal,deptno FROM emp ORDER BY 5,4 DESC;

SQL语句执行顺序
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
2 聚合函数
聚合函数:对一组值执行计算,并返回单个值。
count(列名|*|常数):它求记录数(数据条数)
max(列名):取最大值
min(列名):取最小值
avg(列名):取平均值
sum(列名):求和
举例:查询员工表中的员工人数
SELECT COUNT(empno) FROM emp;
SELECT COUNT(*) FROM emp;
SELECT COUNT(1) FROM emp;

count(主键列或索引列)
count(*)
count(常数)
count(普通列)
它们执行效率从上到下,依次降低。
null值不会参与 count计数。
举例:查询emp表中的部门数
select count(deptno) from emp;

举例:distinct:去重关键字,跟在列的最前面
select distinct deptno from emp;

select count(distinct deptno) from emp;

select empno,distinct deptno from emp; --这个sql语句是错误的

select distinct empno,deptno from emp;

注意:distinct后面跟多个列时,判断重复数据,所有列的值完全相同时,它才会认为是重复数据
举例:查询工资总和
select sum(sal) from emp;

举例:查询员工的平均工资
select avg(sal) from emp;

举例:查询10号部门的最高工资
select max(sal) from emp where deptno=10;

举例:查询最低工资
select min(sal) from emp;

3 分组
select 列名,列名,...,列名 from 表名
where 条件
group by 分组列
order by 排序列 asc|desc
group by:分组的关键字,后面跟分组列名,可以是一个分组列,也可以是多个列
举例:查询各个部门的部门编号和部门的平均工资
select deptno,avg(sal) from emp group by deptno;

举例:查询各个部门的员工人数
select deptno,count(empno) from emp group by deptno;

select job,deptno,count(*) from emp group by job,deptno;
注意:group by 后面跟多个列时,只有当多个列的值同时相等时,它才会分为同一个组;

4 HAVING
select col_name,col_name,...,col_name
from 表名
where 条件
group by 分组列
having 条件
order by 排序列
having:它是对分组后的数据进行筛选,条件表达式中可以使用聚合函数
举例:查询各个部门的部门编号和部门的平均工资
select deptno,avg(sal) from emp group by deptno;

举例:求平均工资大于2000的部门编号和平均工资
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

where和having的异同
where:条件,where后面跟的条件比having后的条件先执行,条件中不允许使用聚合函数。
having:条件中可以使用聚合函数,一般having和group by联用。
边栏推荐
- Library management system 1- project approval
- es5和es6的学习小记
- Leetcode-104. Maximum Depth of Binary Tree
- Calculate the day of the week for a specific month, year and day
- [advanced concurrency] - thread pool summary
- Concurrent tool class
- Listen to the left width of the browser to calculate the distance
- [STL source code analysis] summary notes (10): hashtable exploration
- Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
- 正则表达式匹配
猜你喜欢
![Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

The rotation of the earth and the moon (II)

【LeetCode】-- 17.电话号码的字母组合

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
![[STL source code analysis] summary notes (7): ingenious deque](/img/da/8ec42bfdbbf1b5bd1c2e396c2213e2.jpg)
[STL source code analysis] summary notes (7): ingenious deque

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration

Education expert wangzhongze shared his experience for many years: family education is not a vassal

Biological sequence intelligent analysis platform blog (1)

Niuke wrong question 3.1

QT interface nested movement based on qscrollarea
随机推荐
2022.5.30-6.5 AI行业周刊(第100期):三年时光
matplotlib的cmap
2022年熔化焊接与热切割考试练习题及答案
Leetcode-104. Maximum Depth of Binary Tree
421. maximum XOR value of two numbers in the array
Promise details
First day of database
Janus feature草稿
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
The difference between arrow function and ordinary function
Listen to the left width of the browser to calculate the distance
Dynamically change the direction of this
Records how cookies are carried in cross domain requests
213. house raiding II
Education expert Mr. wangzhongze: family education focuses on self growth
@Jsonproperty annotation
[analysis of STL source code] summary note (4): behind the scenes hero allocator
【CF#388 (Div. 2)】A. Bachgold Problem
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
Tetris preliminary