SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | Comments

In SQL, comments are used to add descriptive notes within your SQL statements or to temporarily disable parts of your SQL code. Comments are ignored by the SQL processor and do not affect the execution of the statement.

There are two main types of comment syntax in SQL:

1. Single-Line Comments:

Single-line comments begin with two consecutive hyphens (--). Everything to the right of the hyphens, up to the end of the line, is a comment.

Example:

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

2. Multi-Line Comments:

Multi-line comments are enclosed between /* and */. Anything between these two sequences is considered a comment, regardless of the number of lines it spans.

Example:

/*
This is a multi-line comment
spanning multiple lines
*/
SELECT first_name, last_name FROM employees;

Using Comments:

  • Descriptive Notes:

Use comments to describe the purpose or logic behind certain SQL statements, especially if they're complex.

-- Get all employees hired in the last year
SELECT * FROM employees WHERE hire_date > DATE_SUB(CURRENT_DATE, INTERVAL 1 YEAR);
  • Temporarily Disabling Code:

If you're debugging or temporarily want to omit certain parts of your SQL script, you can comment them out.

SELECT first_name, last_name 
FROM employees
-- WHERE department_id = 10
ORDER BY last_name;

In the above example, the WHERE clause is commented out, so the query will fetch names of all employees, ordered by their last name.

  • Warnings or TODO Notes:

If you have areas of your SQL script that need further attention or revision in the future, you can use comments to mark these sections.

-- TODO: Optimize this query for better performance
SELECT * FROM large_table JOIN another_large_table USING (common_column);

Points to Note:

  • While comments are great for providing clarity, over-commenting or adding unnecessary comments can make the code cluttered. Aim for a balance: comment what's necessary and let the code speak for itself when possible.

  • Always ensure that your comments are accurate and up-to-date with the code. Outdated comments can mislead other developers.

  • Comment conventions might vary slightly among different SQL databases, but the two main types (-- and /* */) are widely accepted across most RDBMS.

  1. Single-Line Comments in SQL:

    -- This is a single-line comment
    SELECT column1
    FROM your_table;
    
  2. Multi-Line Comments in SQL:

    /*
       This is a multi-line comment
       spanning multiple lines.
    */
    SELECT column1
    FROM your_table;
    
  3. Commenting SQL Queries and Statements:

    -- Selecting data from a table
    SELECT column1, column2
    FROM your_table;
    
    /*
       Updating records based on a condition.
       Use caution when updating data.
    */
    UPDATE your_table
    SET column1 = 'New Value'
    WHERE column2 = 'Condition';
    
  4. Commenting Tables, Columns, and Constraints in SQL:

    CREATE TABLE your_table (
       column1 INT, -- Primary key for identification
       column2 VARCHAR(50) NOT NULL, -- Important data
       CONSTRAINT pk_your_table PRIMARY KEY (column1) -- Primary key constraint
    );
    
  5. Commenting Stored Procedures and Functions:

    /*
       This stored procedure performs a specific task.
       Input: parameter1 - Description
       Output: Result set with specific columns
    */
    CREATE PROCEDURE your_procedure
       @parameter1 INT
    AS
    BEGIN
       -- Procedure logic here
    END;
    
  6. Comments in SQL Views and Triggers:

    /*
       This view combines data from multiple tables.
       Provides a unified view for reporting purposes.
    */
    CREATE VIEW your_view AS
    SELECT column1, column2
    FROM table1
    JOIN table2 ON table1.id = table2.id;
    
    -- Trigger to log changes
    CREATE TRIGGER your_trigger
    AFTER UPDATE
    ON your_table
    FOR EACH ROW
    BEGIN
       -- Trigger logic here
    END;
    
  7. Commenting SQL Scripts for Collaboration:

    /*
       Script Name: Import_Data.sql
       Purpose: Import data from external source
       Author: John Doe
       Date: 2023-01-01
    */
    
    -- SQL statements for data import