Do you know the logic execution sequence of SELECT statements?

Source: Internet
Author: User
Introduction: this is not a deep technical question, but a cool programming capability. This is not directly related to a person's development capabilities, but knowing this will be of great help to your SQL writing, troubleshooting and optimization. It is not a complex knowledge point, but a very basic SQL base. If you don't know this, you have been using ordinary cement.

Introduction: this is not a deep technical question, but a cool programming capability. This is not directly related to a person's development capabilities, but knowing this will be of great help to your SQL writing, troubleshooting and optimization. It is not a complex knowledge point, but a very basic SQL base. If you don't know this, you have been using ordinary cement.

Introduction: this is not a deep technical question, but a cool programming capability. This is not directly related to a person's development capabilities, but knowing this will be of great help to your SQL writing, troubleshooting and optimization. It is not a complex knowledge point, but a very basic SQL base. If you don't know this, you have been building houses with ordinary cement. If you know this, you are building houses with high cement.
However, it is such a small knowledge point that you can investigate colleagues and friends around you, maybe you will get a "surprise ".
As this article was written with a sudden sensation, the SQL statements written below were not tested.

Take a look at the following SQL statements: SELECT ID, COUNT (ID) AS TOTAL FROM STUDENT GROUP BY ID HAVING TOTAL> 2
Select id, COUNT (ID) as total from student group by count (ID) ORDER BY TOTAL
Select firstname + ''+ lastname as name, COUNT (*) AS COUNT FROM STUDENT GROUP BY NAME
Which one do you think NoCan be successfully executed? The following describes the logical execution sequence of the SELECT statement: fromonjoinwheregroup bywith cube or with rolluphavingselectdistinctorder bytop. MICROSOFT pointed out that the actual physical execution sequence of the SELECT statement may be different from that of the query processor.Example 1: select id, COUNT (ID) as total from student group by id having total> 2
Do you think this SQL statement is familiar? A very basic grouping query. But it cannot be executed successfully, because the order of HAVING execution is above SELECT. The actual execution order is AS follows: from studentgroup by idhaving total> 2 select id, COUNT (ID) as total is obvious, TOTAL is in the last select id, COUNT (ID) the new alias generated after the as total is executed. Therefore, TOTAL cannot be identified when having total> 2 is executed. Example 2 select id, COUNT (ID) as total from student group by count (ID) ORDER BY TOTAL
The actual execution order is: from studentgroup by count (ID) select id, COUNT (ID) as totalorder by total. This time there is no problem and the execution can be successful. Example 3 select firstname + ''+ lastname as name, COUNT (*) AS COUNT FROM STUDENT GROUP BY NAME
Actual execution sequence: from student group by name select firstname + ''+ lastname as name, COUNT (*) AS COUNT
Obviously, the alias NAME is not created when the group by name is executed, so it cannot be executed successfully. Summary: I recall that I have asked some people this question at will. No matter who says I don't know it, we will deliberately laugh at it. Of course, this is a joke. However, it turns out that some people still don't notice this knowledge point. It just serves as a friendly reminder.

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.