Group by, order by, having, where

Source: Internet
Author: User

Original article: http://hi.baidu.com/%D6%EC%D5%F2%B3%C7/blog/item/d58a2ecc7ebd942cf9dc6121.html

 

Order by is used to sort data. By default, it is ASC in ascending order, and desc is used in descending order.


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 isUsed 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 (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's just a willingness)

A B
ABC
1 BCD

Asdfg

How to convert the three entries on the right into one, so an aggregate function is required, as shown below (Which of the following statements is correct?):

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

 

 

 

2. Having and where

Where: InBefore grouping query resultsRemove rows that do not meet the where condition, that is, filter data before grouping,The condition cannot contain clustering functions., Use the where ConditionShow specific rows.

Having is used to filter groups that meet the conditions, that isFilter data After grouping,Conditions often contain clustering functions., Using the having ConditionDisplay a specific group, 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

 

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.