In-depth analysis of the differences between order by, group by, and having in Mysql

Source: Internet
Author: User

In English, order by is the sort of rows. The default value is ascending. Order by must be followed by multiple field names.
Group by is a group in English. You must have an aggregate function. At least one group flag field is required for use.

What is an aggregate function "?
For example, sum (), count (), and avg () are all "Aggregate functions"
The purpose of using group by is to classify and summarize data.

Generally, for example:
Select organization name, count (employee id), sum (employee salary) form [A Table]
Group by unit name
The result is that the number of employees and total wages of each unit are counted based on the "unit name.

In the order in which the SQL command format is used, group by is prior to order.

The standard format of 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 GROUP query. Generally, group by is used with Aggregate functions.
The principle of group by is that columns without Aggregate functions in all columns after select must appear after group by (important)

For example, there are the following database tables:
A B
1 abc
1 bcd
1 asdfg
If the following query statement is available (This statement is incorrect for the reason, see the previous principles)

Select A, B from table group by
 
The intention of this query statement is to get the following results (of course, it is just a willingness)
A B
Abc
1 bcd

Asdfg
How do the three entries on the right become one? So the aggregate function is required, as shown in the following (the correct syntax is as follows ):

Select A, count (B) as quantity from table group by
The result is
A quantity
1 3

2. Having
 
The where clause is used to remove rows that do not meet the where condition before grouping query results. That is, data is filtered before grouping. The condition cannot contain clustering functions, use the where condition to display specific rows.

The having clause is used to filter groups that meet the conditions. That is, data is filtered after the group. conditions often contain clustering functions and the having condition is used to display specific groups, you can also use multiple grouping standards for grouping.

The having clause is limited to columns and aggregate expressions defined in the SELECT statement. Generally, you need to repeat the aggregate function expression in the HAVING clause to reference the aggregate value, just as you did in the SELECT statement. For example:
Select a count (B) from table group by a having count (B)> 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.