February 10th, 2009

INNER JOIN with Column Functions

Posted by admin in L. JOINs

You can combine a column function with an INNER JOIN, using a 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
           ORDER BY t2.column2;                               ORDER BY t2.column2;

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

See Column Functions.

February 9th, 2009

INNER JOIN with Three Tables

Posted by admin in L. JOINs

You can join more than two tables.

                      JOIN syntax:                                                   WHERE syntax:

         SELECT t1.column1   AS ‘title1′,                      SELECT t1.column1 AS ‘title1′,
                      t2.column2   AS ‘title2′,                                   t2.column2 AS ‘title2′,
                      t3.column3   AS ‘title3′                                    t3.column3 AS ‘title3′
            FROM table1          [AS] ‘t1′                              FROM table1       [AS] ‘t1′,
            INNER JOIN table2 [AS] ‘t2′                                        table2       [AS] ‘t2′,
                 ON t1.column1 oper t2.column2,                          table3       [AS] ‘t3′
            INNER JOIN table3 [AS] ‘t3′                          WHERE t1.column1 oper t2.column2,
                 ON t2.column2 oper t3.column3,           AND|OR t2.column2 oper t3.column3,
          WHERE condition1                                        AND|OR condition1
         AND|OR condition2;                                       AND|OR condition2;

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

> In a three-table join, only one table will be used to bridge to the other two tables. However, it really does not matter which table is picked to serve this function.

February 6th, 2009

INNER JOIN with Non-Equal Conditions

Posted by admin in L. JOINs

You can use WHERE clause conditions other than = using <, <=, >, >=, <>, NOT column =, NOT NULL, LIKE, BETWEEN, IN, etc.

                       JOIN syntax:                                                   WHERE syntax:

          SELECT t1.column1 AS ‘title1′,                       SELECT t1.column1 AS ‘title1′,
                       t2.column2 AS ‘title2′, …                                 t2.column2 AS ‘title2′, …
             FROM table1        [AS] ‘t1′                              FROM table1         [AS] ‘t1′,
     INNER JOIN table2       [AS] ‘t2′                                        table2         [AS] ‘t2′
                  ON t1.column1 oper t2.column1[,          WHERE t1.column1 oper t2.column1[,
          AND|OR t1.column2 oper t2.column2]          AND|OR t1.column2 oper t2.column2];
           WHERE condition1
         [AND|OR condition2];

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

« Previous PageNext Page »