SQL Comments
beginnerbasics
2 min read
What are SQL Comments?
Comments are notes within your SQL code that the database engine ignores during execution. They help you and your teammates understand the purpose of queries, explain complex logic, and temporarily disable parts of a query.
Syntax
Single-line comment (everything after -- is ignored):
-- This is a single-line comment
SELECT * FROM students;Multi-line (block) comment:
/* This is a
multi-line comment */
SELECT * FROM students;When to Use
- Explaining why a query uses a specific approach
- Documenting business rules embedded in complex queries
- Temporarily disabling a clause while debugging
- Adding context for future maintainers
Key Points
- Single-Line — Use
--followed by a space. Everything after it on the same line is a comment. - Multi-Line — Wrap text between
/*and*/. Can span multiple lines. - No Nesting — PostgreSQL does not support nested block comments.
- No Performance Impact — Comments are stripped before execution and have zero runtime cost.
- Best Practice — Comment complex WHERE conditions, CTEs, and business logic, but avoid over-commenting obvious code.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL Comments Challenge
Write a query that solve this task: add a note above a query.
Expected result
Rows where age is greater than 18. The comment is ignored by PostgreSQL.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What are SQL Comments?
Initializing database...Each run starts from fresh sample data.
More Examples
Inline comment
Add a comment at the end of a line.
Initializing database...Each run starts from fresh sample data.
Multi-line comment
Use a block comment to describe a query.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Do comments affect query performance?
No. The database parser strips comments before executing the query, so there is zero performance impact.
Can I nest block comments in PostgreSQL?
PostgreSQL actually does support nested block comments, unlike some other databases. You can write /* outer /* inner */ still outer */.