SQL Comments

What are SQL Comments?

SQL comments are annotations in SQL code that are ignored by the SQL engine. They are used to explain or clarify parts of the code, making it easier for others (or yourself) to understand it in the future. Comments can also be helpful for debugging.

Types of SQL Comments

  • Single-line comments: These comments start with two dashes (--) and continue until the end of the line.
  • Multi-line comments: These comments are enclosed between /* and */ and can span multiple lines.

Examples of SQL Comments

-- This is a single-line comment

/* This is a 
multi-line comment */

SELECT * FROM Employees; -- This query selects all columns from the Employees table

Best Practices for Using Comments

  • Use comments to explain complex logic.
  • Keep comments up to date as code changes.
  • Avoid over-commenting; comments should add value.
  • Use comments to indicate TODOs or FIXMEs.

Practice Exercise

Add comments to the following SQL query to explain what each part of the code does:

SELECT * FROM Orders WHERE OrderDate > '2023-01-01';