SQL Comments

Updated · 7 min read
easyPostgreSQL 18sqlcommentsbasics

A comment is a note inside your SQL that the database skips. Use comments to explain a query to the next person who reads it (often you, three months later), and to switch part of a query off while you test it. SQL has two kinds: a line comment that starts with --, and a block comment between /* and */.

In One Minute

  • -- starts a line comment: everything after it, to the end of the line, is ignored.
  • /* ... */ is a block comment: it can cover part of a line or many lines.
  • Put -- in front of a line to switch it off while you test, without deleting it.
  • Good comments explain why a query does something, not what each keyword means.
  • MySQL also accepts # for line comments; it's MySQL only, so use -- everywhere.

Line Comments: --

Two hyphens start a comment that runs to the end of the line. It can sit on its own line or after some SQL:

SQL▶ Try it
-- Students in California, for the San Francisco newsletter
SELECT first_name, city
FROM students
WHERE state = 'CA'      -- state codes are stored in capitals
ORDER BY first_name;
Output
 first_name |     city
------------+---------------
 Advitha    | San Francisco
 Srihitha   | San Francisco
(2 rows)

The database reads only the SQL. The result is the same as without the comments.

Comments help the reader; the database reads only the SQL

Block Comments: /* */

A block comment starts with /* and ends with */. Everything between them is ignored, however many lines it covers, so it suits longer notes:

SQL▶ Try it
/*
  Courses that cost more than $400.
  Used for the scholarship list: these are the courses
  where a fee reduction helps most.
*/
SELECT title, fee_usd
FROM courses
WHERE fee_usd > 400
ORDER BY title;
Output
       title       | fee_usd
-------------------+---------
 Java Basics       |  450.00
 Java Collections  |  550.00
 Java OOP          |  500.00
 JavaScript Basics |  450.00
(4 rows)

A block comment can also sit in the middle of a line, such as SELECT title /* , fee_usd */ FROM courses. In PostgreSQL, block comments can even contain other block comments (the SQL standard allows this), which is handy when you switch off a piece of SQL that already has a comment in it.

Switching Part of a Query Off

Comments are the quickest way to test a change without losing the original. Put -- in front of a line and the database skips it. Here the filter is switched off, so every student comes back:

SQL▶ Try it
SELECT first_name, state
FROM students
-- WHERE state = 'CA'
ORDER BY first_name;
Output
 first_name | state
------------+-------
 Advitha    | CA
 Navika     | TX
 Srihitha   | CA
(3 rows)

Delete the two hyphens and the filter is back. This is called commenting out a line.

Two hyphens switch the filter off; deleting them switches it back on

A block comment switches off several lines at once:

SQL▶ Try it
SELECT
  title,
  /* technology,
  level, */
  fee_usd
FROM courses
ORDER BY course_id;
Output
       title       | fee_usd
-------------------+---------
 Java Basics       |  450.00
 Java OOP          |  500.00
 Java Collections  |  550.00
 SQL Basics        |  400.00
 JavaScript Basics |  450.00
 Git Basics        |  250.00
(6 rows)

Watch your commas when you comment out a column. If you switch off the last column in the list, the comma before it is left dangling:

SQL▶ Try it
SELECT
  title,
  fee_usd,
  -- technology
FROM courses;
Output
ERROR:  syntax error at or near "FROM"
Comment out the comma too, or the next word the database sees is FROM

Comments That Help

A good comment says why, not what. Anyone who knows SQL can see what WHERE state = 'TX' does. What they can't see is the reason behind it.

Not helpfulHelpful
-- select from students-- Texas students get the Central time zone reminders
-- where state is TX-- Only Texas for now; California has its own sign-up list
-- order by name-- Sorted by name so the printed list matches the sign-in sheet

Comment the parts that would surprise a reader: an unusual filter, a fixed date, a rule from the business. Leave out comments that only repeat the code; they go out of date the moment someone changes the query.

Common Mistakes

Forgetting to close a block comment. If */ is missing, everything after /* is part of the comment, including the rest of the query. Try it reports an error such as unterminated /* comment.

Commenting out the last column but not its comma. The comma before it is left over, and the database reports syntax error at or near "FROM".

Comments that repeat the code. -- select the title adds nothing and goes stale. Explain the reason instead.

Leaving test code switched off for good. A query full of commented-out lines is hard to read. Once a change is settled, delete the lines you switched off.

Practice

Challenge: Write a query that lists the first name and city of the students in Texas, laid out on several lines, one clause per line. Add a line comment that explains why someone might want this list, and a short block comment at the top that says who the query is for.

Show solution
SQL▶ Try it
/* Weekly reminder list for the Dallas class */
SELECT first_name, city
FROM students
-- Texas students are in the Central time zone, so they get a separate reminder
WHERE state = 'TX'
ORDER BY first_name;
Output
 first_name |  city
------------+--------
 Navika     | Dallas
(1 row)

Your comments can say anything that helps; the result is the same, because the database skips them. Navika is the only student in Texas.

Quick check: SQL comments

3 questions
1Which line comment is standard SQL, accepted by PostgreSQL, MySQL, SQL Server, Oracle and SQLite?
2What happens to SQL inside /* and */?
3Which comment is most useful?

Key Takeaways

You want toWrite
Add a note to the end of a lineWHERE state = 'TX' -- reason
Write a longer note/* several lines */
Switch one line off-- WHERE state = 'CA'
Switch several lines off/* ... */ around them

Was this tutorial helpful?

Lesson 7 of 115