eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Databases for Web Developers
Performing Math  
Sometimes, it is useful to actually massage the numerical data returned by performing basic arithmetic on the results. SQL gives you plenty of useful tools for doing just this. The most basic tools for arithmetic include the "+", "-", "*", and "/" operators as you might expect.

This basic type of arithmetic is usually performed in the SELECT clause and usually involves creating a new column based on the total achieved by doing the math. For example, consider the following example in which we subtract a 1.00 sale value to each of our products, add on the tax, and display the result in a virtual column "REAL_PRICE".

Note that we call this a virtual column because there is no "INFLATED_PRICE" column in the actual table. It only exists in this view. Note also that arithmetic can only be applied to numeric columns. Finally, note that arithmetic follows the usual precedence rules. For example, equations within parentheses are evaluated before they are applied to equations outside of parentheses.

    SELECT P_NUM,
           P_PRICE,
           REAL_PRICE = (PRICE - 1.00) +
                        (PRICE - 1.00) * .07
    FROM PRODUCTS;

The command will yield the following view

    P_NUM	P_PRICE		REAL_PRICE
    -------------------------------
    001		99.99		105.92
    002		865.99		925.54
    003		50.00		52.43
    -------------------------------

Another useful arithmetic tool is the SUM operator that is used to total a column. The basic format looks like:

    SELECT SUM (column_name)
    FROM table_name;
    WHERE where_clause [OPTIONAL];

For example, to get a SUM of all the products that cost less than 100.00 you could use:

    SELECT SUM (P_PRICE)
    FROM PRODUCTS
    WHERE P_PRICE < 100.00;

The command will yield the following view

    SUM (P_PRICE)
    -------
    149.99
    -------

Previous | Next | Table of Contents