How Do You Comment In Sql

5 min read

Comments in SQL are more than just notes for yourself—they are essential tools for documenting logic, disabling code during debugging, and collaborating with teammates who may read your queries months or years later. Whether you are working in MySQL, SQL Server, PostgreSQL, Oracle, or SQLite, knowing how do you comment in SQL correctly ensures your code remains clear, maintainable, and easy to troubleshoot. This guide walks you through every comment style, database-specific quirks, and best practices so you can write SQL that speaks clearly to both humans and machines.

Why Comments Matter in SQL

SQL is a declarative language, but real-world queries often grow into complex joins, subqueries, and business rules that are not self-explanatory. A well-placed comment can explain why a filter exists, what a calculated column represents, or which stakeholder requested a specific condition. Comments also serve as temporary switches: by commenting out a WHERE clause or a JOIN, you can test query behavior without deleting code. In real terms, in team environments, comments reduce onboarding time and prevent costly misinterpretations. Without them, even the author may struggle to recall the intent behind a query six months later Most people skip this — try not to..

Honestly, this part trips people up more than it should.

Single-Line Comments in SQL

Single-line comments begin with two hyphens (--) and extend to the end of the line. This is the most portable comment style, supported by nearly every relational database, including MySQL, SQL Server, PostgreSQL, Oracle, and SQLite.

Example:

-- Calculate total revenue for the current quarter
SELECT SUM(amount) AS total_revenue
FROM orders
WHERE order_date >= '2024-01-01'; -- Adjust date for each quarter

You can place a single-line comment on its own line or after a statement. Everything after -- is ignored by the SQL engine. Note that some databases also support the # symbol for single-line comments (notably MySQL), but -- is the standard and safest choice for cross-platform compatibility Took long enough..

Key rules for single-line comments:

  • Use -- followed by a space for readability.
  • Avoid nesting -- inside a string literal—it will be treated as text, not a comment.
  • Keep comments concise; if you need more than one line, switch to block comments.

Multi-Line Comments in SQL

Multi-line (block) comments start with /* and end with */. They can span multiple lines or appear inline within a statement. Block comments are ideal for longer explanations, disabling large sections of code, or adding header blocks to stored procedures Simple, but easy to overlook..

Example:

/*
  This query retrieves active customers
  who have placed at least one order
  in the last 30 days.
*/
SELECT c.customer_id, c.name
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days'
  AND c.status = 'active';

Block comments can also be used inline:

SELECT /*+ INDEX(orders idx_order_date) */ *
FROM orders
WHERE order_date > '2024-01-01';

The /*+ ... */ syntax is an optimizer hint in Oracle and some other databases, not a true comment—but it uses the same delimiters.

Important: Most SQL databases do not support nested block comments. If you write /* outer /* inner */ outer */, the comment ends at the first */, leaving outer */ as invalid syntax. Avoid nesting unless your database explicitly supports it (PostgreSQL does support nested block comments, but MySQL and SQL Server do not) Which is the point..

Database-Specific Comment Syntax

While -- and /* */ are widely supported, some databases add their own variations. Here is a quick reference:

  • MySQL: Supports -- (requires a space after the hyphens), # for single-line comments, and /* */ for block comments. MySQL also supports version-specific comments like /*!40101 SET ... */ for conditional execution.
  • SQL Server (T-SQL): Supports -- and /* */. Nested block comments are allowed in T-SQL, but use them sparingly to avoid confusion.
  • PostgreSQL: Supports -- and /* */, including nested block comments. It also supports COMMENT ON statements to store metadata about database objects.
  • Oracle: Supports -- and /* */. Oracle also uses /*+ ... */ for optimizer hints.
  • SQLite: Supports -- and /* */. No # comments.

If you work across multiple platforms, stick to -- and /* */ for maximum portability.

Best Practices for Commenting SQL Code

Good comments explain why, not what. The code already shows what is happening; your comment should add context that is not obvious from the syntax Simple, but easy to overlook..

  • Document business rules: Explain why a filter excludes certain records, e.g., -- Exclude test accounts (IDs 9000-9999) from revenue reports.
  • Clarify complex joins: Note the relationship between tables when it is not obvious, e.g., -- Join on legacy customer_id because email is not unique.
  • Mark temporary changes: Use -- TODO: or -- FIXME: to flag work in progress.
  • Disable code during debugging: Comment out a WHERE clause or JOIN to isolate behavior, but remove or restore it before committing.
  • Keep comments up to date: A stale comment is worse than no comment—it misleads readers.
  • Avoid obvious comments: -- Select all columns above SELECT * adds no value.
  • Use a consistent style: Agree on spacing, capitalization, and placement with your team.

Common Mistakes to Avoid

Even experienced developers slip up with SQL comments. Watch for these pitfalls:

  • Forgetting the space after -- in MySQL: --comment is not a valid comment in MySQL; it must be -- comment. In SQL Server and PostgreSQL, --comment works, but the space improves readability.
  • Nesting block comments incorrectly: As noted, most databases do not support nested /* */. Use single-line comments inside block comments if needed.
  • Commenting out a line that contains a string with --: The -- inside a string literal is not a comment. Here's one way to look at it: SELECT '-- not a comment'; returns the string.
  • Leaving commented-out code in production: Dead code clutters queries and confuses readers. Remove it or move it to version control history.
  • Using comments to bypass security or logic: Comments should never hide malicious or unintended behavior. Review them during code reviews.

FAQ

Can I use # for comments in all SQL databases? No. # is supported in MySQL but not in SQL Server, PostgreSQL, Oracle, or SQLite. Use -- for portability.

Do SQL comments affect query performance? No. Comments are stripped by the parser before execution, so they have no runtime cost. Still, extremely large comment blocks can slightly increase parsing time—neg

Fresh Stories

Out This Week

Connecting Reads

We Thought You'd Like These

Thank you for reading about How Do You Comment In Sql. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home