SQL Comments

Last Updated : 21 Jan, 2026

SQL comments are written inside SQL code to help developers understand what the code is doing. They are not executed by the database and are used to explain queries, give additional information, or make the code easier to read and maintain.-

  • They help in explaining the purpose of SQL statements.
  • They make the code more readable and easy to maintain.
  • They allow developers to temporarily disable parts of a query for testing or debugging.

Types of SQL Comments

SQL Comments explain sections of SQL statements or prevent SQL statements from being executed. These are the three commenting methods in SQL, each with its unique use. Let's discuss these SQL comments in detail below:

There are 3 types of comments in SQL:

  1. Single-line comments
  2. Multi-line comments
  3. In-line comments

1. SQL Single Line Comments

Single-line comments are used to annotate a single line of SQL code. They are often employed for brief explanations or to temporarily disable lines of code without removing them. Single-line comments begin with two hyphens (--), and the comment continues until the end of the line.

Syntax:

-- single line comment

Query:

SELECT * FROM Students;  -- This gets all student records

The comment explains that the query retrieves all records from the customers table.

2. SQL Multi-Line Comments

Multi-line comments are used when we need to comment out more than one line of SQL code. These comments are enclosed between /* and */ and can span multiple lines. They are typically used for longer explanations or to disable larger sections of code temporarily.

Syntax:

/* multi line comment
another comment */

Query:

SELECT * 
FROM orders
WHERE YEAR(order_date) = 2022;
/* This query retrieves all orders
that were placed in the year 2022 */

The multi-line comment explains that the query retrieves all orders placed in the year 2022 from the orders table.

3. SQL In-Line Comments

In-line comments allow us to add comments within a query itself. They are typically used to provide additional information or explanations for specific parts of the query, without interrupting the flow of the SQL statement. In-line comments start with /* and end with */.

Syntax:

SELECT * FROM /* Customers; */ 

Query:

SELECT 
customer_name, -- Name of the customer
order_date -- Date when the order was placed
FROM orders;

In-line comments explain what customer_name and order_date mean. They make the code easier to understand for others.

Important Points About SQL Comments

While SQL comments are crucial for code documentation, there are several best practices to follow when using them:

  • Be Concise but Clear: Write comments that are short and easy to understand. Avoid unnecessary comments.
  • Explain Complex Queries: Use comments to explain difficult queries or important business logic.
  • Avoid Commenting Obvious Code: Do not comment on simple or self-explanatory SQL statements.
  • Use Comments to Temporarily Disable Code: Comments can be used to comment out code for testing or debugging.
  • Consistent Formatting: Use the same comment style everywhere to make the code easy to read.
Comment