SELECT Statement Syntax Diagram

Click on the keyword in the simplified syntax diagram below to go directly to that part of the full syntax diagram.
 
SELECT ...
    [ INTO ... ]
    [ FROM ... ]
    [ ... JOIN ... ]
    [ WHERE ... ]
    [ GROUP BY ... ]
    [ HAVING ... ]
    [ UNION ... ]
    [ ORDER BY ... ]
    [ COMPUTE ... ]
    [ FOR BROWSE ... ]
    [ OPTION ... ]

 
Place the cursor on any keyword to see the definition in a pop-up window.
(IE4+, IE5+, beta IE6, N4+)
 
SELECT
    [ ALL  |  DISTINCT ]
    [
        TOP n
      | TOP n PERCENT
      | TOP n WITH TIES
      | TOP n PERCENT WITH TIES
    ]
    {
      [ table_name.  |  table_alias.  |  view_name. ]
      {
          *
        | column_name [ [ AS ] column_alias ]
        | expression [ [ AS ] column_alias ]
        | IDENTITYCOL [ [ AS ] column_alias ]
        | ROWGUIDCOL [ [ AS ] column_alias ]
        | column_alias = expression
      }
    }
    [  ...,  n ]
 
    [ INTO  new_table ]
 
    [
      FROM

      {
          derived_table
            [ AS table_alias ]
            [ ( column_alias_1,   [ column_alias_2,   ...,  n ] ) ]
        |
          CONTAINSTABLE ( table, column  |  *, 'search_conditions' )
            [ AS table_alias ]
        |
          FREETEXTTABLE ( table, column  |  *, 'free_text_string' )
            [ AS table_alias ]
        |
          table_name [ AS table_alias ]
            [
               WITH (  INDEX ( Index_1,   index_2,   ...,  n ] )
                          | FASTFIRSTROW
                          | HOLDLOCK
                          | NOLOCK
                          | PAGLOCK
                          | READCOMMITTED
                          | READPAST
                          | READUNCOMMITTED
                          | REPEATABLEREAD
                          | ROWLOCK
                          | SERIALIZABLE
                          | TABLOCK
                          | TABLOCKX
                          | UPDLOCK
                          [  ...  n  |  ...,  n ]
                         )
            ]
        |
          view_name [ AS table_alias ]
      }
      [
          INNER JOIN
 |  LEFT [ OUTER ] JOIN  |  RIGHT [ OUTER ] JOIN
            {
               derived_table [ ON search_conditions ]
             |
               OPENQUERY( server, 'query' ) [ ON search_conditions ]

             |
               OPENROWSET
                               ( 'provider_name',
                                 { 'datasource';'user_id';'password',  |  'provider_string', }
                                 { [ catalog. ] [ schema. ] object  |  'query' }
                               )
[ ON search_conditions ]
             |
               table_name [ ON search_conditions ]
             |
               view_name [ ON search_conditions ]
            }
            [  ...,  n ]
          |
          CROSS JOIN  |  FULL [ OUTER ] JOIN
            {
               derived_table
             |
               OPENQUERY( server, 'query' )
             |
               OPENROWSET
                               ( 'provider_name',
                                 { 'datasource';'user_id';'password',  |  'provider_string', }
                                 { [ catalog. ] [ schema. ] object  |  'query' }
                               )

             |
               table_name
             |
               view_name
            }
            [  ...,  n ]
       ]
    ]

 
    [
        WHERE
search_conditions
      |
        WHERE column_name =* column_name
      |
        WHERE column_name *= column_name
    ]

 
    [
        GROUP BY
 {  group_by_expression,  [  ...,  n ]  }
        [ WITH CUBE  |  WITH ROLLUP ]
      |
        GROUP BY ALL  {  group_by_expression,  [  ...,  n ]  }
    ]

 
    [ HAVING  search_conditions ]

 
    [
        {
            query 
UNION  query
          |
            query  UNION ALL  query
        }
        [
            UNION  query
          |
            UNION ALL  query
        ]
        [  ...  n ]
    ]

 
    [ ORDER BY order_by_expression [ ACS  |  DESC ] [  ...,  n ]  }  ]

 
    [
      COMPUTE

      {
          AVG ( expression )
        | COUNT ( expression )
        | MAX ( expression )
        | MIN ( expression )
        | STDEV ( expression )
        | STDEVP ( expression )
        | SUM ( expression )
        | VAR ( expression )
        | VARP ( expression )
      }
      [  ...,  n ]
      [ BY expression, [  ...,  n ]
    ]

 
    [ FOR BROWSE ]

 
    [
        OPTION ( 
FAST number_rows
                       | FORCE ORDER
                       | HASH GROUP
                       | ORDER GROUP
                       | HASH JOIN
                       | LOOP JOIN
                       | MERGE JOIN
                       | KEEP PLAN
                       | MAXDOP
                       | ROBUST PLAN
                       | CONCAT UNION
                       | HASH UNION
                       | MERGE UNION
                       [  ...,  n ]
                     )
    ]

 
Key to blue and red text in syntax diagram
KEYWORDS Keywords are denoted with upper case letters. Obey the spelling.
variables All user-supplied variables are denoted with lower case letters.
...,   n Signifies that there can be more than one value in a comma delimited list. Note that the dots and n are not part of the code and must not appear in the SQL query.
...  n Signifies that there can be more than one value in a blank space delimited list. Note that the dots and n are not part of the code and must not appear in the SQL query.
{  } Signifies that all, or some portion, of the code elements between the braces are required elements and must appear in the SQL query. Note that these braces are not part of the code and must not appear in the SQL query.
[  ] Signifies that the code elements between the square brackets can optionally appear in the SQL query, but are not required. Note that these brackets are not part of the code and must not appear in the SQL query.
| The or symbol signifies that you may use only one of the code elements or values from the possible choices. Note that the or symbol is not part of the code and must not appear in the SQL query.

 
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information