SQL Comments

SQL comments are used to document queries and improve readability. They are ignored during execution.

On this page

SQL Comments

SQL comments are used to explain code and improve readability. Comments are ignored by the database engine during execution.

Single-Line Comments

Use two hyphens (--) to create a single-line comment.

-- This query selects all customers
SELECT * FROM customers;

Multi-Line Comments

Use /* */ to create multi-line comments.

/*
  This query retrieves
  active customers only
*/
SELECT *
FROM customers
WHERE status = 'active';

MySQL Special Comment Syntax

MySQL supports version-specific comments using /*! ... */.

SELECT /*!40101 SQL_NO_CACHE */ *
FROM customers;

This syntax executes only on MySQL versions that support the feature.

Comments in Stored Procedures

DELIMITER //

CREATE PROCEDURE ExampleProcedure()
BEGIN
  -- Select all customers
  SELECT * FROM customers;
END //

DELIMITER ;

Best Practices

  • Use comments to explain complex queries
  • Avoid obvious or redundant comments
  • Keep comments updated with code changes

Common Mistakes

  • Using incorrect comment syntax
  • Forgetting to close multi-line comments
  • Leaving outdated comments in production code

Next Step

Continue with SQL Operators to review comparison, logical, and arithmetic operators used in SQL.