Sdílet prostřednictvím


SELECT (Transact-SQL)

Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables. The full syntax of the SELECT statement is complex, but the main clauses can be summarized as:

[ WITH <common_table_expression>]

SELECT select_list [ INTO new_table ]

[ FROM table_source ] [ WHERE search_condition ]

[ GROUP BY group_by_expression ]

[ HAVING search_condition ]

[ ORDER BY order_expression [ ASC | DESC ] ]

The UNION, EXCEPT, and INTERSECT operators can be used between queries to combine or compare their results into one result set.

Topic link iconTransact-SQL Syntax Conventions

Syntax

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 ] ] 
    ] 
    [ <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 [ ALL ] group_by_expression [ ,...n ] 
                [ WITH { CUBE | ROLLUP } ]
    ]
    [ HAVING < search_condition > ] 

Remarks

Because of the complexity of the SELECT statement, detailed syntax elements and arguments are shown by clause:

WITH common_table_expression

UNION

SELECT Clause

EXCEPT and INTERSECT

INTO Clause

ORDER BY

FROM

COMPUTE

WHERE

FOR Clause

GROUP BY

OPTION Clause

HAVING

See Also

Reference

SELECT Examples (Transact-SQL)

Help and Information

Getting SQL Server 2005 Assistance