4D v16

Support of joins

Home

 
4D v16
Support of joins

Support of joins  


 

 

The SQL engine of 4D extends the support of joins.

Join operations may be inner or outer, implicit or explicit. Implicit inner joins are support via the SELECT command. You can also generate explicit inner and outer joins using the SQL JOIN keyword.

Note: The current implementation of joins in the 4D SQL engine does not include:

  • natural joins.
  • the USING construct on inner joins.
  • cross inner joins.

Join operations are used to make connections between the records of two or more tables and combine the result in a new table, called a join. 

You generate joins via SELECT statements that specify the join conditions.

Starting with 4D v15 R4, outer joins involving two tables and outer joins involving three or more tables are different implementations and do not follow the same rules. Please refer below to the section that correspond to your needs.

Note: Usually, in the database engine, the table order is determined by the order specified during the search. However, when you use joins, the order of the tables is determined by the list of tables. In the following example:
SELECT * FROM T1 RIGHT OUTER JOIN T2 ON T2.depID = T1.depID;
... the order of the tables is T1 then T2 (as they appear in the list of tables) and not T2 then T1 (as they appear in the join condition).

To illustrate how joins work, we are going to use the following database throughout this section:

  • Employees
    namedepIDcityID
    Alan1030
    Anne1139
    Bernard1033
    Fabrice1235
    Martin1530
    PhilipNULL33
    Thomas10NULL
  • Departments
    depIDdepName
    10Program
    11Engineering
    NULLMarketing
    12Development
    13Quality
  • Cities
    cityIDcityName
    30Paris
    33New York
    NULLBerlin

If you want, you can generate this database automatically by executing the following code:

 Begin SQL
         DROP TABLE IF EXISTS Employees;
         CREATE TABLE Employees ( depID INT32, name VARCHAR, cityID INT32);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Alan', 10, 30);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Anne', 11, 39);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Bernard', 10, 33);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Fabrice', 12, 35);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Martin', 15, 30);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Philip', NULL, 33);
         INSERT INTO Employees (name, depID, cityID) VALUES ('Thomas', 10, NULL);
 
         DROP TABLE IF EXISTS Departments;
         CREATE TABLE Departments ( depID INT32, depName VARCHAR );
         INSERT INTO Departments (depID, depName) VALUES (10, 'Program');
         INSERT INTO Departments (depID, depName) VALUES (11, 'Engineering');
         INSERT INTO Departments (depID, depName) VALUES (NULL, 'Marketing');
         INSERT INTO Departments (depID, depName) VALUES (12, 'Development');
         INSERT INTO Departments (depID, depName) VALUES (13, 'Quality');
 
         DROP TABLE IF EXISTS Cities;
         CREATE TABLE Cities ( cityID INT32, cityName VARCHAR );
         INSERT INTO Cities (cityID, cityName) VALUES (30, 'Paris');
         INSERT INTO Cities (cityID, cityName) VALUES (33, 'New York');
         INSERT INTO Cities (cityID, cityName) VALUES (NULL, 'Berlin');
 End SQL

An inner join is based on a comparison to find matches between two columns.

Here is an example of an implicit inner join:

SELECT *  
        FROM employees, departments
        WHERE employees.DepID = departments.DepID;

In 4D, you can also use the JOIN keyword to specify an explicit inner join:

SELECT *
        FROM employees
        INNER JOIN departments
            ON employees.DepID = departments.DepID;

You can insert this query into 4D code as follows:

 ARRAY TEXT(aName;0)
 ARRAY TEXT(aDepName;0)
 ARRAY INTEGER(aEmpDepID;0)
 ARRAY INTEGER(aDepID;0)
 Begin SQL
        SELECT Employees.name, Employees.depID, Departments.depID, Departments.depName
        FROM Employees
                INNER JOIN Departments
                    ON Employees.depID = Departments.depID
                INTO :aName, :aEmpDepID, :aDepID, :aDepName;
 End SQL

Here are the results of this join:

aNameaEmpDepIDaDepIDaDepName
Alan1010Program
Anne1111Engineering
Bernard1010Program
Mark1212Development
Thomas1010Program

Note that neither the employees named Philip or Martin nor the Marketing or Quality departments appear in the resulting join because:

  • Philip does not have a department associated with his name (NULL value),
  • The department ID associated with Martin’s name does not exist in the Departments table,
  • There is no employee associated with the Quality department (ID 13),
  • The Marketing department does not have an ID associated with it (NULL value).

