How to add SQL comments to your code

SQL comments are used to add notes directly within the code. There are three main types: single-line comments, multi-line comments, and in-line comments.

What are SQL comments?

As in many other database and programming languages, you can use Structured Query Language to place comments within the code. These comments are readable but have no effect on execution. These have several possible uses. For example, SQL comments can be used to explain certain areas within the code in more detail for future reference or for others who may work on it. A comment can also be used to prevent the execution of an SQL command.

Depending on how they’re formatted, SQL comments can be divided into two or three categories:

  • Single Line Comments: These comments start and end on the same line of code. They start with --.

  • Multi Line Comments: These comments cover several lines. They’re initiated with /* and ended with */.

  • In Line Comments: These comments are a subtype of Multi Line Comments. They’re inserted between two statements and are also marked with /* and */.

VPS Hosting
Fully virtualised servers with root access
  • Unlimited traffic and up to 1 Gbit/s bandwidth
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

Single line comments

Single-line comments in SQL are indicated by two consecutive hyphens (–). Anything written after these hyphens on that line is ignored during execution. The comment ends automatically at the end of the line. From the beginning of the next line, the code is executed normally again. Therefore, single-line comments don’t require an explicit end character.

To better understand how it works, it’s worth taking a look at an example. In the following short code excerpt, we place a comment before the SELECT command that describes its purpose in more detail.

-- Call up all entries from the Employees table 
SELECT * 
FROM Employees;
sql

This means that only the code from SELECT onwards is taken into account.

You can also insert a single-line comment after a statement, allowing both the statement and comment to be on the same line, provided the comment follows the statement. This is not possible the other way around.

SELECT * -- select all entries 
FROM Employees; -- include the table named Employees
sql

Multi-line comments

SQL comments that extend over several lines work a little differently. These must have an end point as well as a starting point. All text between these two points is ignored when the code is executed. Using our example above, this would be:

/* Select all fields 
in all rows 
of the entire table named Employees: */ 
SELECT * 
FROM Employees;
sql

It’s possible to use SQL comments to prevent the execution of a particular statement. This can be useful if you’ve already used them but want to temporarily ignore them:

/ *SELECT*  FROM Invoices; 
SELECT * FROM goods receipts; 
SELECT * FROM returns; 
SELECT  *FROM customer list;* / 
SELECT * FROM Employees;
sql

Although we’ve included five instructions here, the system will only execute the command to call up the table for employees. The instructions for invoices, goods receipts, returns, and the customer list are within the comment and are therefore ignored.

In-line comments

In-line comments are a subtype of regular multi-line comments. With a clear start and end point, they can be placed between instructions without disrupting the reading flow too much. If you want to explain a specific part of a line in more detail, in-line comments can be a good choice. The following simple example demonstrates how such a comment works:

SELECT *
FROM / *Insert the name of the table here* / Employees;
sql

The system will display the entire table called ‘Employees’ with all its columns and fields without any hindrance. The comment is only visible to you or another user of the code.

Tip

Whether MSSQL, MySQL or MariaDB: With SQL Server Hosting from IONOS, you can choose from the best server options. Additionally, you’ll benefit from outstanding performance, robust security mechanisms, and personalised advice.

Was this article helpful?
Page top