SQL Comments
Comments are notes or explanations you add inside your SQL code to make it easier to understand — both for yourself and others reading your queries later. They don’t affect how the SQL runs. ✍️ Why Use Comments? 🛠️ How to Write Comments in SQL There are two main ways to add comments: 1. Single-line […]
Comments are notes or explanations you add inside your SQL code to make it easier to understand — both for yourself and others reading your queries later. They don’t affect how the SQL runs.
✍️ Why Use Comments?
- Explain complex queries
- Temporarily disable code during debugging
- Document assumptions or reminders
- Improve code maintainability when working in teams
🛠️ How to Write Comments in SQL
There are two main ways to add comments:
1. Single-line Comments
Start with two hyphens (--)
Everything after -- on that line is a comment.
SELECT * FROM employees; -- Get all employee records2. Multi-line (Block) Comments
Enclosed between /* and */
Can span multiple lines.
/*
This query fetches all employees
whose salary is above 50000
*/
SELECT * FROM employees WHERE salary > 50000;💡 Tips for Using Comments
- Use comments to clarify why you wrote something, not what the code does (the code itself should be clear).
- Avoid excessive commenting — keep comments relevant and concise.
- Remove or update comments when the code changes.
🧠 Quick Recap
| Comment Type | Syntax | Use Case |
|---|---|---|
| Single-line | -- Comment | Quick notes, short explanations |
| Multi-line | /* Comment */ | Longer descriptions, blocks |
Was this helpful?
Thanks for your feedback!