SQL Comments
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.
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?
Press Run to execute the query once the engine is ready.
More Examples
Inline comment
Add a comment at the end of a line.
Press Run to execute the query once the engine is ready.
Multi-line comment
Use a block comment to describe a query.
Press Run to execute the query once the engine is ready.