Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   SQL Statements > SELECT >

Specifying a Join in the WHERE Clause

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

Read syntax diagramSkip visual syntax diagramJoin:
 
                                                        (1)
|--+-----------------+--column--| Relational Operator |--------->
   '-| Data Source |-'
 
>--+-----------------+--column----------------------------------|
   '-| Data Source |-'
 
Data Source:
 
|--+-alias .-----------+----------------------------------------|
   +-table .-----------+
   +-view .------------+
   +-synonym .---------+
   |  (2)              |
   '--------external .-'
 

Notes:
  1. See Relational Operator
  2. Extended Parallel Server only

Element Description Restrictions Syntax
alias Temporary alternative name declared in the FROM clause for a table or view See Self-Joins; FROM Clause Identifier
column Column of a table or view to be joined Must exist in the table or view Identifier
external External table from which to retrieve data External table must exist Database Object Name
synonym, table, view Name of a synonym, table, or view to be joined in the query Synonym and the table or view to which it points must exist Database Object Name

Rows from the tables or views are joined when there is a match between the values of specified columns. When the columns to be joined have the same name, you must qualify each column name with its data source.

Two-Table Joins

You can create two-table joins, multiple-table joins, self-joins, and outer joins (Informix-extension syntax). The following example shows a two-table join:

SELECT order_num, lname, fname FROM customer, orders
   WHERE customer.customer_num = orders.customer_num

Multiple-Table Joins

A multiple-table join is a join of more than two tables. Its structure is similar to the structure of a two-table join, except that you have a join condition for more than one pair of tables in the WHERE clause. When columns from different tables have the same name, you must qualify the column name with its associated table or table alias, as in table.column. For the full syntax of a table name, see Database Object Name.

The following multiple-table join yields the company name of the customer who ordered an item as well as its stock number and manufacturer code:

SELECT DISTINCT company, stock_num, manu_code
   FROM customer c, orders o, items i
   WHERE c.customer_num = o.customer_num
      AND o.order_num = i.order_num

Self-Joins

You can join a table to itself. To do so, you must list the table name twice in the FROM clause and assign it two different table aliases. Use the aliases to refer to each of the two tables in the WHERE clause. The next example is a self-join on the stock table. It finds pairs of stock items whose unit prices differ by a factor greater than 2.5. The letters x and y are each aliases for the stock table.

SELECT x.stock_num, x.manu_code, y.stock_num, y.manu_code
   FROM stock x, stock y WHERE x.unit_price > 2.5 * y.unit_price

Extended Parallel Server does not support self-joins with an external table.

Informix-Extension Outer Joins

The next outer join lists the company name of the customer and all associated order numbers, if the customer has placed an order. If not, the company name is still listed, and a NULL value is returned for the order number.

SELECT company, order_num FROM customer c, OUTER orders o
   WHERE c.customer_num = o.customer_num

In Extended Parallel Server, you cannot use an external table as the outer table in an outer join.

For more information about outer joins, see the IBM Informix Guide to SQL: Tutorial.

GROUP BY Clause

Use the GROUP BY clause to produce a single row of results for each group. A group is a set of rows that have the same values for each column listed.

Read syntax diagramSkip visual syntax diagramGROUP BY Clause:
 
             .-,------------------------------------.
             V                                      |
|--GROUP BY----+-+----------------------+--column-+-+-----------|
               | |                 (1)  |         |
               | '-| Data Source |------'         |
               |  (2)                             |
               '--------select_number-------------'
 

Notes:
  1. See WHERE Clause
  2. Informix extension

Element Description Restrictions Syntax
column A column (or an expression) from the select list of the Projection clause See Relationship of GROUP BY and Projection Clauses Identifier
select _number Integer specifying ordinal position of a column or expression in select list See Using Select Numbers. Literal Number

The SELECT statement with a GROUP BY clause returns a single row of results for each group of rows that have the same value in column, or that have the same value in the column or expression that the select_number specifies.

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]