SQL Server Series: SELECT Basic statement

Source: Internet
Author: User

In T-SQL use the SELECT statement for data query, select syntax structure:

<SELECTStatement>::=      [With <common_table_expression> [,... n]]    <Query_expression>     [ORDER by {order_by_expression | column_position [ASC | DESC] }   [,... N] ]     [<for clause>]     [OPTION (<query_hint> [,... n] ) ] <Query_expression>::=     { <Query_specification> |(<Query_expression> ) }     [{UNION [ all] | EXCEPT | INTERSECT }        <Query_specification> |(<Query_expression>)[. .. n] ] <Query_specification>::= SELECT [All | DISTINCT]     [TOP (expression) [PERCENT] [With TIES] ]     <Select_list>     [Into new_table]     [From {<table_source>} [,... n] ]     [WHERE <search_condition>]     [<group by>]     [Having < search_condition >] 

1. Basic SELECT query

SELECT *  from [dbo]. [Product]

When you use the asterisk (*) wildcard character, all columns are returned, and the columns are displayed in the order in which they were defined.

2. Specify fields in the query record

When you don't need all of the field values in a datasheet, you can specify the name of the field you want to query, which not only shows the results clearer, but also improves the efficiency of the query.

SELECT [ProductID],[ProductName]from[dbo ]. [Product]

3. Using an expression in query results

 select  [    ,  [ productname   " ,  [ unitprice  ]  *  2  as   Doubleunitprice  from  [ dbo   ". [  ]

The AS keyword indicates that the expression specifies a field name for display, where as is an optional parameter, or it can not be used.

4. Display partial Query Results

When a data table contains a large amount of data, you can limit the number of rows returned in the result set by specifying the number of records to display.

SELECT TOP [n | PERCENT] from table_name

There are two optional arguments behind top, n means that the specified n rows are returned from the query result set, and percent represents the number of rows returned from the result set for the specified percentage.

 select  top  (10 ) *  from  [ dbo   ". [  ]
SELECT TOP (PERCENT*from[dbo]. [Product]

SQL Server Series: SELECT Basic statement

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.