MySQL Comments

Comments let you annotate MySQL code with notes the server ignores at execution time, using -- for single-line and /* */ for multi-line blocks.

Why Comment Your SQL

Queries that look obvious the day you write them can be baffling months later, especially once WHERE clauses, subqueries, and joins pile up. Comments let you record why a query is shaped the way it is — business rules, edge cases, or reminders for whoever maintains it next, often yourself.

Single-Line Comments with --

A double dash starts a comment that runs to the end of the line. MySQL requires at least one whitespace character, a space or tab, after the two dashes for it to be recognized as a comment — writing two dashes directly followed by text with no space can be treated differently and cause a syntax error, so always include the space.

  • -- single line comment (MySQL requires a space after the dashes)
  • # single line comment, a MySQL-specific shorthand that needs no trailing space
  • /* ... */ block comment that can span multiple lines or sit mid-statement

Annotate a Query Line by Line

-- Get all active customers who joined this year
SELECT customer_id, name, join_date
FROM customers
WHERE status = 'active'          -- only active accounts
  AND join_date >= '2024-01-01';  -- new customers only

Block comments open with /* and close with */, and unlike -- or #, they can stretch across several lines or even sit in the middle of a line of code, which makes them useful for longer explanations or for temporarily disabling a chunk of SQL.

Comment Out a Clause While Debugging

SELECT product_name, price
FROM products
/*
WHERE category = 'Electronics'
  AND price > 100
*/
ORDER BY price DESC;
StyleScopeNotes
-- commentTo end of lineRequires a space after the dashes in MySQL
# commentTo end of lineMySQL-specific, no trailing space required
/* comment */Until closing */Can span multiple lines; cannot be nested

Using the # Shorthand

SELECT name, salary FROM employees
# TODO: revisit this threshold after the Q3 salary review
WHERE salary > 50000;
Note: Use comments to temporarily disable part of a query while debugging instead of deleting it — wrapping a clause in /* */ lets you restore it instantly by deleting just the two comment markers.
Note: MySQL's /* */ comments do not nest. If you comment out a block that already contains /* */ inside it, the first closing */ ends the comment early and the rest of your SQL is read literally, usually producing a confusing syntax error.

Exercise: MySQL Comments

Which syntax starts a single-line comment in MySQL?