Use of SQL Server query statements

Source: Internet
Author: User
Tags format sql server query sql one table variables query table name
First, simple query
A simple Transact-SQL query includes only a picklist, a FROM clause, and a WHERE clause. They describe the query column, the table or view of the query, and the search criteria, and so on.
For example, the following statement queries the nickname field and the email field whose name is "John" in the TestTable table.
SELECT Nickname,email
From TestTable
WHERE name= John
(i) SELECT list
The select list (select_list) indicates the query column, which can be a list of column names, asterisks, expressions, variables (including local variables and global variables), and so on.
1. Select all Columns
For example, the following statement shows the data for all columns in the TestTable table:
SELECT *
From Testtable2, select partial columns, and specify their display order the data in the query result collection is the same as the column names specified in the select list.
For example:
SELECT Nickname,email
From Testtable3, change column headings in the select list, you can specify the column headings again. The format is defined as:
column heading = column name
Column Name title
If you specify a column heading that is not a standard identifier format, use a quotation mark delimiter, for example, the following statement uses Chinese characters to display column headings:
SELECT nickname =nickname, e-mail =email
From testtable4, deleting duplicate rows
The SELECT statement uses the all or distinct option to display all rows in the table that meet the criteria, or to delete duplicate rows of data, default to all. When you use the DISTINCT option, only one row is left in the result set returned by SELECT for all duplicate rows of data. 5, limit the number of rows returned
Use the top n [PERCENT] option to limit the number of rows returned, top N to return n rows, and Top n PERCENT to indicate that N is
Represents a percentage that specifies the number of rows to be returned equal to the number of total rows.
For example:
SELECT Top 2 *
From Testtableselect top PERCENT *
From TestTable
Two FROM clause
The FROM clause specifies the SELECT statement query and the table or view associated with the query. You can specify up to 256 tables or views in the FROM clause, separated by commas.
When you specify more than one table or view in the FROM clause, if the same column exists in the selection list, you should qualify the columns with the object name
The table or view to which it belongs. For example, Cityid columns exist in both usertable and citytable tables and should be qualified with the following statement format when querying the Cityid in two tables:
SELECT Username,citytable.cityid
From usertable,citytable
WHERE Usertable.cityid=citytable.cityid
You can specify an alias for a table or view in the FROM clause in the following two formats:
Table name as Alias
Table name aliases such as the preceding statement can be represented as the alias format of the table:
SELECT Username,b.cityid
From usertable a,citytable b
WHERE A.cityid=b.cityidselect Not only retrieves data from a table or view, it can also query data from a collection of results returned by other query statements.

Total 2 page: previous 1 [2] Next page



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.