MySQL Comments
MySQL Comments: Documenting Queries and Migrations
MySQL comments let you add plain-English notes inside SQL queries, migration scripts, and stored routines without affecting execution. The MySQL parser ignores comments cleanly, helping teammates understand complex filters and business rules months later.
Three Comment Styles in MySQL
- Double-Dash (
--): Standard SQL single-line comment. You must put a space or tab immediately after--, or MySQL throws a syntax error. - Hash (
#): MySQL-specific single-line comment (no trailing space required, though it is not portable to PostgreSQL or SQLite). - Slash-Star (
/* ... */): Multi-line or inline block comment that works inside expressions and across multiple lines.
Practical SQL Example
The query below uses standard -- single-line comments and an inline /* ... */ block comment while summarizing active subscriptions in demo_orders:
-- Summarize paid demo orders by status
SELECT
status,
COUNT(*) AS order_count, -- Includes all paid rows
SUM(total_amount) AS revenue_total
FROM demo_orders
WHERE status IN ('paid', 'shipped')
/* Exclude internal test orders
created during staging QA */
AND customer_id > 0
GROUP BY status;
| status | order_count | revenue_total |
|---|---|---|
| paid | 85 | 12450.00 |
| shipped | 14 | 1890.00 |
Special MySQL Comment Variants
MySQL treats /*! ... */ as an executable comment that only MySQL parses (useful in mysqldump files), and /*+ ... */ as optimizer hints such as /*+ MAX_EXECUTION_TIME(1000) */.
Common Mistakes and Best Practices
- Never Forget the Space After
--: Writing--commentwithout a space fails in MySQL, unlike SQLite or PostgreSQL; always write-- comment. - Prefer
--Over Hash Comments: Using--keeps your SQL portable across MySQL, PostgreSQL, and SQLite sandboxes. - Avoid Nested Block Comments: Writing
/* outer /* inner */ */closes early at the first*/and causes a syntax error.
Frequently Asked Questions
Why does --my note cause a syntax error in MySQL?
MySQL requires at least one whitespace character after -- so the parser does not confuse double-dash comments with double negation (--5).
Can I place a block comment inside a SELECT line?
Yes, SELECT /* top customers */ id, email FROM demo_customers; works cleanly inline.
Does /* ... */ work across multiple lines?
Yes, everything between /* and the closing */ is ignored regardless of line breaks.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.