Anyone who has learned Sql or has known Sql should write the following line of code:
select * from t
The above code indicates that all the information in the query table is the most basic and simplest line of code in Sql query. You can understand it as Hello World in other programming languages.
select * is just your first step into the Sql gate. In real work, it is definitely more than that simple. Let's take a look at an example.
Now there is a table below t, which stores the transaction details of each product category. We need to obtain the corresponding categories with order volume greater than 10 through the table below, and take out the product categories with the top 3 order volumes from it. There will be some test orders (catid=c666 is the test), which we need to filter out.
catid | orderid |
---|---|
c1 | 1 |
c1 | 2 |
c1 | 3 |
c2 | 4 |
c2 | 5 |
c3 | 6 |
… | … |
c100 | 10000 |
To do the above requirements, our Sql can be written like this:
select catid, count(orderid) as sales from t where catid <> "c666" group by catid having count(orderid) > 10 order by count(orderid) desc limit 3
The above Sql code involvesselect、from、where、group by、having、order by、limit
These 7 keywords basically include all the query keywords in Sql. The above order is the syntax order of these 7 keywords, that is, when you write the code, you should write it in this order. So what is the execution order of these 7 keywords? That is, which one is executed first and which one is executed?
What is certain is that it is definitely not executed from top to bottom. If this is the case, there is no need to write this article.
One of the attitudes I have always insisted on isComputers are no different from people when doing things, with the basic logic and processes the same. After all, computers are also designed by people.So in this case, let’s take a look at what we would do if we manually do the above requirements.
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 I get it, but not all the information in this table is what I need. I need to remove some unwanted things (such as test orders), or filter out some I need, which is where; now I filter out the order details I need, but I want the order quantity of each category, at this time do I need to do a group aggregation, that is, group by; the results after group aggregation are not all of us. We only need to filter out categories greater than 10, and filter out categories not greater than 10, which is having; now most of the information we want has been released, and we can query them with select; because we finally need to take the first three categories, we need to sort the results we query in descending order, that is, order by; the last step is to only display the first three and just make a limit, that is, limit.
The above is a basic execution order of Sql statements. To summarize it, it is:
from-where-groupby-having-select-orderby-limit
This is the end of this article about the execution order of Sql. For more information about the execution order of Sql, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!