How to Calculate Fields in SQL

  • google plus

In Brief...

A select statement in SQL usually displays column data on a report. At times, however, you might want to display a calculated field on the output. A calculated field is a field whose value is derived from column data.

Take our Introduction to SQL Training course for free.

See the Course Outline and Register

Instructions

To learn how calculate fields in a SQL select statement, follow these steps:

  1. You'll need to setup the MySQL database tables. The instructions for the setup can be found in How to add comments in simple SQL selects. Follow steps 1 through 7 before proceeding to the next step.
  2. We turn our attention to creating a calculated field.
  3. Let us presuppose that Census Bureau statistics estimate an 8% population growth in cities in Florida. We wish to display the city name, current population, estimated population growth and the projected total population. Execute the following statement:
    Cities with calculated field
    The select statement selects the current population, a calculated field in which the population is multiplied by .08 (8 percent) and then a calculated field that contains the projected total population. The calculations are performed using familiar arithmetic operators such as the + sign (add) and the * sign (multiplication). I use the as keyword to give the calculated fields meaningful column headings.

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, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.

Categories

Courses

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss