Learn With Grito

SQL Comments
Explained for Beginners

A small query might be easy to understand. A 200-line business analysis query with multiple joins, filters, calculations, and subqueries? Not so much. That is where SQL comments help.

Tutorial Series6 Mins ReadSQL Level 8

SQL queries can be simple. But real SQL work gets complicated fast.

A small query might be easy to understand. A 200-line business analysis query with multiple joins, filters, calculations, and subqueries? Not so much.

That is where SQL comments help. SQL comments let you explain your logic directly inside your queries. If you are learning SQL for data analysis, interviews, or real projects, comments are a habit worth building early. Because writing SQL is one skill. Writing SQL that humans can understand later is another.

In this guide, you’ll learn:

  • what SQL comments are
  • why data analysts use them
  • single-line comments
  • multi-line comments
  • real business examples
  • debugging use cases
  • common mistakes
  • interview relevance

This is one of the simplest SQL concepts—but one of the most underrated.

What Are SQL Comments?

SQL comments are notes written inside SQL code that the database ignores. They help explain what your query is doing.

Example
-- Get active premium customers
SELECT *
FROM customers
WHERE is_active = TRUE
AND is_premium = TRUE;

The comment -- Get active premium customers is ignored by SQL execution. It exists only for humans.

Simple idea: Comments explain logic without affecting query execution.

Why SQL Comments Matter for Data Analysts

Beginners often skip comments. That works when your query is tiny. But analyst queries quickly become complex.

Analyst Query
-- High-value customers since Jan 2025
SELECT
   c.customer_name,
   SUM(o.revenue) AS total_revenue
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.order_date >= '2025-01-01'
AND o.status = 'Completed'
GROUP BY c.customer_name
HAVING SUM(o.revenue) > 50000;

You may understand it now. But later? Or if a teammate opens it? Comments help. Much clearer.

Analyst use cases:

  • documenting business logic
  • explaining filters
  • debugging queries
  • temporarily disabling code
  • helping teammates understand analysis
  • preserving context for future you

Future you counts as another person.

1. Single-Line Comments in SQL

Single-line comments begin with --. Everything after that on the same line is ignored.

Example
-- Pull customer data
SELECT *
FROM customers;

How Single-Line Comments Work:

Example
SELECT *
FROM customers
-- Only active users
WHERE is_active = TRUE;

Best Use Cases for Single-Line Comments:

  • quick explanations
  • section labels
  • business assumptions
  • temporary debug notes

2. Multi-Line Comments in SQL

Multi-line comments use /* ... */. Everything inside gets ignored.

Example
/*
Revenue analysis
Excludes refunds
Includes only completed orders
*/
SELECT *
FROM orders;

Best Use Cases for Multi-Line Comments:

  • detailed explanations
  • query documentation
  • assumptions
  • analysis context
  • multi-step business logic notes

The Grito Factor Some of the most expensive analytics mistakes happen because someone misunderstood an old query. Not because the SQL was wrong. Because the logic was undocumented. A two-line comment can save hours—or prevent bad business decisions.

Using Comments for Debugging

One of the most practical uses. Suppose a query is failing and you want to isolate the issue. You can temporarily disable parts:

Debugging
SELECT
   customer_id,
   SUM(revenue)
FROM orders
-- WHERE status = 'Completed'
GROUP BY customer_id;

Now the filter is disabled. Fast debugging.

Commenting Out Multiple Lines

Cleaner Debugging
/*
SELECT customer_id,
    revenue,
    order_date
*/

Common Beginner Mistakes

1. Forgetting Comment Syntax

Bad: - comment
Correct: -- comment

2. Forgetting to Close Multi-Line Comments

If you forget the */, you break parsing for the rest of the script.

3. Overcommenting Obvious Code

Bad: -- select customer id followed by SELECT customer_id. Unnecessary.

4. Leaving Debug Comments in Production Queries

Can create confusion. Clean up final queries.

5. Using Comments Instead of Fixing Query Clarity

If aliases and formatting are terrible, comments won’t save the query. Readable SQL first. Comments second.

Writing Good SQL Comments

Bad comments are too obvious. -- select data is useless. Better: -- Exclude internal employee accounts.

Rule: Comment business intent, not obvious syntax.

Practice Questions

  1. Write a single-line comment.
  2. Write a multi-line comment.
  3. Temporarily disable a WHERE clause using comments.

What Comes Next?

Now you understand SQL readability. Next comes SQL clauses. Because clauses are the building blocks that define query structure. This is where SQL query composition becomes clearer.

Final Thoughts

SQL comments seem simple. But they dramatically improve maintainability.

For data analysts, comments help you:

  • explain business logic
  • debug faster
  • collaborate better
  • avoid confusion
  • make future edits easier

Strong analysts do not just write working SQL. They write understandable SQL.

Grit Over Excuses.

— The Grito Team