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.
When writing SQL queries, you often create scripts that others may need to review or modify. Adding comments helps:
Comments are ignored by the SQL engine during execution, so they do not affect performance or the result of the query.
SQL supports two types of 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.
-- This is a single-line comment
SELECT * FROM employees; -- Fetches all employees
-- Fetch all employees who are in the "HR" department
SELECT * FROM employees
WHERE department = 'HR';
employees
table where the department
is ‘HR’.For more detailed explanations, you can use multi-line comments, which are enclosed between /*
and */
. These comments can span several lines.
/*
This is a multi-line comment
It can span multiple lines
*/
SELECT * FROM employees;
/*
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;
employees
table who work in the HR department and have a salary greater than $50,000.Comments can also be used to temporarily disable parts of a query during development or debugging.
-- SELECT name, salary FROM employees WHERE department = 'HR';
SELECT name, department FROM employees;
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.
/*
SELECT name, department, salary
FROM employees
WHERE salary > 50000;
*/
SELECT name, department FROM employees;
Comments should be clear and to the point. They should explain the purpose of the query without being overly verbose.
-- Fetch all employees in the HR department
SELECT * FROM employees WHERE department = 'HR';
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.
/*
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;
Don’t state the obvious in your comments. Only add comments where the logic isn’t immediately clear from the code itself.
-- Select all employees
SELECT * FROM employees;
-- Select all employees who were hired after January 1, 2020
SELECT * FROM employees WHERE hire_date > '2020-01-01';
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 !❤️