Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 42

Comments in SQL

What Are Comments in SQL?

A long SQL query can produce the correct result and still be difficult to maintain. Six months later, a teacher, developer or database administrator may remember what the query does but not why a condition was added. A SQL comment solves that problem: it records an explanation for people while the database ignores it during normal query execution.

💡 Simple definition

Comments in SQL are explanatory notes placed inside a script or query. They make code easier to understand, test and maintain without becoming part of the query result.

Good comments explain intent, business rules or unusual decisions. For example, -- Exclude students who withdrew before the exam is useful because it tells the next reader why a filter exists. A comment such as -- Select rows above a simple SELECT statement adds little value.

Comments are commonly used to document database scripts, temporarily disable a statement during testing, separate script sections and leave safety instructions before an UPDATE or DELETE query.

Three Comment Styles Supported by MySQL

MySQL supports three useful comment forms. The first two end at the current line; the third can cover part of a line or several lines.

1. Double hyphen: --

MySQL – Standard single-line comment
-- Show active students only
SELECT roll_no, name
FROM students
WHERE status = 'Active';

Important MySQL rule: place whitespace after the two hyphens. Write -- explanation, not --explanation. This rule prevents arithmetic expressions such as credit--1 from being misread.

2. Hash sign: #

MySQL – Hash comment
# Monthly fee report for Class XII
SELECT student_id, amount
FROM fee_payments
WHERE payment_month = 'August';

The hash form is convenient in MySQL, but it is not part of standard SQL. Prefer -- when a script may later move to PostgreSQL, SQL Server, Oracle or another database.

3. Block comment: /* ... */

MySQL – Multi-line comment
/* Annual result report
   Prepared for Classes IX to XII
   Reviewed on 14 August 2026 */
SELECT class_name, AVG(percentage) AS class_average
FROM results
GROUP BY class_name;

A block comment begins with /* and ends with */. It is suitable for a file header, a detailed explanation or a short inline note. Do not nest one block comment inside another; the first closing marker may end the comment earlier than expected.

Working SQL Comment Examples with Output

The following examples use a small school table so that you can run every query and verify the result.

MySQL – Sample data
CREATE TABLE students (
    roll_no INT PRIMARY KEY,
    name VARCHAR(50),
    class_name VARCHAR(10),
    status VARCHAR(10)
);

INSERT INTO students VALUES
(1, 'Aarav', 'X-A', 'Active'),
(2, 'Meera', 'X-A', 'Active'),
(3, 'Kabir', 'X-B', 'Inactive');
Output:
Query OK, 3 rows affected

Example 1: A comment on its own line

MySQL – SELECT with comment
-- Only currently enrolled students appear in the report
SELECT roll_no, name
FROM students
WHERE status = 'Active'
ORDER BY roll_no;
Output:
roll_no | name
1 | Aarav
2 | Meera

The sentence beginning with -- is ignored. The WHERE condition, not the comment, removes Kabir from the result.

Example 2: An inline block comment

MySQL – Inline comment
SELECT name, /* shown on the ID card */ class_name
FROM students
WHERE roll_no = 2;
Output:
name | class_name
Meera | X-A

Because /* ... */ can appear between SQL tokens, it documents one selected column without disabling the remaining statement.

Example 3: Temporarily disable one condition

MySQL – Testing a filter
SELECT roll_no, name, class_name
FROM students
WHERE status = 'Active'
/* AND class_name = 'X-A' */
ORDER BY roll_no;
Output:
roll_no | name | class_name
1 | Aarav | X-A
2 | Meera | X-A

The class filter is temporarily ignored while the active-status filter still runs. This is useful during debugging, but remove obsolete commented-out code before a production release.

SQL Comment Syntax Compared

SyntaxScopePortabilityBest use
-- commentTo end of lineStandard SQLShort explanations and safety notes
# commentTo end of lineMySQL-specificMySQL-only scripts and command-line work
/* comment */One or more linesWidely supportedHeaders, detailed notes and inline explanations
⚠️ Advanced MySQL exception

MySQL also recognises version-specific executable comments such as /*!80000 SQL_CODE */. Unlike ordinary comments, compatible MySQL versions may execute the text inside them. Beginners should not treat this special form as a normal documentation comment.

Practical Uses and Best Practices

  • Explain the reason: write why a filter, join or calculation is required, especially when the reason is not obvious.
  • Record assumptions: note whether marks exclude absentees, fees include late charges or dates use a specific time zone.
  • Mark safe execution: before changing data, remind the reader to run the matching SELECT first and confirm the target rows.
  • Separate a long script: clear headings such as -- 1. Create tables and -- 2. Insert master data improve navigation.
  • Keep comments current: an outdated comment is more dangerous than no comment because it gives false confidence.
  • Never store secrets: passwords, API keys and personal student data do not belong in comments; scripts may be shared or committed to Git.

When documenting a data-changing query, also follow the safeguards explained in Safe UPDATE and DELETE Practices.

Common Mistakes While Commenting in MySQL

  • Missing whitespace after --: --wrong may not start a MySQL comment. Use -- correct explanation.
  • Expecting a line comment to continue: both -- and # stop at the newline. Add a marker on every line or use a block comment.
  • Forgetting */: an unfinished block comment can hide the rest of the script or produce a syntax error.
  • Nesting block comments: MySQL does not safely support arbitrary nested /* ... */ comments.
  • Using # in portable SQL: another DBMS may reject the file. Use the standard double-hyphen form.
  • Writing what the code already says: explain the rule or decision instead of translating every SQL keyword into English.

Practice Questions

🏋️ Hands-on task

Create a table named books. Write one SELECT query containing a standard single-line comment, one inline block comment and a multi-line file header. Run the statement again after removing each comment and confirm that the output remains unchanged.

  1. Write a single-line comment that explains why inactive students are excluded from a report.
  2. Temporarily disable AND city = 'Khurja' without deleting it.
  3. Explain why --comment is unsafe in MySQL but -- comment is correct.
  4. Which comment form would you choose for a script shared across different database systems, and why?

Quick Summary

  • SQL comments document a query but are normally ignored during execution.
  • -- is the preferred portable single-line syntax; include whitespace after the hyphens in MySQL.
  • # is a MySQL-specific single-line comment.
  • /* ... */ creates an inline or multi-line block comment.
  • Useful comments explain intent, rules and risks; they do not repeat obvious SQL.
  • Keep comments accurate and never place passwords or sensitive data inside them.

Official References

References reviewed on 14 August 2026. Queries, outputs and explanations are original teaching material.

Frequently Asked Questions

How do you write a comment in MySQL?
Use -- followed by a space for a standard single-line comment, # for a MySQL-specific single-line comment, or /* ... */ for a block comment that can span one or more lines.
Why is a space required after two hyphens in MySQL?
In MySQL, -- begins a comment only when it is followed by whitespace or a control character. Writing --text may not be treated as a comment, so prefer -- text.
What is the difference between -- and # comments in MySQL?
Both can comment to the end of the current line in MySQL. -- is standard SQL and more portable, while # is a MySQL extension and may fail in other database systems.
Can SQL block comments cover multiple lines?
Yes. Text placed between /* and */ is ignored as a block comment and may cover multiple lines. Avoid nested block comments because they are not reliably supported.
Do SQL comments affect query output?
Normal comments are ignored by the SQL parser and do not change the result. They still improve maintenance by documenting intent, assumptions and safety notes. MySQL executable comments are an advanced exception.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.