Comments and commenting out code

Leave notes for the next reader, and switch lines off to debug a query one piece at a time.

Beginner5 min readLesson 8 of 38
In this lesson 3 sections
  1. Explain why, not what
  2. Commenting out code to debug
  3. A trick for safe commenting
Jump to practice

A comment is text the database ignores.

-- Single-line comment: everything after -- to the end of the line is ignored
 
SELECT opportunity_id,
       amount          -- a comment can also sit at the end of a line
FROM opportunities;
 
/* Block comment:
   can span several lines */

Explain why, not what

WHERE account_id <> 999   -- exclude the sandbox account used for CRM testing

That comment is useful: nobody could guess why 999 is excluded. -- filter accounts would just repeat the code.

For queries you save and share, add a short header:

/*
  Purpose : Open pipeline by stage for the weekly forecast call
  Source  : opportunities
*/
SELECT stage, amount
FROM opportunities
WHERE closed_at IS NULL;

Commenting out code to debug

When a query returns the wrong number, switch filters off one at a time and watch how the result changes:

SELECT opportunity_id, stage, amount
FROM opportunities
WHERE closed_at IS NULL
  -- AND amount >= 50000         -- switched off to compare row counts
  AND created_at >= '2026-10-01';

A trick for safe commenting

Two habits let you comment out any line without breaking the query:

SELECT opportunity_id
     , account_id
  -- , lead_source               -- leading commas: no dangling comma
FROM opportunities
WHERE 1 = 1                      -- always true, so every real filter starts with AND
  AND closed_at IS NULL
  -- AND amount >= 50000
;

With commas at the end of lines, commenting out the last column leaves a stray comma and an error.

Common mistake: a block comment that never closes with */ swallows the rest of the query. If an error makes no sense, look for one.

Try it

The query in the box should return open deals created in 2026, but one filter is wrong and it returns nothing. Don’t delete anything: comment out the bad line so the query works.

Fix it in the sandbox below, then press Check answer.

SQL sandbox

Run queries against the sample CRM tables (accounts, opportunities,users, contacts and opportunity_history), right in your browser. Nothing you run leaves your computer.

Back to the top of the lesson