ERPClimb logoERPClimb
Open SQL
ABAP Developmentintermediate 9 min read

Open SQL Part 5: JOINs — When to Use and When to Avoid

Preview · a free account unlocks the full lesson

Key takeaway

Combine tables in one statement: INNER JOIN vs LEFT OUTER JOIN, ON conditions with keys, aliases and the selector, joins on buffered tables, and when JOIN, FOR ALL ENTRIES or a CDS view is the better choice.

In this lessonShow sections

Visual overview

Open SQL Part 5: JOINs — When to Use and When to Avoid — learning sheetIn one line: Combine tables in one statement: INNER JOIN vs LEFT OUTER JOIN, ON conditions with keys, aliases and the ~ selector, joins on buffered tables, and when JOIN, FOR ALL ENTRIES or a CDS view is the better choice.ERPClimb.com / ABAP / LESSON 5/10 / LEARNING SHEETOpen SQL Part 5: JOINs — When to Use and When to Avoid1 · IN ONE LINECombine tables in one statement: INNER JOIN vs LEFT OUTER JOIN, ON conditions with keys,aliases and the ~ selector, joins on buffered tables, and when JOIN, FOR ALL ENTRIES or aCDS view is the better choice.2 · KEY IDEAS EXPLAINEDInner JoinSELECT f~carrid f~connid f~fldate b~bookidb~customidLeft Outer JoinCondition in ON → filters the right side, leftrows stay.Condition in WHERE on the right table → removesleft rows without match (turns it into an innerjoin).Row MultiplicationOne flight with 200 bookings produces 200 resultrows. That is correct for booking lists but wrongif you then sum flight-level fields (e.g…How Many Tables?Joins of 2–4 tables on key fields are normal. Verylarge joins over many tables become hard to readand to optimize — model them as CDS views (see…3 · DO · AVOIDBest practice!Common mistakeJoin on key fields with aliases and ~.Put right-table filters of outer joins intoON.Aggregate at the correct level.✕Right-table conditions in WHERE of a LEFTOUTER JOIN (unintended inner join).✕Joining on non-key fields and multiplyingrows.✕Summing header fields after joining items.4 · IN A REAL PROJECTA report of open orders per customer joined order headers with items and then summed the header's freightcharge. Customers with 10 items showed 10 times the freight. The join itself was correct — the aggregationhappened at the wrong level. The team summed item values from the joined result and read header freight…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 3 of this series combined tables using FOR ALL ENTRIES; a JOIN combines them in a single database statement instead, which is usually faster, especially on HANA. An INNER JOIN returns only the rows that have a match on both sides, while a LEFT OUTER JOIN keeps every row from the left-hand table regardless of whether a match exists.

An inner join looks like: SELECT f~carrid f~connid f~fldate b~bookid b~customid FROM sflight AS f INNER JOIN sbook AS b ON b~carrid = f~carrid AND b~connid = f~connid AND b~fldate = f~fldate INTO TABLE gt_result WHERE f~carrid = p_carr. — flights that have no bookings at all simply don't appear in the result. Join on key fields, ideally the full key of the right-hand table, to avoid unexpectedly multiplying rows; and use table aliases together with the ~ column selector, as in f~carrid or b~bookid, to keep the statement readable.

A left outer join, written as FROM sflight AS f LEFT OUTER JOIN sbook AS b ON …, keeps every flight, leaving the booking fields empty where no booking exists.

Older releases impose a classic restriction here: WHERE conditions on the right-hand table aren't allowed at all, and need to go into the ON clause instead; newer releases relax this, but where you put a condition still changes the meaning — a condition placed in ON filters only the right side while keeping every left row, whereas a condition placed in WHERE on the right-hand table removes left rows that have no match, effectively turning the outer join back into an inner join.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.