SQL Comments
SQL Comments are used to explain sections of a SQL query or to prevent the execution of a part of a statement.
- Single line comment
- Multi-line comment
Single Line Comment
Single line comment can be defined by using a double hyphen --
which marks the start of a comment till the end of that line.
- The first example ignores the text on the first line, as it is commented.
- The second example ignores the WHERE clause till the end of that line, as it is commented.
- The third example will ignore the query on the first line, as it is commented.
--Select all customers
SELECT * FROM customers;
SELECT * FROM customers -- WHERE city='London';
--SELECT * FROM customers;
SELECT * FROM products;
Multi-line Comment
Multi-line comments start with /*
and end with */
which can be used to define a comment that extends to multiple lines.
- The first example ignores the text on the first three lines, as they are commented.
- The second example ignores the statements on the first three lines, as they are commented.
- The third example ignores a part of a statement.
- The fourth examples ignore a part of the statement, that extends to multiple lines.
/*Select all the columns of all the records
in the Customers table
without WHERE clause:*/
SELECT * FROM customers;
/*SELECT * FROM employees;
SELECT * FROM products;
SELECT * FROM orders;
SELECT * FROM suppliers;*/
SELECT * FROM customers;
SELECT customer_name, /*address, city,*/ country FROM customers;
SELECT * FROM customers WHERE (customer_name LIKE 'L%'
OR customer_name LIKE 'R%' /*OR customer_name LIKE 'S%'
OR customer_name LIKE 'T%'*/ OR customer_name LIKE 'W%')
AND country='USA'
ORDER BY customer_name;
Overall
We now know how to use comments while writing SQL queries.