π¬ SQL Comments are non-executable lines of text used to explain SQL code, improve readability, and document database scripts. Comments are ignored by the SQL parser during execution, making them useful for adding notes without affecting the behavior of SQL statements.
π What are SQL Comments?
SQL comments help developers understand the purpose of queries, tables, columns, and business logic. They are commonly used to document complex SQL statements, temporarily disable code during testing, and improve collaboration among team members.
Information
π Types of SQL Comments
SQL supports two main types of comments:
- β‘οΈ Single-line comments
- β‘οΈ Multi-line (block) comments
1οΈβ£ Single-Line Comments
A single-line comment begins with --. Everything after -- on the same line is treated as a comment.
Single-Line Comment
-- Retrieve all student records
SELECT * FROM Students;You can also place a single-line comment after an SQL statement.
Comment After a Statement
SELECT * FROM Students; -- Display all students2οΈβ£ Multi-Line (Block) Comments
Multi-line comments begin with /* and end with */. They are useful for writing longer explanations or commenting out multiple lines of SQL code.
Multi-Line Comment
/*
This query retrieves all students
from the Computer Science department.
*/
SELECT *
FROM Students
WHERE Department = 'Computer Science';π‘ Why Use SQL Comments?
- π Explain the purpose of SQL queries.
- π₯ Improve collaboration within development teams.
- π οΈ Make complex SQL scripts easier to understand.
- π§ͺ Temporarily disable queries during testing.
- π Document business rules and database logic.
π§ͺ Commenting Out SQL Code
During development, comments can be used to temporarily disable SQL statements without deleting them.
Disable a Query
-- DELETE FROM Students
-- WHERE StudentID = 101;
SELECT * FROM Students;You can also comment out multiple lines using a block comment.
Disable Multiple Statements
/*
UPDATE Students
SET Department = 'Information Technology'
WHERE StudentID = 101;
DELETE FROM Students
WHERE StudentID = 102;
*/π Comparison of SQL Comment Types
| Comment Type | Syntax | Best Used For |
|---|---|---|
| Single-Line | -- Comment | Short notes or explanations. |
| Multi-Line | /* Comment */ | Detailed documentation or multiple lines. |
πΌ Real-World Example
Documented SQL Query
-- Retrieve active employees ordered by name
SELECT EmployeeID,
FullName,
Department
FROM Employees
WHERE IsActive = TRUE
ORDER BY FullName ASC;The comment clearly explains what the query does before the SQL statement begins, making the script easier to maintain.
β οΈ Best Practices
Best Practice
π« Common Mistakes
| Mistake | Why It's a Problem | Recommendation |
|---|---|---|
| Outdated comments | They can mislead developers. | Update comments whenever code changes. |
| Too many unnecessary comments | They reduce readability. | Comment only where additional explanation is helpful. |
| Using comments to hide unused code permanently | Makes scripts cluttered. | Remove obsolete code after testing. |
π Key Points to Remember
- π SQL comments are ignored during execution.
- π Use -- for single-line comments.
- π Use /* ... */ for multi-line comments.
- π Comments improve readability and maintainability.
- π Keep comments accurate and relevant.