SQL article select from where group by have order by

Source: Internet
Author: User
Tags select from where


In the past, I always remember how to use GROUP BY, how to use order by, when to use Group by, when to use order by, when the two are used together, how to use, who first who, now, we say together Select from where groupby the things that have order by, simply summarize, deepen your impressions, and provide resources to those in need.

Select from where GroupBy has order by, Needless to say, the select from must be together, then the where, then the group by,having the premise is to group first, so with Group BY, which is the filter after grouping. Finally, the ploy data set Select field , and finally, the sorted data


So the order of execution is:

Wheregroup byhaving   <span style= "font-family:arial, Helvetica, Sans-serif;" >select    Order by</span>

1. Execute where XX to filter the whole table data, return to the 1th result set.

2. Use the group by group for the 1th result set to return the 2nd result set.
3. Perform a having XX for the 2nd collection to filter and return the 3rd result set.
4 executes select xx for every 1 sets of data in the 3rd result set, with several groups executing several times, returning the 4th result set.
5. Sorting for the 4th result set in fact, our SQL statement is mainly these and some of the aggregate functions, embedded method of what, in some cases when the statement, below a more image of the illustrated

Data Source:

First, a more alternative, simple, in-group sort

Select Studentname,lessonname,result from  testtable order Bystudentname,lessonname,result


The select Studentname,lessonname,result from TestTable is executed first and then sorted according to Studentname, based on the Lessonname sort, Based on the above, the ascending order is sorted according to result.

Another:

Select Studentname,sum (Result) Asresultfield from testtable where lessonname= ' language ' GROUP bystudentname having SUM (result) >7 ORDER by Resultfield


His execution order is to filter out the Lessonname= ' language first, according to the Studentname group, on the basis of filtering result of the sum of more than 7 data, and then from the data set Select Studentname,sum ( Result) as Resultfield, and then the final dataset is sorted in ascending order of total score size

Here, the others are quite simple, it is worth noting that the use of GROUP by:

Select column A, aggregate function (aggregate function specification) from indicates where filter condition group BY column A; Select column A, column B aggregate function (aggregate function specification) from indicates where filter condition group By column A, column B

in other words, we Select The fields shown here are the basis to be used for grouping, if:



Weight: Here are the differences between having and where:
1.having can only be used after group by to filter the results after grouping (i.e., the precondition for using having is to group).
2.where must be before group by, that is, before having it.
The aggregate function is not allowed in the conditional expression after 3.where, but has the ability.



SQL article select from where group by have order by

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.