Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the order of execution in Sql?

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you what the order of execution in Sql is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Anyone who has studied Sql, or knows Sql, should write the following line of code:

Select * from t

The above code represents querying all the information in the t table, which is the most basic and simplest line of code in the Sql query. You can understand it as Hello World in other programming languages.

Select * is just your first step into the door of Sql. In real work, it must be more than that. Let's look at an example.

Now there is a table t below, which stores the transaction details of each commodity category. We need to use the following table to get the categories with orders greater than 10, and take out the top 3 commodity categories. There will be some tested orders (catid=c666 's for testing), which we need to filter out.

Catidorderidc11c12c13c24c25c36... C10010000

To do the above requirements, our Sql can write:

Select catid, count (orderid) as salesfrom twhere catid "c666" group by catidhaving count (orderid) > 10order by count (orderid) desclimit 3

The above Sql code involves seven keywords select, from, where, group by, having, order by, limit, which basically includes all the query keywords in Sql. The above order is the grammatical order of these seven keywords, that is, when you write the code, you should write in this order. What is the execution order of these seven keywords? That is, which one should be executed first and then which one?

To be sure, it is not carried out from top to bottom, and if so, there is no need to write this article.

One of the attitudes I have always adhered to is that computers are no different from people when doing things, and the basic logic and process are the same. After all, computers are also designed by people. In that case, let's see what we will do if we do the above requirement manually.

First of all, do I need to know which table I want to get what I want, that is, from;. Now I know which table to get it from, but not all the information in this table is what I need. I need to get rid of something I don't need (such as test orders), or filter out some of what I need. This is where. Now I filter out the order details I need, but I want the order quantity for each category. Is it necessary to do a grouping aggregation at this time, that is, the result after group by; grouping aggregation is not all we want? we only need categories greater than 10, so we need to filter out categories that are greater than 10, and those that are not greater than 10 need to be filtered out. This is having. Now that most of the information we want has come out, we can query them with select; because we finally need to take the top three categories, so we need to put the query results in a descending order, that is, the last step of order by; is to show only the first three and make a restriction, that is, limit.

The above is a basic execution order of Sql statements, which can be summarized as follows:

From-where-groupby-having-select-orderby-limit

The above is all the contents of the article "what is the order of execution in Sql?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report