Help Center> GaussDB(DWS)> Developer Guide> Data Read> Querying Joined Tables
Updated on 2023-10-31 GMT+08:00

Querying Joined Tables

Join Types

Multiple joins are necessary for accomplishing complex queries. Joins are classified into inner joins and outer joins. Each type of joins have their subtypes.

  • Inner join: inner join, cross join, and natural join.
  • Outer join: left outer join, right outer join, and full join.

To better illustrate the differences between these joins, the following provides some examples.

Create the sample tables student and math_score and insert data into them. Set enable_fast_query_shipping to off (on by default), that is, the query optimizer uses the distributed framework. Set explain_perf_mode to pretty (default value) to specify the EXPLAIN display format.

Inner Join

  • Inner join
    Syntax:

    Description: Rows that meet join_condition in the left table and the right table are joined and output. Tuples that do not meet join_condition are not output.

    Example 1: Query students' math scores.

  • Cross join

    Syntax:

    Description: Each row in the left table is joined with each row in the right table. The number of final rows is the product of the number of rows on both sides. The product is also called Cartesian product.

    Example 2: Cross join of student tables and math score tables.

  • Natural join

    Syntax:

    Description: Columns with the same name in left table and right table are joined by equi-join, and the columns with the same name are merged into one column.

    Example 3: Natural join between the student table and the math_score table. The columns with the same name in the two tables are the id columns, therefore equivalent join is performed based on the id columns.

Outer Join

  • Left Join

    Syntax:

    Description: The result set of a left outer join includes all rows of left table, not only the joined rows. If a row in the left table does not match any row in right table, the row will be NULL in the result set.

    Example 4: Perform left join on the student table and math_score table. The right table data corresponding to the row where ID is 3 in the student table is filled with NULL in the result set.

  • Right join

    Syntax:

    Description: Contrary to the left join, the result set of a right join includes all rows of the right table, not just the joined rows. If a row in the right table does not match any row in right table, the row will be NULL in the result set.

    Example 5: Perform right join on the student table and math_score table. The right table data corresponding to the row where ID is 6 in the math_score table is filled with NULL in the result set.

    In a right join, Left is displayed in the join operator. This is because a right join is actually the process replacing the left table with the right table then performing left join.

  • Full join

    Syntax:

    Description: A full join is a combination of a left outer join and a right outer join. The result set of a full outer join includes all rows of the left table and the right table, not just the joined rows. If a row in the left table does not match any row in the right table, the row will be NULL in the result set. If a row in the right table does not match any row in right table, the row will be NULL in the result set.

    Example 6: Perform full join on the student table and math_score table. The right table data corresponding to the row where ID is 3 is filled with NULL in the result set. The left table data corresponding to the row where ID is 6 is filled with NULL in the result set.

Differences Between the ON Condition and the WHERE Condition in Multi-Table Query

According to the preceding join syntax, except natural join and cross join, the ON condition (USING is converted to the ON condition during query parsing) is used on the join result of both the two tables. Generally, the WHERE condition is used in the query statement to restrict the query result. The ON join condition and WHERE filter condition do not contain conditions that can be pushed down to tables. The differences between ON and WHERE are as follows:

  • The ON condition is used for joining two tables.
  • WHERE is used to filter the result set.

To sum up, the ON condition is used when two tables are joined. After the join result set of two tables is generated, the WHERE condition is used.