I am working in SQL. What is the complete explanation for using comments in SQL, with some example code?
-
3--Single line comment. – Amit Garg Oct 22 '14 at 04:49
-
This is ***the*** top search engine hit for `site:stackoverflow.com sql comment character`... – Peter Mortensen Aug 22 '22 at 19:00
-
What is the canonical duplicate? There must be one from 2008 or 2009. – Peter Mortensen Aug 22 '22 at 19:01
-
A candidate (2009) is *[Are there multiline comment delimiters in SQL that are vendor agnostic?](https://stackoverflow.com/questions/728172/)*. Its answers covers both single-line `--` and C style `/* */`. A more specialised one (2011) is *[SQL comments on create table on SQL Server 2008](https://stackoverflow.com/questions/4586842/)*. – Peter Mortensen Aug 22 '22 at 19:28
-
For MySQL (2012): *[How can I add comments in MySQL?](https://stackoverflow.com/questions/9098655/)*. But there must be one from 2008 or 2009. – Peter Mortensen Aug 22 '22 at 19:33
5 Answers
MySQL Server supports three comment styles:
From a #
character to the end of the line.
From a --
sequence to the end of the line. In MySQL, the “-- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.8.2.5, “'--' as the Start of a Comment”.
From a /*
sequence to the following */
sequence, as in the C programming language. This syntax enables a comment to extend over multiple lines because the beginning and closing sequences need not be on the same line.
Reference http://dev.mysql.com/doc/refman/5.0/en/comments.html

- 3,867
- 1
- 27
- 37
In Oracle, there are three ways.
1.
SQL> REM This is a comment
2.
SQL> -- This is a single line comment
3.
SQL> /* This is a
SQL> multiple line
SQL> comment */
SQL>
The difference between REM and the other two is that, -- and /* */ can be used in a PL/SQL
block, while REM[ARK]
cannot.
Let's see.
SQL> REM comment 1
SQL> -- comment 2
SQL> /* comment 3*/
SQL> begin
2 DBMS_OUTPUT.PUT_LINE('comment 1'); --comment 1
3 DBMS_OUTPUT.PUT_LINE('comment 2'); /* comment 2*/
4 end;
5 /
comment 1
comment 2
PL/SQL procedure successfully completed.
SQL>
So, --
and /* */
works in PL/SQL block too. However, REM
won't.
SQL> begin
2 DBMS_OUTPUT.PUT_LINE('comment'); REM comment
3 end;
4 /
DBMS_OUTPUT.PUT_LINE('comment'); REM comment
*
ERROR at line 2:
ORA-06550: line 2, column 41:
PLS-00103: Encountered the symbol "COMMENT" when expecting one of the
following:
:= . ( @ % ;
The symbol "; was inserted before "COMMENT" to continue.
Update Mostly all GUI based tools are able to execute SQL*Plus
commands in their own sqlplus type window. It works perfectly in SQL Developer
when executed as a script
. In PL/SQL Developer
too it should work with COMMAND
window.
A screenshot from SQL Developer
.
Same in SQL*Plus
.
SQL> SELECT * FROM DUAL;
D
-
X
SQL> REM THIS IS A COMMENT
SQL> SELECT 'ABOVE COMMENT WORKS' FROM DUAL;
'ABOVECOMMENTWORKS'
-------------------
ABOVE COMMENT WORKS

- 47,486
- 13
- 97
- 124
-
3`REM` is not a SQL comment it's a SQL*Plus comment - that is something different (`REM` would be invalid when the SQL is executed in a different tool) – Oct 22 '14 at 05:58
-
Which tool? Mostly all GUI based tools are able to execute `SQL*Plus` commands in their own sqlplus type window. It works perfectly in `SQL Developer` when executed as a `script`. In `PL/SQL Developer` too it should work with `COMMAND` window. Which tool are you talking about? Could you be specific. – Lalit Kumar B Oct 22 '14 at 06:59
-
4Yes, because it is a **SQL\*Plus** command, **not** a SQL command. See here: http://sqlfiddle.com/#!4/d41d8/36857 – Oct 22 '14 at 07:25
-
Aah, got your point. But, `REM` is good to know for scripts in any batch jobs. – Lalit Kumar B Oct 22 '14 at 07:28
You can write a comment in SQL & PLSQL statements in two easy ways:
Begin the comment section with a forward slash and an asterisk (/*). Proceed with the text of the comment. This text can be spanning multiple lines.
Begin the comment section with -- (two hyphens). Proceed with the text of the comment. This text cannot extend to a new line for new commenting. Every line start with – (two hyphens). Let see below examples
SELECT e.ename /* Author: OnlinteItTutor.com */ FROM emp e;
Here is a SQL comment in query that appears in the middle of the line:
SELECT /* Author: OnlinteItTutor.com */ e.ename
FROM emp e;
Here is a SQL comments in query that appears at the end of the line:
SELECT e.ename /* Author: OnlinteItTutor.com */
FROM emp e;
Or:
SELECT e.ename -- Author: OnlinteItTutor.Com
FROM emp e;
In Oracle, you can create a SQL comment that shows multiple lines in your SQL statement. For example:
SELECT e.ename
/*
* Author: OnlineItTutor.Com
* Purpose: To show a comment of multiple lines in SQL Query.
*/
FROM emp e;
This SQL comment in query shows across multiple lines in Oracle - in this example, it shows across 4 lines. In Oracle, you can also create a SQL comment in query that spans multiple lines using this syntax:
SELECT e.ename /* Author: OnlineItTutor.com
Purpose: To show a comment in SQL Query for multiple lines. */
FROM emp e;

- 6,882
- 4
- 44
- 65

- 11
- 3
/*
this is a
multiple-line comment
*/

- 466
- 3
- 16
-
1
-
Taken from the same source of Amit Garg's reply. It is ok for MySQL – George Eco Oct 22 '14 at 04:53
-
3`/* Comments are in ISO 9075 - Standard SQL */`. See [Are there multiline comment delimiters in SQL that are vendor agnostic?](http://stackoverflow.com/questions/728172/are-there-multiline-comment-delimiters-in-sql-that-are-vendor-agnostic/728326#728326) – Jonathan Leffler Oct 22 '14 at 05:06
MySQL Server supports three comment styles:
From a “#” character to the end of the line.
From a “-- ” sequence to the end of the line. In MySQL, the “-- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.8.2.5, “'--' as the Start of a Comment”.
From a /* sequence to the following */ sequence, as in the C programming language. This syntax enables a comment to extend over multiple lines because the beginning and closing sequences need not be on the same line. The following example demonstrates all three comment styles:
mysql> SELECT 1+1; # This comment continues to the end of line
mysql> SELECT 1+1; -- This comment continues to the end of line
mysql> SELECT 1 /* this is an in-line comment */ + 1;
mysql> SELECT 1+
/*
this is a
multiple-line comment
*/
1;