How to Add Comments in Simple SQL Selects

Adding comments to your SQL Select statements is a great way to document your SQL command. You can refer to the comments in the future to help you understand why you created the statement in the first place.

To learn how to add comments in simple SQL select statements, follow these steps:

  1. Download MySQL Community Edition 5.7 for Windows, by visiting "
  2. After the download, install MySQL Community Edition on your Windows computer. Be sure to take note of the "root" account password that you create. You will need that password to authenticate to MySQL in order to work through the How to demonstration.
  3. Now you will create a database and populate it with two simple tables. In your text editor, create a new file and type in the following statements carefully to avoid syntax errors later:
    USE HowTo
    CREATE TABLE States ( 
      state_abbrev CHAR(2),
      name VARCHAR(100), 
      CONSTRAINT pk_states PRIMARY KEY(state_abbrev)
    INSERT INTO States VALUES ('CA', 'California');
    INSERT INTO States VALUES ('IL', 'Illinois');
    INSERT INTO States VALUES ('NV', 'Nevada');
    INSERT INTO States VALUES ('FL', 'Florida');
    INSERT INTO States VALUES ('GA', 'Georgia');
    INSERT INTO States VALUES ('NE', 'Nebraska');
    INSERT INTO States VALUES ('NY', 'New York');
    INSERT INTO States VALUES ('ME', 'Maine');
    INSERT INTO States VALUES ('TX', 'Texas');
    CREATE TABLE Cities ( 
      name VARCHAR(100),
      state_abbrev CHAR(2),
      population NUMERIC(8,0), -- According to 2010 U.S. Census 
      CONSTRAINT fk_cities_states FOREIGN KEY (state_abbrev) REFERENCES STATES (state_abbrev)
    INSERT INTO Cities VALUES ('Los Angeles', 'CA', 3792621);
    INSERT INTO Cities VALUES ('San Francisco', 'CA', 805235);
    INSERT INTO Cities VALUES ('San Diego', 'CA', 1307402);
    INSERT INTO Cities VALUES ('Las Vegas', 'NV', 583756);
    INSERT INTO Cities VALUES ('Chicago', 'IL', 2695598);
    INSERT INTO Cities VALUES ('Miami', 'FL', 399457);
    INSERT INTO Cities VALUES ('Orlando', 'FL', 238300);
    INSERT INTO Cities VALUES ('Atlanta', 'GA', 420003);
    INSERT INTO Cities VALUES ('New York City', 'NY', 8175133);
    INSERT INTO Cities VALUES ('Omaha', 'NE', 408958);
  4. Save the file as StatesAndCities.sql. I would suggest you store the file in directory immediately under the C: drive so that you don't have to type in a long directory name when you run the SQL statements through the mysql command tool.
  5. Start the MySQL Server in your services panel.
  6. Open a command prompt and navigate (using cd) to c:/Program Files/MySQL/MySQL Server 5.7/bin. Then type in the following command:
    MySQL command line program startup
    Hit Enter. You will be prompted for your root password. Enter your password and hit Enter again.
  7. You are now in the interactive mode of the mysql command line program. Our first task is to run the script you created. To do so, type in the source command as illustrated:
    Run setup script in mysql
  8. Hit Enter to run the script. You will see the output as shown above (Note: Only a portion of the output is shown on the screen shot). If you experience any errors, check the syntax of your statements with the statements I presented earlier.
  9. Your database is ready for action! Now we'll focus on the topic at hand, putting comments in a simple select statement.
  10. You may have noticed the comment I placed in the CREATE TABLE definition of the Cities table. The comment was preceeded by two hyphens. Let's try the comment syntax using a simple select statement:
    First Comment example
    If you hit your "Up Arrow" key you can retrieve the SQL statement. The comment can help you remember important details about the SQL select statement.
  11. You can also place a comment within the select statement. We will use /* and */ as shown in the following example:
    Second Comment example
    This technique permits you to annotate individual parts of your SQL select statement.
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.