Open SQL Part 2: SELECT Basics — Field Lists, WHERE and INTO
Lessons are open to try during our launch period. Some advanced content may move to Pro later. AI practice limits apply separately.
Write correct, efficient SELECT statements: explicit field lists instead of SELECT , WHERE conditions with ranges and patterns, ORDER BY, INTO TABLE vs INTO CORRESPONDING FIELDS, and UP TO n ROWS.
In this lessonShow sectionsHide sections
Visual overview
Expert visual walkthrough
SELECT Basics — Field Lists, WHERE and INTO
Open SQL · Chapter-specific technical teaching map
Compare key concepts
SELECT inside LOOP: costly
Set-based Open SQL
Filter and project columns
Measure using ST05 / SAT
Tutor's chapter takeaway
Write correct, efficient SELECT statements: explicit field lists instead of SELECT *, WHERE conditions with ranges and patterns, ORDER BY, INTO TABLE vs INTO CORRESPONDING FIELDS, and UP TO n ROWS.
In a real project
A sales report selected all fields of a 300-column document table and then deleted 95% of the rows in ABAP because only one sales organization was relevant. The report needed 20 minutes and a lot of memory. Moving the…
Watch out for
• SELECT * on wide tables. • Filtering in ABAP instead of WHERE. • INTO TABLE with a different field order than the target structure. • Leading wildcards in LIKE on large tables ('%abc'). • Not handling empty results.
The situation: a real project scenario
A sales report selected all fields of a 300-column document table and then deleted 95% of the rows in ABAP because only one sales organization was relevant. The report needed 20 minutes and a lot of memory.
Moving the organization and date filter into WHERE and selecting only the 12 needed columns reduced runtime to under a minute and memory to a fraction.
Explanation
Part 1 explained Open SQL in general; this part covers the SELECT statement you will actually write every day.
The core habits are simple but matter a lot: select only the columns you need, since SELECT * transfers every field of what can be a wide table; put every filter you possibly can into the WHERE clause, rather than reading rows into ABAP just to throw them away; and know your INTO target, since INTO TABLE requires the target's field order and types to match positionally, while INTO CORRESPONDING FIELDS maps columns by name instead.
You can also use SELECT-OPTIONS directly in a WHERE clause, for example WHERE fldate IN s_fldate.
On the field list, a statement like SELECT carrid connid fldate price currency FROM sflight INTO TABLE gt_flight transfers less data and uses less memory than SELECT *. On HANA's column store, fewer columns also means faster reads. Reach for SELECT * only when you genuinely need almost all the fields.
WHERE conditions give you several building blocks: the comparison operators (=, <>, <, >, <=, >=, or their keyword forms EQ, NE, LT, and so on); BETWEEN a AND b; IN (list) or IN range_table for SELECT-OPTIONS; LIKE 'LH%' for pattern matching, where % matches any sequence and _ matches a single character; and IS NULL / IS NOT NULL, which is rarely needed for ordinary dictionary tables but becomes relevant with outer joins.
You can combine all of these with AND / OR and parentheses, and wherever possible you should filter on key or indexed fields — the Performance Tuning topic covers indexes in more depth.
For INTO targets, INTO TABLE itab requires the rows in your field list to match the table's line structure positionally, while INTO CORRESPONDING FIELDS OF TABLE itab matches by field name instead, which is safer when the structures differ, at the cost of a little overhead. APPENDING TABLE adds rows to a table that already has content. With strict ABAP syntax, INTO TABLE @DATA(lt) creates a new internal table automatically, matching the selected fields.
ORDER BY lets you sort by PRIMARY KEY or by an explicit field with DESCENDING; sorting at the database level is fine for result lists, but for internal processing you may still want to SORT again in ABAP. UP TO n ROWS limits the number of rows returned, which is handy for previews, top-N queries combined with ORDER BY, or simple existence checks.
Finally, always consider the empty-result case: check sy-subrc = 4 to detect when no rows were found, and remember that an empty table is valid business data in its own right — it needs to be handled deliberately, whether that means showing a message or simply displaying empty output, rather than assuming something went wrong.
To practice: run the code example with different selection-screen inputs, compare SELECT * against a field list in ST05 (covered in Part 8) for the same WHERE condition, and try INTO TABLE with a structure whose fields are in a different order, to see firsthand why CORRESPONDING FIELDS exists. By the end of this your SELECTs should read exactly the rows and columns you need, nothing more.
Code example
REPORT zosql_part2. TABLES sflight.SELECT-OPTIONS: s_carr FOR sflight-carrid DEFAULT 'LH', s_fldate FOR sflight-fldate. TYPES: BEGIN OF ty_flight, carrid TYPE s_carr_id, connid TYPE s_conn_id, fldate TYPE s_date, price TYPE s_price, currency TYPE s_currcode, END OF ty_flight, BEGIN OF ty_carr, carrid TYPE s_carr_id, carrname TYPE s_carrname, END OF ty_carr. DATA: gt_flight TYPE STANDARD TABLE OF ty_flight, gs_flight TYPE ty_flight, gt_other TYPE STANDARD TABLE OF sflight, gt_carr TYPE STANDARD TABLE OF ty_carr, gt_top TYPE STANDARD TABLE OF ty_flight. START-OF-SELECTION.* ===== 1. Field list + WHERE with ranges + ORDER BY ===== SELECT carrid connid fldate price currency " same order as ty_flight FROM sflight INTO TABLE gt_flight WHERE carrid IN s_carr " IN with a SELECT-OPTIONS range: matches any value/interval the user entered AND fldate IN s_fldate AND seatsocc < 200 ORDER BY carrid connid fldate. " sort pushed to the DB instead of SORT in ABAP afterwards IF sy-subrc <> 0. MESSAGE 'No flights for this selection' TYPE 'S' DISPLAY LIKE 'W'. RETURN. ENDIF. * ===== 2. INTO CORRESPONDING FIELDS: mapping by name ===== SELECT carrid connid fldate price currency FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_other " wide target, only 5 fields filled; matches by field name, not position WHERE carrid IN s_carr. * ===== 3. Pattern search ===== SELECT carrid carrname FROM scarr INTO TABLE gt_carr WHERE carrname LIKE 'L%'. " LIKE with a trailing wildcard can still use an index; leading '%' usually can't * ===== 4. Top 5 most expensive flights ===== SELECT carrid connid fldate price currency FROM sflight INTO TABLE gt_top UP TO 5 ROWS WHERE carrid IN s_carr ORDER BY price DESCENDING. " ORDER BY before UP TO ROWS -> "top N", not just "first 5 found" LOOP AT gt_flight INTO gs_flight. WRITE: / gs_flight-carrid, gs_flight-connid, gs_flight-fldate, gs_flight-price CURRENCY gs_flight-currency, gs_flight-currency. ENDLOOP. * AVOID: SELECT * ... and then DELETE gt_flight WHERE seatsocc >= 200. -> filter in WHERE* 💡 Modern syntax: SELECT carrid, carrname FROM scarr WHERE ... INTO TABLE @DATA(lt_carr).Watch out
Common mistakes
- SELECT * on wide tables.
- Filtering in ABAP instead of WHERE.
- INTO TABLE with a different field order than the target structure.
- Leading wildcards in LIKE on large tables ('%abc').
- Not handling empty results.
Do this
Best practices
- Explicit field lists.
- All filters in WHERE; use ranges from the selection screen.
- INTO CORRESPONDING FIELDS when structures differ.
- ORDER BY when the order matters.
- UP TO n ROWS for top-N and previews.
In the interview
Interview angle
Questions: "Why avoid SELECT *?", "INTO TABLE vs INTO CORRESPONDING FIELDS?", "How do you use SELECT-OPTIONS in SQL?"
Strong answer: SELECT * transfers unnecessary data and memory (especially on wide/column-store tables); INTO TABLE maps positionally, CORRESPONDING by name; SELECT-OPTIONS are used with IN in WHERE; filter everything possible in the database; handle sy-subrc 4.
Practice this lesson
Quick check
0 of 3 questions attempted
Question 1 of 3
Which SELECT uses a selection-screen range correctly?
Question 2 of 3
Why should explicit field lists be preferred over SELECT * on wide tables?
Question 3 of 3
Which addition avoids duplicates in the result set of SELECT?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1A monthly billing report selects every invoice and filters in ABAP. What is the fix?
2A finance extraction selects all BKPF fields for every document. How would you improve it?
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: FOR ALL ENTRIES with an empty driver table →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Interview questions on this topic
Related SAP topics
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
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.