Order by is understood in English as the sort of row, and the default is ascending. The ordered by must be followed by a list of field names, which can be multiple field names.
Group By is understood in English as grouping. You must have an aggregate function to work with, and you need at least one group label field when you use it.
What is "aggregate function"?
Like sum (), COUNT (), AVG (), etc. are "aggregate functions"
The purpose of using group by IS to subtotal data.
generally like:
Select Unit name, COUNT (worker id), sum (Worker's salary) Form [table]
Group BY organization name
The result of this operation is to "unit name" as the classification of statistics units of the number of employees and total wages.
in the order in which the SQL command format is used, group by is preceded by.
the standard format for the Select command is as follows:
SELECT select_list
[Into new_table]
From Table_source
[WHERE search_condition]
[GROUP by Group_by_expression]
[Having search_condition]
1. Group BY is a grouped query, and general group by is used in conjunction with aggregate functions
Group by has a principle that all columns following the Select Do not have columns that use aggregate functions and must appear after group by (important)
For example, you have the following database tables:
A B
1 ABC
1 BCD
1 ASDFG
If you have the following query statement (the statement is wrong, for reasons see the previous principle)
Select A,b from table GROUP by A
The intent of the query statement is to get the following result (of course, just wishful)
A B
Abc
1 BCD
Asdfg
The right 3 how to become a, so you need to use the aggregate function, as follows (the following is the correct way):
Select A,count (B) as Quantity from table group by A
So the result is
A Quantity
1 3
2. Having
The role of the WHERE clause is to remove rows that do not conform to the where condition before grouping the query results, that is, to filter the data before grouping, in conditions that cannot contain a clustered function, and where conditions are used to display a particular row.
The HAVING clause is used to filter the groups that meet the criteria, that is, to filter the data after grouping, often including the clustering function, using the having condition to display a specific group, or multiple grouping criteria for grouping.
The HAVING clause is restricted to a column and an aggregate expression defined in the SELECT statement. Typically, you need to refer to an aggregate value by repeating the aggregate function expression in the HAVING clause, as you do in the SELECT statement. For example:
SELECT A count (b) from TABLE GROUP by A having COUNT (b) >2