SQL Comments

SQL Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

Comments are ignored by the database engine. They are for human readers to better understand the purpose of the code.


Single Line Comments

Single line comments start with --.

Any text between -- and the end of the line will be ignored (will not be executed).

Single Line Comment Example

-- Select all customers from Germany
SELECT * FROM Customers
WHERE Country = 'Germany';

You can also place a single line comment at the end of a line:

Inline Comment Example

SELECT * FROM Customers -- WHERE Country = 'Germany';

In this example, the WHERE clause is commented out and will not be executed.


Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored.

Multi-line Comment Example

/* Select all customers.
This is a multi-line comment
used to explain the query. */
SELECT * FROM Customers;

You can also use multi-line comments to disable parts of a statement:

Commenting Out Code

SELECT CustomerName, /* City, */ Country FROM Customers;

Here, the City column will not be included in the result set.