SQL Comments

πŸ’¬ 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

Comments are intended for humans, not the database engine. They do not affect the execution of SQL statements.

πŸ“ 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 students

2️⃣ 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 TypeSyntaxBest Used For
Single-Line -- CommentShort 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

Write clear and concise comments, keep them up to date when the SQL code changes, avoid stating the obvious, and use comments to explain why a query exists rather than simply describing what the SQL syntax already shows.

🚫 Common Mistakes

MistakeWhy It's a ProblemRecommendation
Outdated commentsThey can mislead developers.Update comments whenever code changes.
Too many unnecessary commentsThey reduce readability.Comment only where additional explanation is helpful.
Using comments to hide unused code permanentlyMakes 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.
>>"Good comments don't replace clean SQLβ€”they explain the intent behind it."

Summary

βœ… SQL comments are an essential documentation tool that helps developers write readable, maintainable, and collaborative database scripts. By using both single-line and multi-line comments appropriately, you can make SQL code easier to understand without affecting its execution.