Open SQL Part 5: JOINs — When to Use and When to Avoid
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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
Interview questions on this topic
Related SAP topics
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.