February 13th, 2009

INNER JOIN with NATURAL JOIN

Posted by admin in L. JOINs

There are two ways to do a NATURAL JOIN. You can use a NATURAL JOIN or a WHERE syntax.

          NATURAL JOIN syntax format:                   Implied natural join example:

          SELECT columns                                        SELECT V.*,
             FROM table1                                                        I.DESCRIPTION
       NATURAL JOIN table2[,                                    FROM VENDOR_TBL V,
       NATURAL JOIN table3];                                               INVENTORY_TBL I
                                                                              WHERE V.VNDR_ID = I.VNDR_ID;

When NATURAL JOIN is explicitly stated, all columns in one table that have the same names, same data types, and same lengths as corresponding columns in the second table are compared for equality. This creates a NATURAL INNER JOIN.

> Be careful when doing explicit NATURAL JOINS. The data names, data types, and lengths must be exactly alike. This statement will unexpectedly return different results if someone else decides to add, change, rename, or delete the columns involved in the join.

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

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

> Oracle supports the JOIN syntax in version 9 and beyond.

> PostgreSQL supports all JOINs.

February 12th, 2009

INNER JOIN with Column Functions and HAVING

Posted by admin in L. JOINs

You can combine a column function with an INNER JOIN using a GROUP BY clause. The HAVING clause limits the number of groups retrieved for the GROUP BY clause.

                       JOIN syntax:                                                  WHERE syntax:

          SELECT t1.column1 AS ‘title1′,                       SELECT t1.column1 AS ‘title1′,
                       COUNT(t2.column2)                                      COUNT(t2.column2)
             FROM table1          [AS] ‘t1′                            FROM table1 [AS] ‘t1′,
            INNER JOIN table2   [AS] ‘t2′                                     table2 [AS] ‘t2′,
                 ON t1.column1 oper t2.column2,            WHERE t1.column1 oper t2.column2,
          GROUP BY t1.column1                                    GROUP BY t1.column1
          HAVING COUNT(t2.column2) condition           HAVING COUNT(t2.column2) condition
          ORDER BY t2.column2;                                  ORDER BY t2.column2;

Where oper is a comparison operation and WHERE is used for additional filtering.

February 11th, 2009

INNER JOIN with Column Functions and Calculations

Posted by admin in L. JOINs

You can combine a column function with an INNER JOIN, using a GROUP BY clause. These column functions can include mathematical formulas.

                      JOIN syntax:                                                WHERE syntax:

          SELECT t1.column1   AS ‘title1′,                  SELECT t1.column1 AS ‘title1′,
                       COUNT(t2.column2)                                    COUNT(t2.column2)
             FROM table1          [AS] ‘t1′                          FROM table1       [AS] ‘t1′,
             INNER JOIN table2 [AS] ‘t2′                                    table2       [AS] ‘t2′,
                  ON t1.column1 oper t2.column2,         WHERE t1.column1 oper t2.column2,
            GROUP BY t1.column1                                GROUP BY t1.column1
            ORDER BY t2.column2;                              ORDER BY t2.column2;

Where oper is a comparison operation and WHERE is used for additional filtering.

« Previous PageNext Page »