Order of execution of keywords in SQL queries

Source: Internet
Author: User

1, the key words commonly used in the query are:

SELECT, from, WHERE, GROUP by, have, ORDER by

Where select and from are required, other keywords are optional.

2, the writing Order of these keywords:

SELECT Field List

From table name

WHERE Record Filter Criteria

Group BY Group Field List

Having group filter criteria

Order By Sort Field List

3, where and having the difference:

Where is the filtering of records, and having is a filter for groups after group by.

Having a group by can only be used after it has been used.

4. GROUP by two roles:

The first effect is to group the records taken from the table in one or more fields;

The second function is to go to the weight and its essence is still grouped.

5, the execution order of these keywords:

From->where->group By->having->select->order by

In particular, say:

From: Retrieving records from a specified data table

WHERE: Filters the retrieved records according to the specified criteria

GROUP BY: Groups The filtered records above by the specified criteria

Having: Filters all groups according to the specified criteria

SELECT: Extracts the field list for the specified query (including aggregated fields, calculated fields, expression fields, and so on) from each grouped record that was filtered in the previous step

ORDER BY: The result set from the previous query is sorted by the sorted field list, and the sort results are output

6, Group By field 1, field 2--first group all records according to Field 1, and then group the results after the group in the same field 2.

7. Order BY field 1, field 2--is sorted by field 1 first, and then by Field 2.

8. Collation Keyword: ASC (ascending, default), DESC (Descending).

9, Count keyword, used as a count. COUNT (*) is the overall count of the candidate datasets, and the Count (field) is the count of a field. If Shang appears with a null value, COUNT (*) counts into the column, and count (field) does not count to that column.

Reference: In the query statement, select from the where group by have order by order of execution, thank you.

Order of execution of keywords in SQL queries

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.