In our SAP Technical Telegram Group, we discuss everything about SAP. UI5, OData, Fiori issue, New ABAP syntax, CDS approaches, Performance Tuning debates. You think of a topic, we discuss it in our group. No wonder, we are one of the largest SAP Technical group with zero spamming.
Yesterday, one member asked the below question. It was no doubt an interview question from one of the largest IT Company in the world.
Question: How to write the entries of first and second table to third table without SORT statement?

No SORT statement should be there in the solution.
The moment this question was floated, there was a debate among the members asking what is the business requirement? Why can we not use SORT?
Then the member who asked this question revealed it is one of the interview question from one the Big Multi National IT Companies.
Then the next debate started that we should not ask such questions in interview. But one of the member defended saying that SORTing Algorithms are asked by interviewers to eliminate at the very first stage of the selection process.
Ok, leaving behind the debate, whether such interview questions actually test the programming skill or not, everyone agreed to find a solution.
Also Read: Open SQL, CDS or AMDP, which Code to Data Technique to use?
We received 2 approaches with working code snippet.
Solution 1 – Provided by Sercan Küçükdemirci
<code>TYPES: tt_int TYPE TABLE OF i WITH EMPTY KEY. DATA(it_one) = VALUE tt_int( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ). DATA(it_two) = VALUE tt_int( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ). DATA:it_three TYPE tt_int. APPEND LINES OF it_one TO it_three. APPEND LINES OF it_two TO it_three. DATA(lv_len) = lines( it_three ). DATA(i) = 1. WHILE i < lv_len. DATA(lv_min) = it_three[ i ]. DATA(j) = i + 1. WHILE j < lv_len + 1 . IF it_three[ j ] < lv_min. DATA(lv_temp) = lv_min. lv_min = it_three[ j ]. it_three[ j ] = lv_temp. ENDIF. j = j + 1. ENDWHILE. it_three[ i ] = lv_min. i = i + 1. ENDWHILE.</code>
Team SAPYard validated the output. It is as per the requirement. Thank you Sercan!! You may connect with Sercan at LinkedIn.

Solution 2 – Provided by Stephan Koester
<code>TYPES: gtyt_interger TYPE TABLE OF i WITH EMPTY KEY, gtyt_interger_sorted TYPE SORTED TABLE OF i with NON-UNIQUE KEY table_line. DATA(lt_table1) = VALUE gtyt_interger( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ). DATA(lt_table2) = VALUE gtyt_interger( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ). DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted( lt_table1 ). INSERT LINES OF lt_table2 INTO TABLE lt_table3.</code>
Check the output.

Just the INSERT statement did the Magic!! Amazing solution.
Also Read:My First Program in S/4HANA
Every developer think differently. And there can be different ways to achieve the same solution. Solution to this question is a perfect example.
Our SAP Technical Group is just an example of how we can collaborate and learn every day. No question is too silly to ask. Ask, Answer, Propose, Depend, Share and Learn is the motto of our SAP Group.
This is the exact code snippet and output shared by Stephan. All of us can keep this for our quick reference for new ABAP syntax and usage.

Solution 3 – Provided by Stephan Koester
Stephan provided yet another solution without using INSERT statement. Check the below snippet.
<code>TYPES: gtyt_interger TYPE TABLE OF i WITH EMPTY KEY, gtyt_interger_sorted TYPE SORTED TABLE OF i WITH NON-UNIQUE KEY table_line. DATA(lt_table1) = VALUE gtyt_interger( ( 1 ) ( 9 ) ( 12 ) ( 16 ) ). DATA(lt_table2) = VALUE gtyt_interger( ( 4 ) ( 10 ) ( 15 ) ( 20 ) ). DATA(lt_table3) = CORRESPONDING gtyt_interger_sorted( BASE ( lt_table1 ) lt_table2 ). * Starting with NW7.51 It is possible to prevent duplicate entries using DISCARDING DUPLICATES * https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm cl_demo_output=>write_data( value = lt_table1 name = 'First Table' ). cl_demo_output=>write_data( value = lt_table2 name = 'Second Table' ). cl_demo_output=>write_data( value = lt_table3 name = 'Third Table' ). cl_demo_output=>display( ).</code>

