SQL > SQL Commands > Comment

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

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.

Next: Advanced SQL

This page was last updated on October 11, 2024.




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


AdBlock Detected!

Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.

AltStyle によって変換されたページ (->オリジナル) /