TExcelTAD
Home › SQL › SQL Comments

SQL Comments

Comments are notes for humans. The database ignores them completely. Use them to explain why a query does something, to label sections of a long script, or to temporarily switch off a line while testing.

Single-line comments

Two dashes -- start a comment that runs to the end of the line:

sql · try it yourself
-- Top 3 products by stock value
SELECT name,
       ROUND(price * stock, 2) AS stock_value   -- price times units on hand
FROM products
ORDER BY stock_value DESC
LIMIT 3;
name                        | stock_value
----------------------------+------------
The SQL Handbook            | 1450
USB-C Cable                 | 1398.6
Noise-Cancelling Headphones | 1192
(3 rows)

Multi-line comments

/* … */ comments can span several lines, or sit in the middle of a line:

sql · try it yourself
/*
  Report: open orders
  Author: data team
  Shows every order that has not shipped and was not cancelled.
*/
SELECT order_id, customer_id, order_date
FROM orders
WHERE status = /* still being packed */ 'processing'
ORDER BY order_date
LIMIT 4;
order_id | customer_id | order_date
---------+-------------+-----------
9        | 5           | 2025-03-24
25       | 2           | 2025-07-28
36       | 22          | 2025-09-10
35       | 2           | 2025-09-11
(4 rows)

Commenting out code

While developing, comment out a condition to see what changes — without deleting it:

sql · try it yourself
SELECT name, price
FROM products
WHERE category_id = 6
  -- AND price < 50
ORDER BY price;
name                        | price
----------------------------+------
USB-C Cable                 | 9.99
Wireless Mouse              | 24.99
Power Bank                  | 29.5
Mechanical Keyboard         | 89
Noise-Cancelling Headphones | 149
(5 rows)

Putting AND at the start of each condition line (as above) means you can comment out any single line and the query still runs.

A whole statement can be disabled too:

sql · try it yourself
SELECT COUNT(*) AS products FROM products;
/* SELECT COUNT(*) AS customers FROM customers; */
-- SELECT COUNT(*) AS orders FROM orders;
products
--------
30
(1 row)

Comment styles by database

Style SQLite PostgreSQL MySQL SQL Server Oracle
-- comment yes yes yes (needs a space after --) yes yes
/* comment */ yes yes (can be nested) yes yes (can be nested) yes
# comment no no yes no no

Tip: Good comments explain intent: -- exclude test accounts created by QA is useful; -- select the name repeats the code and adds nothing.

Comments and security

Attackers abuse -- in SQL injection: if user input closes a string and then adds --, the rest of the query becomes a comment. That is one more reason never to paste user input into SQL text — see the SQL Injection lesson.

Test yourself

Which syntax starts a single-line comment in standard SQL?

  1. //
  2. #
  3. --
  4. <!--

How do you write a comment spanning several lines?

  1. -- at the start and end
  2. /* ... */
  3. { ... }
  4. ''' ... '''