You can generate outer joins with 4D. With outer joins, it is not necessary for there to be a match between the rows of joined tables. The resulting table contains all the rows of the tables (or of at least one of the joined tables) even if there are no matching rows. This means that all the information of a table can be used, even if the rows are not completely filled in between the different joined tables. 

There are three types of outer joins, specified using the LEFT, RIGHT and FULL keywords. LEFT and RIGHT are used to indicate the table (located to the left or right of the JOIN keyword) where all the data must be processed. FULL indicates a bilateral outer join.

Note: Only explicit outer joins are supported by 4D.

With two-table outer joins, conditions can be complex but they must always be based on an equality comparison between the columns included in the join. For example, it is not possible to use the >= operator in an explicit join condition. Any type of comparison can be used in an implicit join. Internally, equality comparisons are carried out directly by the 4D engine, which ensures rapid execution

The result of a left outer join (or left join) always contains all the records for the table located to the left of keyword even if the join condition does not find a matching record in the table located to the right. This means that for each row in the left table where the search does not find any matching row in the right table, the join will still contain this row but it will have NULL values in each column of the right table. In other words, a left outer join returns all the rows of the left table plus any of those of the right table that match the join condition (or NULL if none match). Note that if the right table contains more than one row that matches the join predicate for a single row of the left table, the values of the left table will be repeated for each distinct row of the right table. 

Here is an example of 4D code with a left outer join:

 ARRAY TEXT(aName;0)
 ARRAY TEXT(aDepName;0)
 ARRAY INTEGER(aEmpDepID;0)
 ARRAY INTEGER(aDepID;0)
 Begin SQL
        SELECT Employees.name, Employees.depID, Departments.depID, Departments.depName
            FROM Employees
            LEFT OUTER JOIN Departments
                ON Employees.DepID = Departments.DepID
                INTO :aName, :aEmpDepID, :aDepID, :aDepName;
 End SQL

Here is the result of this join with our example database (additional rows shown in red):

aNameaEmpDepIDaDepIDaDepName
Alan1010Program
Anne1111Engineering
Bernard1010Program
Mark1212Development
Thomas1010Program
Martin15NULLNULL
PhilipNULLNULLNULL

A right outer join is the exact opposite of a left outer join. Its result always contains all the records of the table located to the right of the JOIN keyword even if the join condition does not find any matching record in the left table. 

Here is an example of 4D code with a right outer join:

 ARRAY TEXT(aName;0)
 ARRAY TEXT(aDepName;0)
 ARRAY INTEGER(aEmpDepID;0)
 ARRAY INTEGER(aDepID;0)
 Begin SQL
        SELECT Employees.name, Employees.depID, Departments.depID, Departments.depName
            FROM Employees
            RIGHT OUTER JOIN Departments
                ON Employees.DepID = Departments.DepID;
                INTO :aName, :aEmpDepID, :aDepID, :aDepName;
 End SQL

Here is the result of this join with our example database (additional rows shown in red):

aNameaEmpDepIDaDepIDaDepName
Alan1010Program
Anne1111Engineering
Bernard1010Program
Mark1212Development
Thomas1010Program
NULLNULLNULLMarketing
NULLNULL13Quality

A full outer join simply combines together the results of a left outer join and a right outer join. The resulting join table contains all the records of the left and right tables and fills in the missing fields on each side with NULL values. 

Here is an example of 4D code with a full outer join:

 ARRAY TEXT(aName;0)
 ARRAY TEXT(aDepName;0)
 ARRAY INTEGER(aEmpDepID;0)
 ARRAY INTEGER(aDepID;0)
 Begin SQL
        SELECT Employees.name, Employees.depID, Departments.depID, Departments.depName
            FROM Employees
            FULL OUTER JOIN Departments
                ON Employees.DepID = Departments.DepID
                INTO :aName, :aEmpDepID, :aDepID, :aDepName;
 End SQL

Here is the result of this join with our example database (additional rows shown in red):

aNameaEmpDepIDaDepIDaDepName
Alan1010Program
Anne1111Engineering
Bernard1010Program
Mark1212Development
Thomas1010Program
Martin15NULLNULL
PhilipNULLNULLNULL
NULLNULLNULLMarketing
NULLNULL13Quality

Starting with 4D v15 R4, the built-in SQL server extends the support of SQL outer joins to queries involving three or more tables. This specific implementation has its own rules and limitations, which are described in this section.

Like two-table outer joins, outer joins with three or more tables can be LEFT, RIGHT, or FULL. For general information on outer joins, please refer to the Outer joins with two tables paragraph above.

