Part 2: Mastering SQL Comments

Part 2: Mastering SQL Comments | SQL Challenges & Capstone
SQL Business Logic & Data Handling Practice · Part 2 · Lecture Notes

Mastering SQL Comments

How to document SQL queries using single-line and multi-line comments β€” explaining business logic, improving readability, and supporting team collaboration.

Part 2 of 633% Complete
— Single-Line
/* Multi-Line */
Query Documentation
Business Rules
8 Practice Questions

Learning Objectives

  • Write single-line comments using — to annotate individual lines of SQL
  • Write multi-line comments using /* */ to document larger sections of query logic
  • Use comments to explain the business purpose of a query and the reasoning behind filters
  • Understand that comments are completely ignored when SQL executes β€” they only serve human readers
Click any section header to open or close it

SQL comments allow analysts to add notes and explanations inside SQL queries. Comments are completely ignored by the database when the query executes β€” they exist only for humans reading the code. Well-commented SQL is easier to maintain, easier to hand off to colleagues, and easier to return to weeks or months later.

In industry, analysts use comments to explain SQL logic, document business rules behind filters and calculations, improve team collaboration, organize complex multi-step queries, and make reports easier to understand for non-technical reviewers.

Two Types of SQL Comments

Single-line comments begin with — (two hyphens) and apply to the rest of that line only. Multi-line comments are wrapped in /* and */ and can span as many lines as needed. Both types are completely ignored when SQL runs.

Single-line comments start with two hyphens (–) and extend to the end of the current line. Everything after — on that line is treated as a comment. Single-line comments are best for short explanations β€” describing what a WHERE condition does, why a specific value was chosen, or what a query is for.

When to Use Single-Line Comments

Use — for brief annotations: explaining a WHERE condition, labelling a SELECT column, or giving the query a one-line purpose statement at the top. If the explanation needs more than one line, switch to a multi-line comment.

Single-Line Comment β€” Query Purpose
— Show all users from Canada SELECT * FROM users WHERE country = ‘Canada’;
Inline Comment β€” Explaining a Calculation
SELECT transaction_id, — Round transaction amount to 2 decimal places ROUND(amount, 2) AS amount FROM transactions;

Multi-line comments open with /* and close with */ and can contain any number of lines in between. They are best for longer explanations β€” describing the business purpose of a query, documenting a set of filtering conditions, or providing context that a single line cannot capture.

When to Use Multi-Line Comments

Use /* */ when the explanation runs to two or more lines, when documenting the business context of a query, or when temporarily disabling a block of SQL during testing. Everything between /* and */ is ignored by the database.

Multi-Line Comment β€” Business Context
/* Retrieve high-value transactions for financial review */ SELECT * FROM transactions WHERE amount > 100;

In professional environments, SQL queries are shared, reviewed, and maintained by multiple people. Comments turn a query from a set of cryptic commands into documented, self-explanatory code. A colleague reading a well-commented query understands not just what it does but why it was written that way.

Professional Documentation Standard

At minimum, professional SQL queries include: a comment at the top explaining the business purpose, comments on any non-obvious WHERE conditions, and inline comments on complex calculations. This is the standard in analytics teams and BI departments.

Documented Query β€” Active Subscriptions
— Retrieve active subscriptions only SELECT * FROM subscriptions WHERE status = ‘Active’;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Add a single-line comment explaining a query that retrieves all users
AI Prompt
Write a MySQL query with a single-line comment explaining a query that retrieves all users.
SQL Answer
— Retrieve all users SELECT * FROM users;
Practice 2 Add a multi-line comment explaining a query that retrieves transactions above 50 dollars rounded to 2 decimal places
AI Prompt
Write a MySQL query with a multi-line comment explaining a query that retrieves transactions above 50 dollars rounded to 2 decimal places.
SQL Answer
/* Retrieve transactions greater than 50 dollars */ SELECT transaction_id, ROUND(amount, 2) AS amount FROM transactions WHERE amount > 50;
Practice 3 Add a single-line comment explaining a query that retrieves all products
AI Prompt
Write a MySQL query with a single-line comment explaining a query that retrieves all products.
SQL Answer
— Retrieve all products SELECT * FROM products;
Practice 4 Add a multi-line comment explaining a query that retrieves completed orders
AI Prompt
Write a MySQL query with a multi-line comment explaining a query that retrieves completed orders.
SQL Answer
/* Retrieve completed customer orders */ SELECT * FROM orders WHERE order_status = ‘Completed’;
Practice 5 Add a comment explaining a query that retrieves active subscriptions
AI Prompt
Write a MySQL query with a comment explaining a query that retrieves active subscriptions.
SQL Answer
— Retrieve active subscriptions SELECT * FROM subscriptions WHERE status = ‘Active’;
Practice 6 Add a comment explaining a query that retrieves open support tickets
AI Prompt
Write a MySQL query with a comment explaining a query that retrieves open support tickets.
SQL Answer
— Retrieve open support tickets SELECT * FROM support_tickets WHERE ticket_status = ‘Open’;
Practice 7 Add a multi-line comment explaining a query that retrieves campaigns using Email channels
AI Prompt
Write a MySQL query with a multi-line comment explaining a query that retrieves campaigns using Email channels.
SQL Answer
/* Retrieve campaigns using Email channels */ SELECT * FROM campaigns WHERE channel = ‘Email’;
Practice 8 Add a comment explaining a query that retrieves sessions from Mobile devices
AI Prompt
Write a MySQL query with a comment explaining a query that retrieves sessions from Mobile devices.
SQL Answer
— Retrieve sessions from Mobile devices SELECT * FROM sessions WHERE device_type = ‘Mobile’;

Knowledge Check

Five questions β€” select the best answer for each.

1. What happens when SQL runs a query that contains comments?
AThe comment text is displayed as an extra row in the results
BComments cause a syntax error
CComments are completely ignored β€” only the SQL code executes
DComments slow down query performance
2. Which symbol starts a single-line SQL comment?
A//
B#
C/*
D—
3. Which syntax correctly wraps a multi-line SQL comment?
A// comment text //
B/* comment text */
C— comment text —
D** comment text **
4. Why do professional analysts add comments to SQL queries?
ATo make queries run faster
BTo filter out unwanted rows
CTo explain logic, document business rules, and support collaboration
DTo prevent other users from editing the query
5. An analyst writes — Retrieve active subscriptions before a WHERE clause. What effect does this have on the query result?
AThe query only returns subscriptions labelled active in the comment
BNo effect β€” the comment is ignored and the WHERE clause still filters normally
CThe comment replaces the WHERE clause
DThe query returns an error because comments cannot appear before WHERE

Part 2 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

SQL comments help analysts document query logic, explain business rules, and improve collaboration and readability. Use — for single-line notes. Use /* */ for multi-line documentation. Comments are invisible to the database and have no effect on query results.

—/* */Single-Line CommentMulti-Line CommentDocumentationBusiness RulesCollaborationReadability