Skip to content
C

Comments


Comments

Definition

A comment is text in a SQL script that the engine ignores — notes for humans, not instructions for the database. SQL supports two comment styles.

How It Works

  • Single-line comment: -- comment text, from the -- to the end of the line. This is the standard SQL comment style and is supported by essentially every dialect (MySQL additionally supports # for single-line comments, which is MySQL-specific).
  • Multi-line (block) comment: /* comment text that can span multiple lines */, exactly like C/Java/JavaScript block comments. Everything between /* and */ is ignored, including line breaks.

Example:

sql
-- Get every Engineering employee, highest paid first SELECT id, name, salary FROM employees WHERE department = 'Engineering' -- filter to one department ORDER BY salary DESC; /* This query intentionally excludes contractors — see ticket ENG-482 for why. */

Edge Cases and Pitfalls

  • -- requires a space or non-hyphen character to be safe in some contexts (some engines treat -- immediately followed by certain characters differently) — but as a rule of thumb, -- (with a trailing space) is always safe and is the conventional style.
  • Nesting block comments (/* outer /* inner */ still outer? */) is not supported in standard SQL — the first */ ends the comment, so still outer? */ becomes actual (invalid) SQL text, a genuinely common source of confusing syntax errors when someone tries to "comment out" a block that already contains a comment.
  • A forgotten, unterminated block comment (/* with no matching */) silently swallows everything after it — including real statements — until either a */ appears later in the file or the file ends, which can be very confusing to debug.

Key Takeaways

  • -- starts a single-line comment (standard); /* ... */ is a multi-line block comment (also standard, and familiar from C-family languages).
  • MySQL's # single-line comment is a dialect-specific addition, not standard SQL.
  • Block comments do not nest — an unterminated or accidentally-nested block comment is a classic, hard-to-spot bug.

Mock Test

  • Comments - Quick Test

    8 questions on Comments.

    8 questions · 8 min · Medium
    Start Mock Test