SQL Server SELECT statement Basics

Source: Internet
Author: User

Later I found that this part was not completed, so I made a supplement to the SELECT statement-nested query and join query.

SQL Server statements are classified into three types:


Data Definition Language (DDL): used to define the data structure. Common statements include create, drop, and alter)
Data manipulation language (DML): used to retrieve and modify data structures. Common statements include insert, update, select, and delete)
Data Control Language (DCL): used to specify the permissions for database users. Common statements: (Grant, revoke)
The most important of these statements is the SELECT statement in the data manipulation statement. In the video from instructor Yan jianling, the SELECT statement is emphasized, the SELECT statement is described in detail in the last 90 minutes using the four sets. So what is the SELECT statement? See:The features and scope of the SELECT statement are needless to say. It is clear at a glance. Let's take a look at the following clauses ~~~~~Select statement:Structure: select [All/distinct] select_list [into [new_table_name] from {table_name/view_name} [[, {table_name2/view_name2 }[......, {view_name16/view_name16}] [where search_conditions] [group by group_by_list] [having search_conditions] [order by order_list [ASC/DESC]1) Select clause: Specifies the columns of the table returned by the query. Syntax: select [All/distinct] [Top N] <select_list> analysis: select_list indicates the list of fields to be retrieved. If select * is used, all fields in the data table are returned. All indicates that columns with the same display value can be displayed in the query results, and all is the default value. If the returned values of the distinct well-known query results have the same column, only one of the columns is returned. Top N specifies the first n items of data returned from the query results.2) into clause: Syntax for placing query results in a new table: into new_table analysis: new_table specifies the name of the new table. The columns of the new table are composed of columns specified by the Select clause, data rows are specified by the WHERE clause.3) from clause: Specifies the table to be queried. Syntax: from {<table_source>} [,... n] Analysis: table_source specifies the table, view, and other data sources used by the SELECT statement.4) WHERE clause: Specify the Data Query conditions to filter out unwanted data rows. Query conditions: Compare query conditions, including <,>, <=, >=, =, and ,! <,!> ,! = List query conditions, including in And notin query conditions, including like, notlike, and * (wildcard) null judgment conditions, including is null, is not null, and logical combinations that meet the preceding conditions: And, or, not5) group by clause: Specify the grouping conditions for the query results. Syntax: group by [all] group_by_expression [,... n] [With {cube/rollup}] Analysis: group_by_expression indicates the grouping conditions. It is usually a column name, but cannot be the column alias, restrict all data types of text, ntext, and image to return a combination of all possible query results (even if there is no WHERE clause in the combination). Note: All cannot be used together with cube and rollup. Difference between cube and Rollup6) Having clause: Specify the grouping search conditions, which are usually used together with the Group by clause. Syntax: Having <search_condition>, similar to the WHERE clause. Analysis: The WHERE clause acts on tables and views, and the having clause acts on groups.7) order by clause: Specifies the sorting method of the query results. Syntax: Order by {order_by_expression [ASC/DESC]} [,... n] Analysis: order_by_expression specifies the items selected by the sorting rule ASC in ascending order8) Compute clause: Generate a summary data row at the end of the query result. Syntax: Compute {AVG/count/max/MIN/SUM} (expression )}[,.... n] [by expression [,.... n] Analysis: expression specifies the name of the column to be counted. This column must be included in the select list and cannot be used as an alias. By expression generates the rows of classification statistics in the query results. To use this item, you must use the order by clause (order_by_name)9) union clause: Merge two or more query results into a result set. Syntax: select statement Union [all] select statement analysis: the Union clause deletes duplicate records from the final result set. If you want to keep all records, the all clause can only use the order by or compute clause in the last SELECT statement. Basic rule: The number and sequence of columns must be consistent with the data type in all queries.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.