SQL > SQL Commands > Comment

Documentation is an important component of coding, and SQL is no exception.

SQL supports two comment styles: single-line comments using -- and multi-line comment blocks using /* ... */. Neither style affects query execution — they exist purely to document and explain your code.

There are two main ways to add comments in SQL:

Method 1: Single line

To add a comment that is only across a single line, we would add two dashes '--'. Everything after these two dashe will be considered as a comment and will not be executed. The line break acts as the end of the comment.

Note that you can add the two dashes in the middle of a line. In that instance, everything before the two dashes will still be considered as part of the code and will be executed, while everything after the two dashes is treated as a comment.

Example 1:

An entire line is used as a comment.

-- This is a comment. We want to select everything from the Sales table.
SELECT * FROM Sales;

Example 2:

Comments can appear anywhere in the code, even in the middle of a SQL block.

SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name
HAVING SUM(Sales) > 1000 -- Only include stores with sales of greater than $1000
ORDER BY Store_Name;

In the code above, the HAVING clause will still execute as only the texts after the two dashes are considered as comments.

Method 2: Multiple lines

To add a comment that spans across multiple lines, we would start the comment block with /* and then end the comment block with */. Everything in between these two markers is considered as a comment. There is no strict rule on what needs to go into the comment block. You can leave open lines, have special characters... it doesn't matter.

Example:

/* 2/22/2022:
We are adding in an additional filter to the query below so only sales over $5000 is included in our final report.
*/

Select * from Sales where sales > 5000;

In this example, the first three lines are all comments and has no impact on how the code is executed.

Frequently Asked Questions

Q: How do you write a single-line comment in SQL?
A: Use two dashes (--) before the text. Everything after -- on that line is treated as a comment and is not executed.

Q: How do you write a multi-line comment in SQL?
A: Wrap the comment block with /* at the start and */ at the end. Everything between these markers is ignored by the SQL engine.

Q: Can a SQL comment appear in the middle of a query?
A: Yes. A single-line comment (--) can appear in the middle of a line; only the text after the dashes is commented out, while the code before it still executes.

Q: Are SQL comments supported in all databases?
A: The -- single-line and /* */ multi-line comment styles are supported in virtually all major SQL databases including MySQL, SQL Server, PostgreSQL, and Oracle.

Next: Advanced SQL

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact