SQL Coding Guidelines

SQL is a freeform language. That means that you can include line breaks, spaces, and indentation without affecting the way the database interprets the code. In addition, SQL is not case-sensitive like some languages. That means that you can use uppercase or lowercase letters or a combination of the two without affecting the way the database interprets the code.

Although you can code SQL statements with a freeform style, we suggest that you follow the coding recommendations presented in figure 1-15. First, you should start each clause of a statement on a new line. In addition, you should continue long clauses onto multiple lines and you should indent the continued lines. You should also capitalize the first letter of each keyword in a statement to make them easier to identify, you should capitalize the first letter of each word in table and column names, and you should end each statement with a semicolon. Although the semicolon isn’t currently required in most cases, it will be in a future version of SQL Server. So you should get used to coding it now. Finally, you should use comments to document code that’s difficult to understand.

The examples at the top of this figure illustrate these coding recommendations. The first example presents an unformatted SELECT statement. As you can see, this statement is difficult to read. In contrast, this statement is much easier to read after our coding recommendations are applied, as you can see in the second example.

The third example illustrates how to code a block comment. This type of comment is typically coded at the beginning of a statement and is used to document the entire statement. Block comments can also be used within a statement to describe blocks of code, but that’s not common.

The fourth example in this figure includes a single-line comment. This type of comment is typically used to document a single line of code. A single-line comment can be coded on a separate line as shown in this example, or it can be coded at the end of a line of code. In either case, the comment is delimited by the end of the line.

Although many programmers sprinkle their code with comments, that shouldn’t be necessary if you write your code so it’s easy to read and understand. Instead, you should use comments only to clarify portions of code that are hard to understand. Then, if you change the code, you should be sure to change the comments too. That way, the comments will always accurately represent what the code does.

A SELECT statement that’s difficult to read

A SELECT statement that’s coded with a readable style

A SELECT statement with a block comment

A SELECT statement with a single-line comment

Figure 1-15 SQL coding guidelines

Coding recommendations

  • Start each new clause on a new line.
  • Break long clauses into multiple lines and indent continued lines.
  • Capitalize the first letter of each keyword and each word in column and table names.
  • End each statement with a semicolon (;).
  • Use comments only for portions of code that are difficult to understand.

How to code a comment

  • To code a block comment, type /* at the start of the block and */ at the end.
  • To code a single-line comment, type –followed by the comment.


  • Line breaks, white space, indentation, and capitalization have no effect on the operation of a statement.
  • Comments can be used to document what a statement does or what specific parts of a statement do. They are not executed by the system.


Throughout this tutorial, SQL keywords are capitalized so they’re easier to identify. However, it’s not necessary or customary to capitalize SQL keywords in your own code.