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 onlyBlock 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;Using the # Shorthand
SELECT name, salary FROM employees
# TODO: revisit this threshold after the Q3 salary review
WHERE salary > 50000;Exercise: MySQL Comments
Which syntax starts a single-line comment in MySQL?