January 27th, 2009

Qualifying Column Names

Posted by admin in L. JOINs

As mentioned earlier, column names must be unique within each table. However, the same column name can be used in other tables. To uniquely identify names that are the same in multiple tables, we use a qualifying name.A qualified name is a table name followed by a period and the name of the column in the table.

                       Format:                         table_name.column_name

Tables must have unique names within a database. Therefore, a qualified name is unique within the database.

These two statements are functionally identical:

                    SELECT VNDR_ID,                          SELECT VENDOR_TBL.VNDR_ID
                                 VENDOR_NAME                              VENDOR_TBL.VENDOR_NAME
                       FROM VENDOR_TBL;                      FROM VENDOR_TBL;

> A column name does not need to be qualified if it is a unique name among all the column names of all the tables
used in a statement.> Qualified and unqualified names may be intermixed in a statement.

> It is a good idea to use qualified names. Then, if a matching name is added to another table in the future,
there will not be a problem with an unqualified name.

> Some RDBMS systems might require a hierarchy of qualification such as server, database, schema, owner, table.

> SQL Server requires server.database.owner.table.

January 26th, 2009

Introduction to JOINs

Posted by admin in L. JOINs

All of the queries up to this point have retrieved rows from a single table, but using a relational database means retrieving relational data from multiple tables.A JOIN is a table operation that uses related columns to combine rows from two or more input tables into one result table.

This is where the relational database (RDBMS) has its true power. There is so much more information that can be obtained from tables, through joins of related data, using the SELECT statement.

Most join statements can be rewritten as subselects and most subselect statements can be rewritten as joins.> Table columns used to join two or more tables do not have to be SELECTed, they just have to be in the JOIN or WHERE clause. However, these columns may appear in the SELECT statement if you wish to see them in the results table.

> Access, DB2, and SQL Server do not support NATURAL JOIN syntax. See NATURAL JOIN.

> MySQL only supports a NATURAL JOIN on an OUTER JOIN, not an INNER JOIN. It also uses the non-standard STRAIGHT_JOIN. See INNER JOIN and OUTER JOIN.

> Oracle 8i and earlier does not support the JOIN syntax. Only Oracle 9i and beyond support the JOIN syntax.
If JOIN is not supported, use the WHERE clause.

> PostgreSQL supports all JOINs.

January 23rd, 2009

HAVING

Posted by admin in K. Grouping and Filtering

Just as the WHERE clause limits the number of rows retrieved by the SELECT statement, the HAVING clause limits the number of groups retrieved for the GROUP BY clause.

                         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 [ASC | DESC], …];

> Unlike the WHERE clause, the HAVING clause can contain a column function.

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

> The WHERE clause can eliminate data from consideration by the GROUP BY clause. The WHERE clause is applied before grouping happens. The HAVING clause is applied after grouping happens.

> An entry in the HAVING clause must match an entry in the SELECT statement or be a column function.
> Column names in the HAVING clause do not have to be in the same order as the SELECT statement.

> If the SELECTed name is qualified, the HAVING name must be qualified.

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

> Multiple HAVING conditions can be specified using AND, OR, and NOT.

« Previous PageNext Page »