6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24DDIC0608 – Understanding Joins and Their Types

What is a Join?

A join is a SQL operation that allows you to combine data from two or more tables based on a common field between them. This helps in retrieving related data from multiple tables in a single query.

Types of Joins

ABAP supports several types of joins, each serving a different purpose. The most common types are:

  1. Inner Join
  2. Left Outer Join
  3. Right Outer Join
  4. Cross Join

 

Inner Join

An Inner Join returns only the rows where there is a match in both tables. If there is no match, the row is not included in the result set.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  INNER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

In this example, the query retrieves rows from table1 and table2 where common_field matches in both tables.

 

Left Outer Join

A Left Outer Join returns all rows from the left table and the matched rows from the right table. If there is no match, the result is NULL from the right table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  LEFT OUTER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

This query retrieves all rows from table1, and the matching rows from table2. If there is no match, it shows NULL for columns from table2.

 

Right Outer Join

A Right Outer Join is similar to a Left Outer Join but retrieves all rows from the right table and the matched rows from the left table. If there is no match, the result is NULL from the left table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  RIGHT OUTER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

This query retrieves all rows from table2, and the matching rows from table1. If there is no match, it shows NULL for columns from table1.

 

Cross Join

A Cross Join returns the Cartesian product of the two tables, meaning it combines each row of the first table with each row of the second table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a,

       table2 AS b.

 

This query retrieves all possible combinations of rows from table1 and table2.

 

When to Use Each Join Type

– Inner Join: Use when you only need rows with matching values in both tables.

– Left Outer Join: Use when you need all rows from the left table, regardless of whether there is a match in the right table.

– Right Outer Join: Use when you need all rows from the right table, regardless of whether there is a match in the left table.

– Cross Join: Use when you need all possible combinations of rows from both tables.

Author : Aniket Pawar, 9373518385                                                     

24DDIC0608 – Understanding Joins and Their Types

What is a Join?

A join is a SQL operation that allows you to combine data from two or more tables based on a common field between them. This helps in retrieving related data from multiple tables in a single query.

Types of Joins

ABAP supports several types of joins, each serving a different purpose. The most common types are:

  1. Inner Join
  2. Left Outer Join
  3. Right Outer Join
  4. Cross Join

 

Inner Join

An Inner Join returns only the rows where there is a match in both tables. If there is no match, the row is not included in the result set.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  INNER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

In this example, the query retrieves rows from table1 and table2 where common_field matches in both tables.

 

Left Outer Join

A Left Outer Join returns all rows from the left table and the matched rows from the right table. If there is no match, the result is NULL from the right table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  LEFT OUTER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

This query retrieves all rows from table1, and the matching rows from table2. If there is no match, it shows NULL for columns from table2.

 

Right Outer Join

A Right Outer Join is similar to a Left Outer Join but retrieves all rows from the right table and the matched rows from the left table. If there is no match, the result is NULL from the left table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a

  RIGHT OUTER JOIN table2 AS b

  ON a~common_field = b~common_field.

 

This query retrieves all rows from table2, and the matching rows from table1. If there is no match, it shows NULL for columns from table1.

 

Cross Join

A Cross Join returns the Cartesian product of the two tables, meaning it combines each row of the first table with each row of the second table.

Example:

SELECT a~field1, b~field2

  INTO TABLE @DATA(result)

  FROM table1 AS a,

       table2 AS b.

 

This query retrieves all possible combinations of rows from table1 and table2.

 

When to Use Each Join Type

– Inner Join: Use when you only need rows with matching values in both tables.

– Left Outer Join: Use when you need all rows from the left table, regardless of whether there is a match in the right table.

– Right Outer Join: Use when you need all rows from the right table, regardless of whether there is a match in the left table.

– Cross Join: Use when you need all possible combinations of rows from both tables.

Author : Aniket Pawar, 9373518385