Adding comments in SQL

Comments in SQL are essential for making your code more understandable and maintainable. They provide explanations for parts of the code, clarify complex queries, and can also be used to temporarily disable parts of the code for testing purposes. In this chapter, we will explore how to add comments in SQL, the different types of comments, and some best practices for using them effectively.

Why Use Comments in SQL?

When writing SQL queries, you often create scripts that others may need to review or modify. Adding comments helps:

  • Improve Readability: Describing the purpose and logic behind complex queries.
  • Facilitate Debugging: Temporarily disabling parts of queries to identify issues.
  • Provide Documentation: Indicating the purpose of scripts or sections of code.
  • Enhance Collaboration: Making it easier for other developers or team members to understand the query.

Comments are ignored by the SQL engine during execution, so they do not affect performance or the result of the query.

Types of Comments in SQL

SQL supports two types of comments:

  1. Single-line comments: Used for brief explanations.
  2. Multi-line comments: Used for longer descriptions that span multiple lines.

Single-line Comments

Single-line comments in SQL are indicated by two hyphens (--). Anything written after these two dashes on the same line is ignored by the SQL engine. They are often used for quick notes or brief explanations.

Syntax:

				
					-- This is a single-line comment
SELECT * FROM employees; -- Fetches all employees

				
			

Example:

				
					-- Fetch all employees who are in the "HR" department
SELECT * FROM employees
WHERE department = 'HR';

				
			

Explanation:

  • The first comment explains the purpose of the query.
  • The query selects all rows from the employees table where the department is ‘HR’.

Multi-line Comments

For more detailed explanations, you can use multi-line comments, which are enclosed between /* and */. These comments can span several lines.

Syntax:

				
					/*
 This is a multi-line comment
 It can span multiple lines
*/
SELECT * FROM employees;

				
			

Example:

				
					/*
 The following query selects all employees who
 are in the HR department and have a salary above $50,000.
*/
SELECT * FROM employees
WHERE department = 'HR'
AND salary > 50000;

				
			

Explanation:

  • The multi-line comment explains the purpose of the query in more detail.
  • The query selects employees from the employees table who work in the HR department and have a salary greater than $50,000.

Commenting Out Parts of Queries

Comments can also be used to temporarily disable parts of a query during development or debugging.

Example:

				
					-- SELECT name, salary FROM employees WHERE department = 'HR';
SELECT name, department FROM employees;

				
			

Explanation:

  • The first query, which selects the name and salary of employees in the HR department, is commented out. The second query, which selects the name and department of employees, is executed instead.

Commenting SQL Code for Debugging

In development, it’s common to comment out sections of SQL code to test specific parts of a query or script. For example, if you’re not sure why a WHERE clause isn’t working as expected, you can comment out sections to isolate the problem.

Example:

				
					/*
SELECT name, department, salary 
FROM employees 
WHERE salary > 50000;
*/
SELECT name, department FROM employees;

				
			

Explanation:

  • The first query is commented out for debugging purposes, allowing the developer to focus on a simplified query that fetches employee names and departments.

Best Practices for Using Comments in SQL

Be Concise

Comments should be clear and to the point. They should explain the purpose of the query without being overly verbose.

Example:

				
					-- Fetch all employees in the HR department
SELECT * FROM employees WHERE department = 'HR';

				
			

Use Comments for Complex Logic

When writing complex SQL queries, make sure to comment on the logic behind each part of the query. This helps others (and yourself in the future) understand the thought process behind the query.

Example:

				
					/*
 This query joins the employees and departments tables
 to find employees who work in the "HR" department
 and earn more than $50,000 per year.
*/
SELECT employees.name, employees.salary, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_name = 'HR'
AND employees.salary > 50000;

				
			

Avoid Obvious Comments

Don’t state the obvious in your comments. Only add comments where the logic isn’t immediately clear from the code itself.

Bad Example:

				
					-- Select all employees
SELECT * FROM employees;

				
			

Good Example:

				
					-- Select all employees who were hired after January 1, 2020
SELECT * FROM employees WHERE hire_date > '2020-01-01';

				
			

Use Consistent Commenting Style

Stick to a consistent style of commenting across your SQL scripts. This helps make your code more readable and professional.

Comments are an essential part of writing clear, maintainable SQL code. They allow developers to explain the purpose and logic behind their queries, which is crucial for collaboration, debugging, and future code updates. Whether you're adding a brief single-line comment or a detailed multi-line explanation, using comments effectively will make your SQL code easier to understand and maintain. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India