He has used the new BASE key word. BASE is a functional operand position in which a database convertible to the target type can be specified. BASE is use to specify a start value base for the new structure of internal table.
You can read more about keyword BASE in SAP ABAPDOCU

Also, we can use DISCARDING DUPLICATES keywords for handling duplicate rows in component operator CORRESPONDING. Check more about DISCARDING DUPLICATES in ABAP Help.

You may follow Stephan Koester at LinkedIn. You may also check Stephan’s website Koester Consulting for more SAP blogs.
Would you like to share some weird Interview Questions you faced? Anything.. Technical or Non-Technical.
Please share.
Join our Telegram SAP Technical Discuss Group. You need to install Telegram App first.
Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.
Check HANA-ABAP Tutorials
- ABAP on SAP HANA. Part I. First Program in ABAP HANA
- ABAP on SAP HANA. Part II. ADT Eclipse and HANA Studio
- ABAP on SAP HANA. Part III. Debugging in ADT
- CDS Part 1. Core Data Services – Introduction
- CDS Part 2. Core Data Services – Deep Dive
- ABAP on SAP HANA. Part VI. New Age Open SQL ABAP 740
- ABAP on SAP HANA. Part VII. SQL Script and SAP HANA Stored Procedure
- ABAP on SAP HANA. Part VIII. ADBC – ABAP DataBase Connectivity
- ABAP on SAP HANA. Part IX. AMDP – ABAP Managed Database Procedure
- ABAP on SAP HANA. Part X. AMDP with SELECT OPTIONS
- ABAP on SAP HANA. Part XI. Are Native SQL and Open SQL Competitors?
- ABAP on SAP HANA. Part XII. Open SQL, CDS or AMDP, which Code to Data Technique to use?
- ABAP on SAP HANA. Part XIII. Sample Functional Specification of HANA Project
- ABAP on SAP HANA: Part XIV. HANA Ready, HANA-tization & HANA Plus
- CDS Part 3. Expose CDS Views as OData Service through Annotation
- ABAP on SAP HANA: Part XVI. HANAtization
- ABAP on SAP HANA: Part XVII. ATC – ABAP Test Cockpit Setup & Exemption Process
- SAP ABAP on HANA: Part XVIII. SALV IDA (Integrated Data Access)
- ABAP for SAP HANA. Part XIX. Sample Technical Specification of HANA Project
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- 4 Useful Tips on ABAP and ABAP on HANA
- Associations in HANA – A Conceptual Approach
- CDS Part 5. ABAP CDS Views With Authorization
- ABAP for SAP HANA. Part XXII. How to Consume Native HANA Views Using ADBC?
- CDS Part 6. Basic Expressions & Operations Available for CDS View – I
- 115 ABAP for SAP HANA Interview Questions & Answers
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- CDS Part 8. Usage of Built-In Functions in CDS – I
- CDS Part 9. Usage of Built-In Functions in CDS – II
- CDS Part 10. Usage of Built-In Functions in CDS – III
- CDS Part 11. How to Consume CDS View in Smart Business Service KPI Fiori Apps?
- CDS Part 12. Useful 6 CDS Related Tools in ADT
- CDS Part 13. Key Definition in CDS Views
- CDS Part 14. ABAP Annotations for Translatable Texts in CDS Views
- CDS Part 15. Associations in CDS Views – I
- ABAP for SAP HANA – Part 23. How to Access Database Schema Dynamically Using AMDP?
- Video Course – New Features and Syntaxes in SAP ABAP 7.40+ with Exercises
- CDS Part 16. Usage of Built-In Functions in CDS – IV
- ‘ABAP for SAP HANA’ Points to Remember
- Limitations of Open SQL Compared to SQL Script in HANA
- CDS Part 17. How to Overcome GUID Mismatch Linking Problem in ABAP CDS?
- Curious Case of ATC – Priority 2 – Low Performance Operations on Internal Tables