Skip to content
Tutorials Center
My learning

Topics

SQL Reference ยท Data Definition

SQL comments

Reference for SQL comments: -- line comments and /* */ block comments, nesting rules, MySQL's # and hint comments, and when comments change behavior.

Comments let you explain a query or switch part of it off without deleting it. The database ignores them completely. SQL has two standard forms: -- starts a comment that runs to the end of the line, and /* ... */ wraps a block that may span several lines.

Good comments say why a query is written the way it is (a business rule, a workaround), not what each keyword does.

SQL
-- orders from the last quarter
SELECT id, status /* ignore internal test orders */
FROM orders;

Try it

SQL Line and block comments ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

SQL Commenting out a condition and a string that only looks like a comment ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

Syntax

SQL
-- single-line comment (needs a space after the dashes in MySQL)
/* block comment,
   possibly over several lines */
Form Description
-- text Runs from -- to the end of the line.
/* text */ Runs until the first */. Can sit inside a statement, even between two keywords.
# text MySQL only: single-line comment.

Dialect differences

Dialect Notes
SQLite -- and /* */. Block comments do not nest. An unterminated /* runs to the end of the input.
PostgreSQL -- and /* */; block comments nest, so /* a /* b */ c */ is one comment.
MySQL Also #. -- must be followed by a space or control character. /*! ... */ runs only on MySQL, and /*+ ... */ holds optimizer hints.
SQL Server Block comments nest. GO is a client batch separator, not a comment.
Oracle --, /* */ and REM in SQL*Plus; /*+ ... */ is a hint, not a plain comment.
SQL
-- MySQL
SELECT * FROM orders; # hash comment
SELECT /*! STRAIGHT_JOIN */ * FROM orders;   -- executed by MySQL, ignored elsewhere

-- Oracle: hint comment
SELECT /*+ INDEX(o idx_orders_customer) */ * FROM orders o;

Usage notes

  • Not inside strings. Dashes or slashes within single quotes are data, as the second example shows.
  • Do not nest block comments in portable code; SQLite and MySQL end the comment at the first */.
  • Comment out a WHERE carefully: if you comment the whole WHERE of an UPDATE or DELETE, it affects every row.
  • Hints look like comments (/*+ ... */) but may change the execution plan in Oracle and MySQL. Other databases ignore them.
  • Stored documentation: PostgreSQL, Oracle and others also have COMMENT ON TABLE ... IS '...' to attach descriptions to objects, kept in the catalog.
  • Safety: attackers use -- to cut off the rest of a query in SQL injection. Always use parameters.