The syntax structure of the select statement in SQL is: "SELECT select_list [ INTO..] FROM table_name [ WHERE...] [ GROUP BY...][ HAVING...][ ORDER BY... ];".
The SELECT statement consists of a flexible series of clauses that together determine which data is retrieved. In addition to viewing information about tables and views in ordinary databases, users can also view SQL Server system information by using the SELECT statement. Before introducing the use of the SELECT statement, it is necessary to introduce the basic syntax structure and execution process of the SELECT statement.
The syntactic structure of the SELECT statement
Although the complete syntax of the SELECT statement is more complex, its main clauses can be summarized as follows:
SELECT select_list [ INTO new_table] FROM table_name [ WHERE search_condition] [ GROUP BY group_by_expression] [ HAVING search_condition] [ ORDER BY order_expression [ ASC|DESC ] ]
The only required clauses are the SELECT clause and the FROM clause, and the other clauses are optional. The specific meaning of each clause is as follows:
— SELECT clause: Specifies the columns returned by the query.
— INTO clause: Store the search results in a new table or view.
— FROM clause: used to specify the table or view where the referenced column is located. If there is more than one object, they must be separated by commas.
— WHERE clause: Specify the search criteria used to limit the rows returned. If the SELECT statement does not have a WHERE clause, the DBMS assumes that all rows in the target table satisfy the search criteria.
— GROUP BY clause: Specify the group used to place the output rows, and if the SELECT clause
— HAVING clause: Specify the search conditions for the group or aggregation. HAVING is usually used with the GROUP BY clause. If you do not use the GROUP BY clause, HAVING behaves the same as the WHERE clause.
— ORDER BY clause: Specify the sorting of the result set. The ASC keyword indicates ascending order of results, and the DESC keyword indicates descending order of results. If no keyword is specified, ASC is the default keyword. Without the ORDER BY clause, the DBMS will display the data based on where the data is stored in the input table.
The order and function of each clause of SELECT
The order of clauses in the SELECT statement is very important. Optional clauses may be omitted, but they must appear in the appropriate order when used. Their order and main functions in the SELECT query statement are shown in Table 4.1.
Table 4.1 The order and function of each clause of the SELECT query statement
Sequence number | Clause keywords |
Clause function |
1 |
SELECT |
From the specified table Get the data of the specified column |
##2 |
FROM |
Specify the table to be queried |
3 |
WHERE |
Used to specify a criteria for selecting queries |
4 |
##GROUP BY |
Group the result set, often used with aggregate functions |
##5
|
HAVING
|
Returns the number of rows in the selected result set
|
6
|
ORDER BY
|
specified grouping Search conditions
|
Related recommendations: "
PHP Tutorial", "mysql Tutorial"
The above is the detailed content of What is the syntax structure of select statement in SQL?. For more information, please follow other related articles on the PHP Chinese website!