SQL Keywords

SQL COMMENTS

What are SQL COMMENTS?

SQL COMMENTS let you add notes that the database engine ignores, improving readability, maintainability, and debugging.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL COMMENTS: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift, DB2 (minor syntax differences in hints)

SQL COMMENTS Full Explanation

SQL COMMENTS are non-executing text fragments inserted into SQL code to describe intent, disable portions of code, or leave metadata for future readers. The parser discards comment text before the statement reaches the optimizer, so comments have no impact on results or performance (except negligible parsing time). SQL supports two primary forms:1. Single-line comment that begins with two hyphens (--) and extends to the end of that line.2. Block or multi-line comment enclosed by /* and */.Placement is flexible: at the start, end, or inside a statement, between tokens. They can also appear in DDL objects like views, stored procedures, and functions. Most dialects do not allow nested block comments, and forgetting the closing */ causes a syntax error. Some vendors extend the block comment syntax for optimizer hints (e.g., /*+ hint */ in Oracle), so you must avoid leading plus signs unless intentionally providing a hint.

SQL COMMENTS Syntax

-- single line comment
SELECT * FROM customers;  -- fetch all customers

/* multi-line or block comment
   that can span several lines */
SELECT id, name FROM customers;

SQL COMMENTS Parameters

Example Queries Using SQL COMMENTS

-- Single line comment before statement
-- Get active users created this month
SELECT id, email
FROM users
WHERE created_at >= date_trunc('month', CURRENT_DATE);

/* Temporarily disable a filter
SELECT *
FROM orders
WHERE status = 'completed';
*/

/* Annotate complex join */
SELECT o.id,
       c.name,
       o.total
FROM orders o  /* sales orders */
JOIN customers c ON c.id = o.customer_id;

Expected Output Using SQL COMMENTS

  • All queries run normally because the database ignores the commented lines
  • Only executable SQL outside the comment blocks affects the result set

Use Cases with SQL COMMENTS

  • Document complex joins, business rules, and calculations
  • Leave TODO notes for collaborators
  • Disable parts of a query while testing
  • Embed metadata or version tags in migration scripts
  • Add optimizer hints (vendor-specific)

Common Mistakes with SQL COMMENTS

  • Forgetting to close a block comment, leading to unterminated comment errors
  • Assuming nested block comments are allowed (most dialects reject them)
  • Placing a comment inside a string literal by accident
  • Using /*+ */ without understanding that it can trigger optimizer hints
  • Relying on comments for security (they are visible in source code)

Related Topics

SQL HINTS, EXPLAIN, DEBUGGING SQL, VIEW definition comments

First Introduced In

SQL-92

Frequently Asked Questions

How do I add a comment to a SQL statement?

Start a single line with -- or wrap multiple lines between /* and */. The database will ignore that text.

Are SQL comments executed?

No. Comments are removed during parsing and never reach the optimizer or executor.

Can I nest block comments?

Most databases do not support nested /* */ comments. Nesting causes a syntax error unless your dialect explicitly supports it (e.g., PL/SQL supports a variant).

Do comments affect performance?

Apart from a trivial increase in parsing time, comments have no measurable impact on execution speed.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!