Unlike two-table outer joins, outer joins with three or more tables support several comparison operators, in addition to the equality (=): <, >, >=, or <=. These operators can be mixed within the ON clauses. 

  • Each explicit outer join ON clause must reference exactly two tables, no more and no less. Each joined table must be referenced at least once in the ON clauses.
  • One of the tables must come from the immediate left part of the JOIN clause and the other, from the immediate right.

For example, the following query will be executed with success:

SELECT * FROM T1
LEFT JOIN
(T2 LEFT JOIN T3 ON T2.ID=T3.ID) -- here T2 is on the left and T3 is on the right
ON T1.ID=T3.ID -- here T1 is on the left and T3 is on the right

With our three tables, this example could be:

 ARRAY TEXT(aName;0)
 ARRAY TEXT(aDepName;0)
 ARRAY TEXT(aCityName;0)
 ARRAY INTEGER(aEmpDepID;0)
 ARRAY INTEGER(aEmpCityID;0
 ARRAY INTEGER(aDepID;0)
 ARRAY INTEGER(aCityID;0)
 Begin SQL
        SELECT Employees.name, Employees.depID, Employees.cityID, Departments.depID, Departments.depName, Cities.cityID, Cities.cityName
            FROM Departments
                LEFT JOIN
                (Employees LEFT JOIN Cities ON Employees.cityID=Cities.cityID)
                ON Departments.depID=Employees.depID
                INTO :aName, :aEmpDepID, :aEmpCityID, :aDepID, :aDepName, :aCityID, :aCityName;
 End SQL

Here are the results:

aNameaEmpDepIDaEmpCityIDaDepIDaDepNameaCityIDaCityName
Alan103010ProgramNULLNULL
Bernard103310Program30Paris
Anne113911Engineering33New York
Fabrice123512DevelopmentNULLNULL
Thomas10NULL10ProgramNULLNULL
NULLNULLNULLNULLMarketingNULLNULL
NULLNULLNULL13QualityNULLNULL

On the other hand, the following three queries will be rejected since they violate certain rules:

SELECT * FROM T1
LEFT JOIN
(T2 LEFT JOIN T3 ON T2.ID=T1.ID) -- here T2 is on the left but T1 is not present in the immediate right
ON T1.ID=T3.ID

SELECT * FROM
(T1 LEFT JOIN T2 ON T1.ID=T2.ID)
LEFT JOIN
(T3 LEFT JOIN T4 ON T3.ID=T4.ID)
ON T3.Name=T4.Name -- here both T3 and T4 come from the right side of the JOIN clause and no tables at all come from the left side

SELECT * FROM T1
LEFT JOIN
(T2 LEFT JOIN T3 ON T2.ID=T3.ID)
ON T1.ID=T3.ID AND T1.ID=T2.ID -- here more than two tables are being used in the ON clause: T1, T2, and T3

In general, if tables (Tx1, Tx2..., Txn) on the left of JOIN clause and tables (Ty1, Ty2..., Tym) on the right are being joined, then the ON expression must reference exactly one left table Txa and exactly one right table Tyb.

Not supported in the ON clauseSupported in the ON clause
Boolean operationsORAND and NOT
Predicate and functionsIS NULL, COALESCEAll other predicates and built-in functions (can be used in any combination desired)
4D variable references-Supported without restriction
4D method callsWhen either left or right side of the current JOIN clause is an explicit outer joinAny other cases (see example below)

The following example with a 4D method call is supported because there are no non-inner sub-joins to join:

SELECT * FROM T1
LEFT JOIN T2
ON T1.ID={FN My4DCall (T2.ID) AS INT32}

On the other hand, this example of 4D method call is not supported because non-inner sub-joins are being joined:

SELECT * FROM
(T1 LEFT JOIN T2 ON T1.ID=T2.ID)
LEFT JOIN -- Both left and right sides of this join clause contain explicit LEFT joins
(T3 LEFT JOIN T4 ON T3.ID=T4.ID)
ON T1.ID={FN My4DCall (T4.ID) AS INT32} -- non-inner sub-joins are being joined

  • References to SQL views are not allowed in the explicit join declaration
  • Subqueries that use external joins are not supported. The following will be rejected:
    SELECT T2.ID FROM T2
    WHERE T2.ID=(
    SELECT COUNT ( * ) FROM
    (T1 LEFT JOIN T3 ON T1.ID=T3.ID)
    RIGHT JOIN T4 ON T3.ID=T4.ID)

 
PROPERTIES 

Product: 4D
Theme: Using SQL in 4D

 
HISTORY 

 
ARTICLE USAGE

4D SQL Reference ( 4D v16)