Print

How to Retrieve Data with Select Statement - Select Syntax

SQL Statement retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables.


SELECT statement ::= [WITH <common_table_expression> [,...n]] <query_expression> [ ORDER BY { order_by_expression | column_position [ ASC | DESC ] } [ ,...n ] ] [ COMPUTE { { AVG | COUNT | MAX | MIN | SUM } (expression )} [ ,...n ] [ BY expression [ ,...n ] ] ] [ ] [ 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> ] [ ] [ HAVING < search_condition > ]

Explanation of Syntax

Syntax Element Arguments Usage
With common table expression expression name, column name, as sql select statement Specifies a temporary named result set, known as a common table expression (CET)
Select Clause

ALL, DISTINCT, TOP, Column name, Table name, $ROWGUID,

Column list, expression, Column alias

Specifies the column to be returned by the query
Into Clause new_table, #table name, ##table name Creates a new table in the database, insert the resulting rows in to a table or temp table
From Clause table or view name Specifies the tables, views, derived tables, and joined tables
WHERE search condition Specifies search conditions for the rows to be returned by the query
GROUP BY column expression

Groups a selected set of rows into a set of summary rows by the values of one or more columns

or expressions

HAVING search condition Specifies a search condition for a group or an aggregate
UNION query expression, Union, Union All Combines the results of two or more queries into a single result set
EXCEPT and INTERSECT

Except : returns any distinct values from the left query that do not exist on the right query. 

Intersect: returns any distinct values that are returned by both the query

Return distinct values by comparing the results of two queries
ORDER BY query expression, COLLATE, ASC, DESC Specifies the sort order on columns returned in the select statement. 
COMPUTE  AVG,COUNT, SUM, MAX, MIN, STEDEV, STDEVP, VAR, VARP  Generates sum, average, max, count, min, stdev, var, varp calculations
FOR Clause  XML, AUTO  Used to specify the results of a query to be xml or other types

Source: the information is from Microsoft MSDN site. For more information, visit http://msdn.microsoft.com/en-us/library/ms189499(v=sql.100).aspx