Home >Database >Mysql Tutorial >What is the order in which SQL keywords are executed?

What is the order in which SQL keywords are executed?

清浅
清浅Original
2019-04-09 10:59:4414447browse

The order in which SQL keywords are executed is: first execute the from statement; then the where statement, group by statement; then the having statement; and finally the order by statement.

What is the order in which SQL keywords are executed?

【Recommended course: MySQL Tutorial

sql keyword execution order

Example:

select m.* from(select t.*,rownum r from
(select id,name,e.username,e.realname from departments d ,employees e where d.manager=e.username(+)
order by id desc ) t where rownum<=? ) m where r>?

select m.*(select t.*,t.rownum r from(
select a,b,c,d from table1 order by a desc
) t where rownum < = ? ) m
where r > ?

The standard SQL parsing order is

(1) FROM clause, assemble data from different data sources

(2) WHERE clause, filter records based on specified conditions

(3) GROUP BY clause, combine The data is divided into multiple groups

(4) Use aggregate functions to calculate

(5) Use HAVING clause to filter groups

(6) Calculate all expressions

(7) Use ORDER BY to sort the result set

Example

In the student grade table (tentatively tb_Grade), Group the records with non-empty "candidate name" content according to "candidate name", and filter the grouping results to select the

whose "total score" is greater than 600 points. The SQL statement is:

 select 考生姓名, max(总成绩) as max总成绩
  from tb_Grade
  where 考生姓名 is not null
  group by 考生姓名
  having max(总成绩) > 600
  order by max总成绩

In the above example, the execution sequence of the SQL statements is as follows:

(1) First execute the FROM clause and assemble the data from the data source from the tb_Grade table

(2) Execute the WHERE clause, filter all data in the tb_Grade table that is not NULL

(3) Execute the GROUP BY clause, group the tb_Grade table by the "Student Name" column

(4) Calculate the max() aggregate function and find the largest values ​​in the total score according to "total score"

(5) Execute the HAVING clause and filter the courses whose total score is greater than 600 points.

(6) Execute the ORDER BY clause and sort the final results by "Max Score".

Summary: The above is the entire content of this article. I hope it will be useful to everyone. Helps.

The above is the detailed content of What is the order in which SQL keywords are executed?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn