1. Simple query
A simple Transact-SQL query only includes the select list, FROM clause, and WHERE clause. They indicate the queried columns, the queried tables or views, and the search conditions.
For example, the following statement queries the nick name and email fields in the testtable table named "zhang san.
SELECT nickname, email
FROM testtable
WHERE name = ''zhang san''
(1) select a list
Select_list indicates the queried column, which can be a list of column names, asterisks, expressions, and variables (including local variables and global variables.
1. Select all columns
For example, the following statement shows the data of all columns in the testtable table:
SELECT *
FROM testtable
2. Select some columns and specify their display order
The data in the query result set is arranged in the same order as the column name in the selected list.
For example:
SELECT nickname, email
FROM testtable
3. Change the column title
In the selection list, you can re-specify the column title. Definition format:
Column title = column name
Column name column title
If the specified column title is not in the standard identifier format, use the quotation mark. For example, the following statement displays the column title in Chinese characters:
SELECT nickname = nickname, email = email
FROM testtable
4. Delete duplicate rows
Use the ALL or DISTINCT option in the SELECT statement to display ALL rows that meet the conditions in the table or delete duplicate data rows in the table. The default value is ALL. When the DISTINCT option is used, only one row is retained for all duplicate data rows in the result set returned by the SELECT statement.
5. Limit the number of returned rows
Use the TOP n [PERCENT] option to limit the number of returned data rows. TOP n indicates that n rows are returned, while TOP n indicates that the score is one hundred, specify the number of returned rows equal to a few percent of the total number of rows.
For example:
Select top 2 *
FROM testtable
Select top 20 PERCENT *
FROM testtable
(2) FROM Clause
The FROM clause specifies the tables or views related to the query of SELECT statements. Up to 256 tables or views can be specified in the FROM clause. They are separated by commas.
When the FROM clause specifies multiple tables or views at the same time, if the same column exists in the selected list, you should use the object name to limit the table or view to which these columns belong. For example, if the usertable and citytable tables contain the cityid column at the same time, the following statement format should be used to limit the cityid column when querying the two tables:
SELECT username, citytable. cityid
FROM usertable, citytable
WHERE usertable. cityid = citytable. cityid
You can specify aliases for tables or views in the FROM clause in the following two formats:
Table name as Alias
Table name alias
(2) FROM Clause
The FROM clause specifies the tables or views related to the query of SELECT statements. Up to 256 tables or views can be specified in the FROM clause. They are separated by commas.
When the FROM clause specifies multiple tables or views at the same time, if the same column exists in the selected list, you should use the object name to limit the table or view to which these columns belong. For example, if the usertable and citytable tables contain the cityid column at the same time, the following statement format should be used to limit the cityid column when querying the two tables:
SELECT username, citytable. cityid
FROM usertable,</