MSDN Documentation

SQL Syntax Conventions

This document outlines the standard syntax conventions used in Microsoft SQL Server documentation to represent SQL statements, keywords, identifiers, and other language elements.

General Conventions

Syntax Representation

The following table details common syntax elements and their representation:

Element Description Example
Uppercase Keywords SQL language keywords (e.g., SELECT, FROM, WHERE). SELECT column_name FROM table_name;
Sentence Case Identifiers User-defined names for tables, columns, views, procedures, etc. CustomerName, OrderDetails
Square Brackets [ ] Optional clauses or parameters. SELECT column_name [ AS alias_name ] FROM table_name;
Ellipses ... Indicates that an element or group of elements can be repeated. INSERT INTO table_name ( column1, column2, ... ) VALUES ( value1, value2, ... );
Curly Braces { } with | Indicates a required choice between two or more options. CREATE TABLE table_name ( { column_name data_type | CONSTRAINT constraint_name constraint_definition } ... );
Angle Brackets < > Placeholder for a specific value or expression. SELECT * FROM Products WHERE UnitPrice > <price_threshold>;
Braces { } (as literals) Literal curly braces, often used for grouping. SET @variable = {value1, value2};

Examples of Syntax Elements

Keywords

SQL keywords are always presented in uppercase to distinguish them from identifiers.

SELECT, INSERT, UPDATE, DELETE, FROM, WHERE, GROUP BY, ORDER BY, JOIN, PRIMARY KEY, FOREIGN KEY, CONSTRAINT

Identifiers

Identifiers, such as table and column names, are usually written in sentence case. If they contain spaces or are reserved keywords, they should be enclosed in square brackets ([ ]) or double quotes (" ") depending on the database system configuration.

SELECT FirstName, LastName FROM Employees WHERE DepartmentID = 5;

Example with a reserved identifier (if not using quoting rules):

SELECT [Order] FROM OrderTable;

Optional Clauses

The ORDER BY clause is optional in a SELECT statement.

SELECT ProductName, UnitPrice
FROM Products
[ ORDER BY UnitPrice DESC ];

Choices

When specifying data types for a column, you have to choose one.

CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    UserName VARCHAR(50),
    { Age INT | DateOfBirth DATE }
);

Repetitive Elements

When inserting multiple rows, you can provide multiple sets of values.

INSERT INTO Sales ( ProductID, Quantity, SaleDate )
VALUES
    ( 101, 5, '2023-10-26' ),
    ( 102, 2, '2023-10-26' ),
    ... ;

Comments

Comments in SQL can be single-line or multi-line.

-- This is a single-line comment.
SELECT COUNT(*) FROM Orders;

/* This is a multi-line
   comment that spans
   several lines. */
UPDATE Products
SET Discontinued = 1
WHERE ProductID = 70;

Adhering to these conventions ensures clarity and consistency across SQL documentation, making it easier for developers and database administrators to understand and use SQL effectively.