SQL Comments

SQL Comments : SQL Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

Note:

The examples in this chapter will not work in Firefox and Microsoft Edge!

Comments are not supported in Microsoft Access databases. Firefox and Microsoft Edge are using Microsoft Access database in our examples.

Single Line Comments

  • Single line comments start with –.
  • Any text between — and the end of the line will be ignored (will not be executed).
  • The following example uses a single-line comment as an explanation:

Example

–Select all:
SELECT * FROM Customers;

 

Output

Number of Records: 2

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico

 

The following example uses a single-line comment to ignore the end of a line:

Example

SELECT * FROM Customers — WHERE City=’Berlin’;

 

Output

Number of Records: 2

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021

 

The following example uses a single-line comment to ignore a statement:

 

Multi-line Comments

  • Multi-line comments start with /* and end with */.
  • Any text between /* and */ will be ignored.
  • The following example uses a multi-line comment as an explanation:

Example

/*Select all the columns
of all the records
in the Customers table:*/
SELECT * FROM Customers;

 

Output

Number of Records: 4

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

 

The following example uses a multi-line comment to ignore many statements:

Example

/*SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
SELECT * FROM Categories;*/
SELECT * FROM Suppliers;

 

Output

Number of Records: 6

SupplierID SupplierName ContactName Address City PostalCode Country Phone
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK (171) 555-2222
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA (100) 555-4822
3 Grandma Kelly’s Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA (313) 555-5735
4 Tokyo Traders Yoshi Nagase 9-8 Sekimai Musashino-shi Tokyo 100 Japan (03) 3555-5011
5 Cooperativa de Quesos ‘Las Cabras’ Antonio del Valle Saavedra Calle del Rosal 4 Oviedo 33007 Spain (98) 598 76 54
6 Mayumi’s Mayumi Ohno 92 Setsuko Chuo-ku Osaka 545

 

To ignore just a part of a statement, also use the /* */ comment.

The following example uses a comment to ignore part of a statement:

Example

SELECT * FROM Customers WHERE (CustomerName LIKE ‘L%’
OR CustomerName LIKE ‘R%’ /*OR CustomerName LIKE ‘S%’
OR CustomerName LIKE ‘T%’*/ OR CustomerName LIKE ‘W%’)
AND Country=’USA’
ORDER BY CustomerName;

 

Output

Number of Records: 5

CustomerID CustomerName ContactName Address City PostalCode Country
43 Lazy K Kountry Store John Steel 12 Orchestra Terrace Walla Walla 99362 USA
45 Let’s Stop N Shop Jaime Yorres 87 Polk St. Suite 5 San Francisco 94117 USA
48 Lonesome Pine Restaurant Fran Wilson 89 Chiaroscuro Rd. Portland 97219 USA
65 Rattlesnake Canyon Grocery Paula Wilson 2817 Milton Dr. Albuquerque 87110 USA
89 White Clover Markets Karl Jablonski 305 – 14th Ave. S. Suite 3B Seattle 98128 USA