DEV Community

Cover image for Clean SQL with Comments: Syntax and Practices for All Engines
DbVisualizer
DbVisualizer

Posted on

Clean SQL with Comments: Syntax and Practices for All Engines

Writing clean SQL is about more than just correctness—it's about clarity. SQL comments help you communicate with others (or your future self) by adding context, notes, or temporarily disabling code. Let’s explore how to use comments effectively.

Using Comments in SQL

Two Main Types:

  • Single-line

    -- Pull users created in the last week
    SELECT * FROM users WHERE created_at >= CURRENT_DATE - INTERVAL '7 days';
    
  • Multi-line

    /*
      Use this version for reporting
      Includes only verified accounts
    */
    SELECT * FROM accounts WHERE verified = TRUE;
    

Why and When to Comment

Helpful When:

  • Writing complex logic
  • Sharing scripts with a team
  • Disabling old queries during testing

Avoid:

  • Leaving sensitive info
  • Explaining simple queries
  • Letting comments go stale

Different Engines, Different Capabilities

  • MySQL: Supports versioned comments /*! */.
  • PostgreSQL: COMMENT ON lets you label schema elements.
  • SQLite: Keeps it basic and portable.
  • SQL Server & Oracle: Great for documenting stored code and schema.

FAQ

How do I add comments in SQL?

Use -- for quick notes or /* */ for multiple lines or blocks.

Can I use comments to remove lines during testing?

Yes—wrap code in /* */ and it won’t run.

Are comments part of SQL standards?

Yes, both styles are widely supported with small engine-specific features.

Can comments be dangerous?

Only if they include secrets or mislead developers. Keep them clean and relevant.

Conclusion

SQL comments don’t take much effort but make a big difference. They’re your best tool for writing collaborative, maintainable SQL scripts. Read SQL Comment: A Comprehensive Guide for more insights.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️