Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

SQL Comments: Avoid MySQL’s Dash Trap and Hidden Syntax

SQL uses -- for line comments and /* ... */ for block comments, but MySQL’s whitespace rule and dialect-specific nesting or executable comments can change what a query does.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use -- for a line comment and /* ... */ for a block comment. The main gotchas are that MySQL requires whitespace after --, and a line comment hides everything else on that line—including punctuation your query may need. Comment rules also vary by database, so check the dialect before relying on nesting or assuming every comment is ignored.

Two ways to write a SQL comment

Line comments with --

In PostgreSQL, MySQL, SQLite, SQL Server (Transact-SQL), and Oracle, -- marks a line comment. The comment runs to the line ending or, where applicable, the end of the input. In MySQL, put whitespace or a control character after the second dash; the details and example are covered below.

As an Amazon Associate I earn from qualifying purchases.

-- The rest of this line is a comment
SELECT customer_id, total
FROM orders;

For portability, write a space after the dashes and put the comment on its own line when possible.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Block comments with /* ... */

A block comment starts with /* and ends at */. It can span lines in PostgreSQL, MySQL, SQLite, and Oracle; SQL Server also supports block comments.

/* This comment can span lines.
   Keep the closing marker visible. */
SELECT customer_id, total
FROM orders;

Comments can appear inside a statement where whitespace is allowed, but avoid placing one where it could obscure punctuation you need to check:

SELECT customer_id, /* describes the next column */ total
FROM orders;

Why -- can behave differently in MySQL

MySQL 8.4 recognizes -- as a comment opener only when the second dash is followed by whitespace or a control character. Other engines can have different parsing rules, so do not generalize this MySQL-specific requirement to every database. See the MySQL 8.4 Reference Manual.

This matters in generated arithmetic. The MySQL 8.0 manual gives the example balance=balance--1: the dashes can begin a comment, causing the intended expression after them to be discarded. The result is not the subtraction the writer meant. MySQL’s 8.4 documentation also specifies the whitespace requirement. See MySQL 8.0: “–” as the Start of a Comment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make the operation explicit with spacing and parentheses, for example balance = balance - (-1). This illustrates the intended arithmetic; it is not a substitute for checking how the target database parses the full statement.

How a comment can hide required SQL

A line comment consumes the rest of its line. If a comma, closing parenthesis, join condition, predicate, or statement terminator appears after --, that text is part of the comment too. The next line may then leave an incomplete expression or change how the statement is structured.

SELECT customer_id, -- temporarily omit total,
FROM orders;

Here the comma after total is hidden along with the rest of the line. The remaining statement is not the same as one with a valid second select item. When commenting out a selected expression, inspect the commas and parentheses around it; use a comment on its own line when that is clearer.

Block comments do not nest consistently

Do not assume a block comment can safely contain another block comment. PostgreSQL and Transact-SQL support nested block comments. SQLite says comments do not nest, and MySQL documents nested comments as unsupported and advises avoiding them. Oracle’s cited documentation describes multiline comments but does not establish nested-comment support.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, wrapping code that already contains /* ... */ in another block comment can cause the inner closing marker to end the outer comment earlier than expected in SQLite or MySQL. For portable code, avoid nested block comments. Compare the engine-specific documentation: PostgreSQL 18 lexical structure, SQLite comment syntax, Transact-SQL block comments, and the MySQL 8.4 comments reference.

When comments can affect behavior

Ordinary comments are generally treated like whitespace for parsing, but not every comment-shaped sequence is inert. MySQL supports executable comments using forms such as /*! ... */, and comment syntax is also used for optimizer hints such as /*+ ... */. Oracle hints are comments that provide instructions to the optimizer. These are dialect-specific features, not ordinary explanatory comments; see the MySQL 8.4 documentation and Oracle Database 18c SQL Language Reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

SQL comment behavior by database

Database and documentation -- line comment /* ... */ block comment Nesting or special case
PostgreSQL 18 Through the end of the line Supported Block comments nest; comments are replaced by whitespace before syntax analysis. PostgreSQL documentation
MySQL 8.4 Requires whitespace or a control character after the second dash Supported Nested comments are unsupported and should be avoided. /*! ... */ can execute MySQL code; /*+ ... */ is used for optimizer hints. MySQL documentation
SQLite Through the newline or end of input Supported through the first closing */ Comments do not nest and behave like whitespace. SQLite documentation
SQL Server (Transact-SQL) Through the newline Supported Nested block comments are supported; an unmatched nested opener produces an error. Line-comment documentation and block-comment documentation
Oracle Database 18c Ends at a line break Multiline form documented Hints use comment syntax to give optimizer instructions. SQL*Plus may impose interface-specific blank-line restrictions. Oracle documentation

This comparison is limited to the comment behavior in the linked documentation; it does not imply these databases share other SQL syntax.

What to check when a query breaks after commenting

  1. Identify the database and execution tool. Parser behavior can differ by engine, and a client or script runner may have its own handling.
  2. Write line comments clearly. Put -- on its own line where practical, and include a space after it; MySQL requires whitespace or a control character after the second dash.
  3. Inspect the rest of the line. Look for commas, closing parentheses, predicates, join conditions, and statement terminators that may have been swallowed.
  4. Match the block markers. Count the /* and */ pairs, and do not assume nested comments are supported.
  5. Check the SQL that remains. In a copy of the statement, remove comments mentally or temporarily and confirm the remaining expressions and clauses are complete. If behavior changes despite ordinary-looking comments, check for executable comments or optimizer hints in that dialect.
  6. Isolate the statement. Re-run the smallest relevant query in the target engine to pinpoint which comment or surrounding token changes the parse.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.