SQL Comments
SQL has two comment styles. The parser ignores them, but every reviewer reads them.
Single-line comment
SQL
-- This is a single-line comment SELECT * FROM customers; -- you can also put one at end of line
Block comment
SQL
/* Daily revenue report. Owner: @ada Last touched: 2026-06-06 */ SELECT DATE(created_at) AS day, SUM(total) AS revenue FROM orders GROUP BY DATE(created_at);
MySQL extension — #
MySQL accepts # as a single-line comment, too. Stick with -- for portability.
Comment-out trick
Wrap a clause in a block comment to quickly disable it for testing:
SQL
SELECT * FROM customers WHERE active = 1 /* AND country = 'AU' */ ORDER BY name;
What to comment
- Why, not what — the SQL already says what.
- Non-obvious filters: "exclude test accounts created before launch".
- Vendor quirks: "MySQL bug 12345 — must use STRAIGHT_JOIN here".
- Owner and last-touched date for ad-hoc reports.
Tip: Many query logs strip comments. If you need a marker that survives logging, tag it as a column alias or a literal:
SELECT /*+ owner=ada */ * FROM ….Example
Example
-- Single-line comment SELECT * FROM customers; /* inline */ /* Block comment */Try it Yourself »
Exercise
Write a single-line SQL comment.
This is a comment
Two hyphens.
Discussion
Loading…