SQL statement execution sequence, detailed explanation of the location and function of select, from, where, group by, having, order by statements

Note: Keywords and functions in SQL statements are not case-sensitive.

SQL statement execution sequence:

from-->where-->group by -->having --- >select --> order

The first step: from statement, select the table to be operated.

The second step: the where statement, set the filter conditions in the table after from, and filter out the records that meet the conditions.

The third step: group by statement, group the filtered records.

Step 4: Using the having statement, set conditions to filter the grouped data.

Step 5: select statement, select the result set after the above process.

Step 6: order by statement: display the selected result set in order.

Notice:

It is very important to master the execution process of SQL statements, which is the prerequisite for understanding and writing SQL statements. According to the actual business logic, the database operations to be executed correspond to the execution process of SQL statements, which can help us quickly write standard SQL statements with corresponding functions.

select:

  • Function: Select the result set.
  • Position: At the beginning of the SQL statement.

from:

  • Function: from is followed by the data table to be operated on.
  • Data table form: single table, multiple parallel tables, multiple join tables.
  • Position: after select.

where:

  • Function: Set conditions and filter records.
  • Position: after from.

Operators in where statements:

operator describe
= equal
<> not equal to. Note: != is also available in some versions.
> more than the
< less than
>= greater or equal to
<= less than
BETWEEN  within a certain range
LIKE match a pattern
IN among many possible values

Condition types in where statements:

logic operation:

  • AND: And, values ​​that satisfy multiple conditions at the same time.
  • OR: Or, at least one of the conditions must be met.

Special conditions:

  • IS NULL: Null value judgment.

  • BETWEEN: Values ​​between ranges.
  • IN: Among multiple possible values.
  • LIKE: Fuzzy query.

group by:

  • Role: used to combine aggregate functions, and feel that one or more columns group the result set.
  • Position: generally located after where or at the end of the SQL statement.

having:

  • Function: Filter each group of data after grouping, generally used in combination with aggregate functions.
  • Position: Generally located at the end of the SQL statement.

Commonly used aggregate functions:

function effect
sum(column name) Sum
max(column name) maximum value
min(column name) minimum value
avg (column name) average value
first(column name) first record
last(column name) last record
count(column name) Count the number of records Note: count(*) is the count of all records

order by:

  • Function: Used to sort the result set according to one or more columns.
  • Position: Generally located at the end of the SQL statement.

Sort by:

  • By default, records are sorted in ascending order. If you want to sort records in descending order, you need to add the DESC keyword.
  • When ordering by multiple columns, sort by the first column_name first, and then sort by the second column_name.

Sorting example:

order by A       # A升序排列
order by A desc,B   # A 降序(优先),B 升序排列
order by A ,B desc  #  A 升序(优先),B 降序排列

The difference between where and having:

  • where is before group by, and having is after group by.
  • Aggregate functions cannot be used in where, but aggregate functions can be used in having. Reason: Because the aggregation function is performed on the result set, but where is performed before querying the result set, the aggregation function cannot be used in where; having is used to filter the result set, so the aggregation function is generally placed in having.

Guess you like

Origin blog.csdn.net/qq_44973310/article/details/126986263