January 21st, 2009

Column Functions with Calculations

Posted by admin in J. Column Functions

Column function expressions can be used in the SELECT clause to perform calculations. We have a table: EMPLOYEE_TBL

If we wanted to know the difference between the minimum and maximum RATE_HOUR, we could do the following:

                    SELECT (MAX(RATE_HOUR) – MIN(RATE_HOUR))
                      FROM EMPLOYEE_TBL;

Result Table:

                   

Calculations are evaluated in the following order:
1. Expressions within parentheses with the innermost first.
2. Multiplication and division.
3. Addition and subtraction.
4. Left to right.

January 20th, 2009

Column Function Combinations

Posted by admin in J. Column Functions

Column functions can be SELECTed with other column functions because they each return only one value.
Column functions can not be combined with SELECTed columns or scalar functions because, they return multiple rows.

If we have a table: INVENTORY_TBL

          SELECT MIN(PEOPLE)      AS MIN,
                       MAX(PEOPLE)     AS MAX,
                       AVG(PEOPLE)      AS AVG,
                       COUNT(PEOPLE) AS NOT_NULL,
                       COUNT(*)           AS ALL
             FROM INVENTORY_TBL;

Result Table:

 

January 19th, 2009

COUNT (Rows)

Posted by admin in J. Column Functions

COUNT(expression) counts the number of rows selected that are not null.
COUNT(*) counts all of the rows, including those that are null.
The expression is a column name, numeric expression, or literal.
Nulls are included in the count with COUNT(*), otherwise Nulls are removed from the count.
The argument values can be character, numeric, datetime, or asterisk (*).
The result will be a numeric integer of zero value or greater.
The keyword, DISTINCT, can be used, except when using COUNT(*).

 

          COUNT(expression)                     <- Count the number of non-nulls.

                              or

          COUNT(DISTINCT column-name) <- Count the number of unique non-nulls.

                              or

          COUNT(*)                                      <- Count the number of null and non-nulls.

 

Notes:
COUNT(*) – COUNT(expression)                        <- Gives the number of nulls in a column.
((COUNT(*)-COUNT(expr))*100)/COUNT(*)        <- Gives the percentage of nulls.

Next Page »

Store

Start your SQL Training now by ordering the Computer Based Training SQL Course
for $24.95.

Purchase Software For
$24.95