当前位置:网站首页>[Oracle database] mammy tutorial day04 Sorting Query
[Oracle database] mammy tutorial day04 Sorting Query
2022-06-11 07:23:00 【Rookie advanced station】
Sort groups
1 Sort
select Name , Name ,.., Name from Table name where Conditional expression order by Column name of sorting column asc|desc;
asc|desc: asc Expressing ascending order ,desc Representation of descending order .
give an example : Query employee information , Sort by employee's salary in ascending order
SELECT * FROM emp ORDER BY sal ASC; --- If it's in ascending order ,asc It can be omitted
SELECT * FROM emp ORDER BY sal;

give an example : Query employee information , Sort by employee number in descending order
SELECT * FROM emp ORDER BY empno DESC;

order by : It can be followed by multiple rows , With multiple sort Columns , What it means : In the first row ( Follow closely order by Columns of keywords ) Sort , If the value of the first sorting field is the same , It will sort by the second sort field .
give an example : Query employee information , Sort by department number in ascending order , If the Department numbers are the same , Sort by salary in ascending order
SELECT * FROM emp ORDER BY deptno ASC,sal ASC;

give an example : Query employee information , Sort by department number in descending order , If the Department numbers are the same , Sort by employee number in ascending order
SELECT * FROM emp ORDER BY deptno DESC,empno;

give an example : Check the employee's number 、 full name 、 Work 、 Wages 、 Department number , Sort by department number in ascending order , If the departments are the same , Sort by salary in descending order
SELECT empno,ename,job,sal,deptno FROM emp ORDER BY deptno,sal DESC;

Be careful :order by It can be followed by the column names in the table , You can follow select and from Sequence number of query results between
SELECT empno,ename,job,sal,deptno FROM emp ORDER BY 5,4 DESC;

SQL Statement execution order
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
2 Aggregate functions
Aggregate functions : Perform calculations on a set of values , And return a single value .
count( Name |*| constant ): It counts the number of records ( Number of data )
max( Name ): Taking the maximum
min( Name ): Minimum value
avg( Name ): Average.
sum( Name ): Sum up
give an example : Query the number of employees in the employee table
SELECT COUNT(empno) FROM emp;
SELECT COUNT(*) FROM emp;
SELECT COUNT(1) FROM emp;

count( Primary key column or index column )
count(*)
count( constant )
count( Common column )
They perform efficiently from top to bottom , Lower in turn .
null Value does not participate in count Count .
give an example : Inquire about emp Number of departments in the table
select count(deptno) from emp;

give an example :distinct: Remove keywords , Follow at the top of the column
select distinct deptno from emp;

select count(distinct deptno) from emp;

select empno,distinct deptno from emp; -- This sql The statement is wrong

select distinct empno,deptno from emp;

Be careful :distinct When followed by more than one column , Judge duplicate data , When the values of all columns are exactly the same , It will be considered as duplicate data
give an example : Check the total wage
select sum(sal) from emp;

give an example : Query the average salary of employees
select avg(sal) from emp;

give an example : Inquire about 10 The highest salary in department No
select max(sal) from emp where deptno=10;

give an example : Query minimum wage
select min(sal) from emp;

3 grouping
select Name , Name ,..., Name from Table name
where Conditions
group by Group columns
order by Sort columns asc|desc
group by: Grouped keywords , Followed by the group column name , It can be a grouping column , It can also be multiple columns
give an example : Query the department number and average salary of each department
select deptno,avg(sal) from emp group by deptno;

give an example : Query the number of employees in each department
select deptno,count(empno) from emp group by deptno;

select job,deptno,count(*) from emp group by job,deptno;
Be careful :group by When followed by more than one column , Only when the values of multiple columns are equal at the same time , It will be divided into the same group ;

4 HAVING
select col_name,col_name,...,col_name
from Table name
where Conditions
group by Group columns
having Conditions
order by Sort columns
having: It is used to filter the grouped data , Aggregate functions can be used in conditional expressions
give an example : Query the department number and average salary of each department
select deptno,avg(sal) from emp group by deptno;

give an example : The average wage is greater than 2000 Department number and average wage of
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

where and having Similarities and differences
where: Conditions ,where The following conditions are better than having The latter condition is executed first , Aggregate functions... Are not allowed in conditions .
having: You can use aggregate functions in conditions , commonly having and group by In combination with .
边栏推荐
- Promise. All capture error
- [analysis of STL source code] summary notes (3): vector introduction
- Nodejs database (Part 2)
- @JsonProperty注解
- 1300. the array closest to the target value after transforming the array and
- 【CF#388 (Div. 2)】A. Bachgold Problem
- Create a form whose client area is 800 pixels by 600 pixels
- Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
- If you want to save an IP address, what data type is better? 99% of people will answer wrong!
- R语言并行计算实战教程
猜你喜欢

Mobile console Gobang (first draft of detailed design)

Leetcode-104. Maximum Depth of Binary Tree

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
![[STL source code analysis] summary note (2): overview of containers](/img/66/60fba564ae6020dfb503c7fdf78529.jpg)
[STL source code analysis] summary note (2): overview of containers

二、用户登录和注册

Explain the difference between void 0 and undefined

Leetcode-141. Linked List Cycle

2022 low voltage electrician operation certificate test question simulation test platform operation

Analysis of key points and difficulties of ES6 promise source code

Decimal to binary
随机推荐
213. house raiding II
Group arrays by a specified size
Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
gaussDB for redis和 redis的区别?
P3172 [cqoi2015] data selection (Mobius inversion + Du Jiao sieve)
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
Server parameter adjustment record
Concurrent tool class
Leetcode-104. Maximum Depth of Binary Tree
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
JVM学习记录(七)——类加载过程与双亲委派模型
自动化测试的生命周期是什么?
[STL source code analysis] summary note (2): overview of containers
【CF#262 (Div. 2)】 A. Vasya and Socks
MS office level II wrong question record [4]
Interview question 17.08 Circus tower
Console program for viewing BMP files
Use definite integral to calculate triangle area
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
2022低压电工操作证考试题模拟考试平台操作