当前位置:网站首页>[Oracle database] mammy tutorial day03 Sorting Query
[Oracle database] mammy tutorial day03 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 .
边栏推荐
猜你喜欢

Mobile console Gobang (first draft of detailed design)

Leetcode-141. Linked List Cycle

webserver

Leetcode-104. Maximum Depth of Binary Tree

Biological sequence intelligent analysis platform blog (1)

@Jsonproperty annotation

CMAP of Matplotlib

No response from win10 explorer when dragging files

Nodejs database (Part 2)

webserver
随机推荐
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
MS office level II wrong question record [8]
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Leetcode-647. Palindromic Substrings
Analysis of key points and difficulties of ES6 promise source code
Typora set markdown syntax inline mode
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
Dynamically change the direction of this
Mobile console Gobang (first draft of detailed design)
213. house raiding II
**Count the characters with the largest number of words**
The gap between the parent box and the child box
Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
AtomicInteger原子操作类
Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
webserver
Object. Specific implementation and difference between create() and new
[STL source code analysis] summary notes (1): Opening
Niuke wrong question 3.1
Phi and phi (Mobius inversion + formula)