All Products
Search
Document Center

Comments in SQL statements

Last Updated: Jun 18, 2021

Comments make applications easier to read and maintain. For example, you can create a comment in a statement to describe the purpose of the statement in an application. Only hints in SQL statements affect how the statements are executed. The other comments in SQL statements do not affect how the statements are affected.

You can add a comment between keywords, parameters, or punctuations in a statement. You can use two methods to add a comment:

  • Add a comment that starts with a slash and an asterisk (/*). The text of the comment follows the slash and the asterisk (/*). The text can be distributed across multiple lines. The comment must end with an asterisk and a slash (*/). You do not need to use white-space characters or line feeds to separate the start and end symbols from the text.

  • Add a comment that starts with two hyphens (--). The text of the comment follows the hyphens (--). The text can be distributed in only one line. The comment must end with a line feed.

An SQL statement can contain multiple comments of the preceding two styles. The comment text can contain all of the printable characters in your database character set.

The following example is used to demonstrate multiple forms of comments, each of which starts with a slash and an asterisk (/*):

SELECT last_name, employee_id, salary + NVL(commission_pct, 0), 
            job_id, e.department_id
  /* Select all employees whose compensation is
  greater than that of Pataballa. */
  FROM employees e, departments d
  /*The DEPARTMENTS table is used to get the department name. */
  WHERE e.department_id = d.department_id
    AND salary + NVL(commission_pct,0) >   /* Subquery:       */
       (SELECT salary + NVL(commission_pct,0)
         /* total compensation is salary + commission_pct */
         FROM employees 
         WHERE last_name = 'Pataballa')
  ORDER BY last_name, employee_id;

The statement in the following example contains multiple forms of comments, each of which starts with two hyphens (--):

SELECT last_name,                                   -- select the name
       employee_id                                  -- employee id
       salary + NVL(commission_pct, 0),             -- total compensation
       job_id,                                      -- job
       e.department_id                              -- and department
  FROM employees e,                                 -- of all employees
       departments d
  WHERE e.department_id = d.department_id
    AND salary + NVL(commission_pct, 0) >           -- whose compensation 
                                                    -- is greater than
        (SELECT salary + NVL(commission_pct,0)      -- the compensation
          FROM employees 
          WHERE last_name = 'Pataballa')            -- of Pataballa
  ORDER BY last_name                                -- and order by last name
           employee_id                              -- and employee id.
;