How to Comment in SQL

Galaxy Glossary

How do you add single-line and multi-line comments in SQL?

Commenting in SQL means adding single-line or multi-line annotations that the database ignores but humans read for context, debugging, and collaboration.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

How to Comment in SQL

Learn single-line, multi-line, and database-specific comment syntax, plus Galaxy shortcuts and best practices.

What Is Commenting in SQL?

Commenting in SQL adds explanatory text that the database engine skips during execution. Comments improve readability, document business logic, and help teams debug queries without altering results.

Why Do Developers Comment SQL Code?

Developers comment SQL to record intent, flag TODOs, mark temporary changes, and onboard new teammates. Well-placed comments reduce misunderstandings and cut maintenance time.

How Do You Write Single-Line Comments in SQL?

Use two dashes (--) followed by text. Everything after -- until the line break is ignored by the engine.-- return newest orders
SELECT * FROM orders ORDER BY created_at DESC;

How Do You Write Multi-Line Comments in SQL?

Wrap text between /* and */. The engine skips all characters inside, even across lines./*
Join orders and customers
Filter to last 30 days only
*/
SELECT ...

Which Comment Syntax Do Popular Databases Support?

PostgreSQL, MySQL, SQLite, SQL Server, and Oracle accept -- and /* */. SQL Server also supports // inside its CLI, while BigQuery supports #. Always check vendor docs for edge cases.

Can You Nest SQL Comments?

Most engines reject nested /* */ comments. Attempting to close an inner block early throws a syntax error. Use single-line -- for inner notes instead.

How to Comment Out Code Quickly in Galaxy?

Galaxy’s SQL editor toggles comments with ⌘ / (Mac) or Ctrl / (Win/Linux). Select lines and trigger the shortcut to prepend or remove --. Multi-line blocks auto-wrap in /* */ when multiple lines are selected.

Best Practices for SQL Comments

Keep comments concise, action-oriented, and up-to-date. Document why a query exists, not what obvious SQL already shows. Use consistent terminology and include ticket links for traceability.

What Are Common Pitfalls When Commenting SQL?

Avoid disabling critical filters without a TODO, leaking credentials in comments, and using nested block comments. Also, remember comments remain in query logs—sanitize sensitive data.

Real-World Example of Commented Query

/* revenue_growth.sql
Computes MoM revenue for dashboards
Owner: data-eng@acme.com */
WITH monthly AS (
SELECT DATE_TRUNC('month', paid_at) AS month,
SUM(amount) AS revenue
FROM payments -- includes refunds already netted
WHERE status = 'paid'
GROUP BY 1
)
SELECT month,
revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS mom_growth
FROM monthly;

Why How to Comment in SQL is important

Commenting is crucial because SQL often powers business-critical dashboards and ETL pipelines. Clear annotations prevent misinterpretation, reduce onboarding time, and speed up debugging. In collaborative environments, comments paired with version control and tools like Galaxy enable teams to share context without leaving the editor.

How to Comment in SQL Example Usage


/* Example: Using comments to disable a filter for debugging */
SELECT *
FROM users
-- WHERE is_active = true; -- temporarily disabled to inspect all users

How to Comment in SQL Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

Frequently Asked Questions

Does commenting slow down query execution?

No. The SQL parser strips comments before planning, so performance is unchanged.

How can I bulk-comment lines in Galaxy?

Select multiple lines and press ⌘ / (Mac) or Ctrl / (Win/Linux). Galaxy toggles -- on each line or wraps the selection in /* */.

Are comments stored in the database?

Comments in ad-hoc queries exist only in logs or saved files. However, comments in database objects like views are stored with the object definition.

Can I use emojis in SQL comments?

Most engines allow UTF-8, so emojis render, but avoid them in production code for professionalism and compatibility.

Want to learn about other SQL terms?