SELECT

データベースから行を取得し、1 つ以上のテーブルから 1 つ以上の行または列を選択できるようにします。SELECT ステートメントの全構文は複雑なので、主な句のみを以下にまとめます。

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 ] ]

クエリの間で UNION 演算子を使用すると、結果を単一の結果セットに結合できます。

構文

SELECT statement ::=
    < query_expression >
    [ ORDER BY {
order_by_expression | column_position [ ASC | DESC ] }
        [
,...n ]    ]
    [ COMPUTE
        
{ { AVG | COUNT | MAX | MIN | SUM } ( expression ) } [ ,...n ]
        [ BY
expression [ ,...n ] ]
    
]
    [ FOR { BROWSE | XML { RAW | AUTO | EXPLICIT }
            [
, XMLDATA ]
            [
, ELEMENTS ]
            [
, BINARY base64 ]
        }
]
    [ OPTION ( < query_hint > [
,...n ]) ]

< query expression > ::=
    
{ < query specification > | ( < query expression > ) }
    [ UNION [ ALL ] < query specification | ( < query expression > ) [...
n ] ]

< query specification > ::=
    SELECT [ ALL | DISTINCT ]
        [ { TOP
integer | TOP integer 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 > ]

SELECT ステートメントは非常に複雑なので、構文の構成要素と引数の詳細を句ごとに説明します。

SELECT 句
INTO 句
FROM 句
WHERE 句
GROUP BY 句
HAVING 句
UNION 句
ORDER BY 句
COMPUTE 句
FOR 句
OPTION 句

表示: