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
- SQL keywords are presented in uppercase.
- User-defined identifiers (such as table names, column names, function names) are presented in sentence case.
- Optional elements are enclosed in square brackets (
[ ]). - Required elements are not enclosed in any special characters.
- Elements that can be repeated are indicated by ellipses (
...). - Alternative elements are separated by vertical bars (
|) and enclosed in curly braces ({ }) to indicate a choice.
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.
SELECTFirstName,LastNameFROMEmployeesWHEREDepartmentID= 5;
Example with a reserved identifier (if not using quoting rules):
SELECT[Order]FROMOrderTable;
Optional Clauses
The ORDER BY clause is optional in a SELECT statement.
SELECTProductName,UnitPriceFROMProducts[ORDER BYUnitPriceDESC];
Choices
When specifying data types for a column, you have to choose one.
CREATETABLEUsers(UserIDINTPRIMARY KEY,UserNameVARCHAR(50), {AgeINT|DateOfBirthDATE} );
Repetitive Elements
When inserting multiple rows, you can provide multiple sets of values.
INSERTINTOSales(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.SELECTCOUNT(*)FROMOrders;/* This is a multi-line comment that spans several lines. */UPDATEProductsSETDiscontinued= 1WHEREProductID= 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.