SQL comments

SQL Tutorial

Here is a clean, practical guide to writing SQL comments, with syntax and best-practice examples for all major SQL databases.


What Are SQL Comments?

It allows you to add notes, explanations, or disable parts of code without affecting execution.

They are ignored by the SQL engine.


1. Single-Line Comments

Style A: -- (ANSI Standard)

  •  Works in PostgreSQL, SQL Server, Oracle, SQLite
  •  Supported in MySQL (unless sql_mode=ansi is off)

Style B: # (MySQL Only)


2. Multi-Line Comments (Block Comments)

  •  Works in MySQL, PostgreSQL, SQL Server, Oracle

Comment Out Code Blocks


Inline Comments


 Using Comments for Debugging Queries

Temporarily disable conditions:


 Best Practices

  •  Use comments to explain why, not what
  •  Avoid over-commenting obvious SQL
  •  Use block comments when disabling multiple lines
  •  Keep comments updated when queries change
  •  In production stored procedures, document logic heavily

Real-World Examples

Documenting business rules

Explaining calculations

Annotating JOIN logic


 

You may also like...