January 22nd, 2009

GROUP BY

Posted by admin in K. Grouping and Filtering

The GROUP BY clause divides a table into logical groups.
The order of multiple columns in the GROUP BY clause specifies the grouping level from the highest to the lowest level of grouping.
Only one row results for each distinct value in the grouping values.
Each row in the result contains summary data of the values in the grouping columns.

                                  SELECT    [ALL | DISTINCT]
                                                  [* | column1[, column2, ...]]
                                    FROM     [table1 correlation, ...
                                                | view1 correlation, ...]
                                   WHERE     condition1[, condition2, ...]
                                  GROUP BY column1[, column2, ...]
                                 HAVING     condition1[, condition2, ...]
                                  ORDER BY column1 [ASC | DESC][, column2 ...];

> The GROUP BY clause is not necessary unless used with a column function.

> The GROUP BY clause comes after the WHERE clause and before the HAVING and ORDER BY clause.

> The WHERE clause can eliminate data from consideration by the GROUP BY clause.

> A column name in the GROUP BY clause must match a column name in the SELECT statement. Column names in the GROUP BY clause do not have to be in the same order as the SELECT statement. If the SELECTed name is qualified, the GROUP BY name must be qualified.

> An expression in the GROUP BY clause must match the exact same expression in the SELECT statement.

> If a grouping column contains a NULL or NULLs, the NULLs become a group.

> To sort the GROUP BY groups, use the ORDER BY clause.

> Without the ORDER BY clause, the groups returned by the GROUP BY clause can be in any order. In some RDBMS Systems, GROUP BY implies ORDER BY.

> Multiple grouping columns in the GROUP BY clause creates nested groups.

> Table AS aliases are allowed as qualifiers, but AS aliases can’t be used in the GROUP BY clause.
See Table Aliases (AS).

> If you use a column for GROUP BY frequently, that column should have an index.

> If used without a column function, GROUP BY is the same as DISTINCT.

> RDBMS systems such a MySQL and PostgreSQL allow column aliases in the GROUP BY clause.

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:

 

« Previous PageNext Page »