January 30th, 2009

JOIN versus WHERE (Part 2 of 3)

Posted by admin in L. JOINs

> Joins exist only for the duration of the query. They are not part of the database.

> Joined columns don’t need the same column name (except when doing a natural join).

> If a joining column contains a null, the null never joins.

> The joining columns must be compatible, such as (INTEGER, NUMERIC, FLOAT), (CHAR, VARCHAR), (DATE, TIME, TIMESTAMP).

January 29th, 2009

JOIN versus WHERE (Part 1 of 3)

Posted by admin in L. JOINs

There are two ways to do a join. You can use a JOIN or a WHERE syntax. WHERE joins were the only way to do a join until SQL-92 introduced the JOIN syntax. As a result, you will find both forms in use.

                  JOIN syntax format:                                         Example:

          SELECT select_columns                                   SELECT V.VNDR_ID,
            FROM table1 join_type table2                                     V.VENDOR_NAME,
                ON join_condition                                                   I.DESCRIPTION
        [WHERE search_condition]                                   FROM VENDOR_TBL V
         [GROUP BY grouping_columns]                           INNER JOIN INVENTORY_TBL I
        [HAVING search_condition]                                        ON V.VNDR_ID = I.VNDR_ID;
        [ORDER BY sort_columns];

 Select_columns is one or more selected columns, separated by a comma, from table1 and table2.

Join_type is the type of join to be performed, such as INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, NATURAL JOIN, and CROSS JOIN.

ON is not allowed for CROSS JOIN or NATURAL JOIN.

Join_condition shows the conditions for one or more joined rows.
The format is [table1.]column oper [table2.]column where oper is =, <, <=, >, >=, <>, AND, OR, or NOT.

The WHERE, GROUP BY, HAVING, and ORDER BY have been discussed in previous topics.

> WHERE syntax joins can only be done with AND. WHERE syntax joins using OR are not legal.

January 28th, 2009

AS (Table Aliases)

Posted by admin in L. JOINs

You can create table aliases just as you did column aliases. The difference is that with column aliases, the key word AS is required and it changes the resulting column name. With the table alias, the key word AS is optional and the table alias is used only to associate columns with tables — the resulting table is unchanged.In a FROM or JOIN clause, format: table_name [AS] alias_name

Alias is a single word, without quotes, and contains only letters, digits, or underscores. You can not use key words, spaces, punctuation, or special characters.

> Each table’s alias name must be unique within the SQL statement.> Besides being used to associate specific column names with tables, table aliases are used to reduce statement clutter which makes the statement more easy to understood.

> A table alias does not change the table name in the table definition and it exists only for the duration of the statement.

> An alias name negates the table name for the duration of the statement. Therefore, all references to the table must use the alias name in all qualifying references.

> Alias names can also be used for table views.

Next Page »