A comment is text inside a SQL statement that the database ignores entirely, meant purely for whoever reads the query later. Every lesson's code examples in this course have used them already, this lesson covers the syntax directly.
Learning Objectives
- Write a single-line comment with --.
- Write a multi-line block comment with /* */.
- Know the one extra comment style specific to MySQL and MariaDB.
Single-Line Comments
-- starts a comment that runs to the end of the line, works identically across every database in this course, and can appear on its own line or trailing after real SQL on the same line.
-- Only return employees earning over 90,000 SELECT first_name, salary FROM employees WHERE salary > 90000; -- filters after the join, not before
Block Comments
/* starts a block comment, * / ends it, spanning any number of lines in between, also identical across every database here. Block comments can also appear mid-statement, around a single clause, not just surrounding the whole query.
/* This query powers the quarterly budget report. Last reviewed: see Module 3, AVG() lesson. */ SELECT department_id, AVG(salary) AS average_salary FROM employees GROUP BY department_id;
MySQL and MariaDB's Extra Style
| Syntax | Databases |
|---|---|
| -- comment | All six (note: MySQL/MariaDB require a space after --) |
| /* comment */ | All six |
| # comment | MySQL / MariaDB only |
MySQL and MariaDB specifically require a space (or another whitespace character) immediately after --, --comment with no space is parsed differently. The other databases in this course don't have this restriction.
Try It Yourself
Add a single-line comment explaining why this query filters on department_id, and a block comment above it noting who it's for.
/* For the HR team's Engineering headcount review. */ SELECT first_name FROM employees WHERE department_id = 1; -- Engineering only, see joins-overview lesson for the schema
Frequently Asked Questions
Do comments affect query performance?
No, comments are stripped before the query is parsed and executed, they have zero effect on how a query runs or performs.
Can block comments be nested, a comment inside a comment?
No, in every database in this course, the first */ encountered closes the block comment, regardless of how many /* appeared before it. Nested comments aren't supported.
Common Beginner Mistakes
Using # comments outside MySQL/MariaDB
# isn't a comment marker on PostgreSQL, SQLite, SQL Server, or Oracle, it's parsed as part of the SQL itself (or causes a syntax error). Stick to -- or /* */ for anything meant to be portable.
Forgetting to close a block comment
A missing */ comments out everything after it, potentially the rest of the entire query, producing a confusing "incomplete statement" error far from the actual mistake.
Best Practices
- Use -- for short, single-line notes, and /* */ for longer explanations spanning multiple lines.
- Avoid # for anything that might ever run on a database other than MySQL/MariaDB.
- Comment the why, not the what, a query's syntax already shows what it does, a comment is most useful explaining a non-obvious reason.
Interview Questions
What are the two portable comment styles supported by every database in this course?
-- for a single-line comment running to the end of the line, and /* ... */ for a block comment spanning any number of lines. Both are part of the ANSI SQL standard.
Why might a # comment work on one database and fail on another?
# as a comment marker is a MySQL/MariaDB-specific extension, not part of the ANSI standard. On PostgreSQL, SQLite, SQL Server, or Oracle, it isn't recognized as a comment at all, and either causes a syntax error or is parsed as literal SQL.
Summary
-- (single-line) and /* */ (block) are portable comment syntax across every database in this course. # is an extra single-line style specific to MySQL and MariaDB. Comments are stripped before execution and have no effect on performance.
What's Next?
This module closes with a full SQL Operators Reference, pulling comparison, logical, and arithmetic operators together in one place, including a few, like modulo and string concatenation, not covered yet.
Ready to Practice SQL?
Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.
Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.