liveBook · Manning

Comment

As explained in Learn SQL in a Month of Lunches

An SQL comment is text in an SQL statement or script that is ignored during execution and can document intent or context.

An SQL comment adds helpful context to a query without affecting execution. Use comments to explain a query’s purpose, record an author and date, or guide the next person who maintains the script. section 4.3

Choose a comment style that makes the boundaries and intent clear:

  • Use two consecutive hyphens for an inline comment; the comment continues to the next carriage return. section 4.3
  • In MySQL, use # for an inline comment, but remember that another RDBMS may not recognize it. section 4.3
  • Use /* and */ for comments spanning several lines or for reusable code, because the beginning and end are clearly marked. section 4.3 section 4.5

Explain a query’s purpose

/* This query returns 3 TitleNames ...with the most recent PublicationDate ...excluding the single most recent TitleName */ SELECT TitleName, PublicationDate FROM title ORDER BY PublicationDate DESC LIMIT 3 OFFSET 1; The database ignores the explanatory lines and executes the SELECT statement. A clear comment prevents readers from having to infer why the query sorts, limits, and skips rows. section 4.3

/*
# This query returns 3 random titles, but it wasn't what we needed
SELECT
    TitleName,
    PublicationDate
FROM title
LIMIT 3;
*/
-- This is the updated query, now ordered by most recent PublicationDate
SELECT
    TitleName,
    PublicationDate
FROM title
ORDER BY PublicationDate DESC
LIMIT 3;

Compare an outdated query commented out beside its replacement, showing how comments preserve context while keeping an alternative from executing. section 4.3

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains inline and block syntax, portability, and accidental whole-query commenting, but does not detail comment parsing internals.
Examples
Uses commented SELECT queries that return random rows, recent titles, skipped rows, and an updated replacement query.
What you can do after
Apply it via SQL comment syntax to document, disable, and clarify queries without changing executable results.
How it connects
Connects comments with SQL queries, scripts, reusable code, RDBMS portability, and stored procedures.

Also explained in

Compare how 10 books explain comment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.