IT CanvassTalk to an advisor
SAP examples · LessonBy , SAP Trainer, 13 yrs · Published · SAP S/4HANA 2023 · all levels

SQL examples

These SQL examples show querying SAP data effectively, whether Open SQL in ABAP or native HANA SQL, with an emphasis on efficient, set-based access.

Quick answer

Write set-based SQL that filters (WHERE), joins and aggregates in the database, returning only what you need.

Key takeaways
  • Watch out: SELECT * / no WHERE, reading too much.
  • Worked examples: open sql in abap (modern syntax), joins and aggregation, what it teaches.
  • How SQL examples fits into SAP development and the wider SAP landscape

Open SQL in ABAP (modern syntax)

SELECT ebeln, SUM( netwr ) AS total
  FROM ekpo
  WHERE werks = @lv_plant
  GROUP BY ebeln
  INTO TABLE @DATA(lt_po_totals).

Joins and aggregation

SELECT k~ebeln, k~lifnr, SUM( p~netwr ) AS total
  FROM ekko AS k INNER JOIN ekpo AS p ON k~ebeln = p~ebeln
  WHERE k~bsart = 'NB'
  GROUP BY k~ebeln, k~lifnr
  INTO TABLE @DATA(lt).

What it teaches

Write set-based SQL that filters (WHERE), joins and aggregates in the database, returning only what you need. This is efficient on any database and essential on HANA (code-to-data). Avoid SELECT *, always use a WHERE, and never loop the database.

Common pitfalls

  • SELECT * / no WHERE, reading too much.
  • Aggregating in ABAP instead of SQL.
  • SELECT in a loop.

Practice challenge

+0 XPStreak ×0
Question 1 of 3
Which statement is true of SQL examples?

Frequently asked questions

What does the term SQL examples refer to in SAP?
These SQL examples show querying SAP data effectively, whether Open SQL in ABAP or native HANA SQL, with an emphasis on efficient, set-based access.
What is worth remembering about SQL examples in practice?
Write set-based SQL that filters (WHERE), joins and aggregates in the database, returning only what you need.
What tends to go wrong with SQL examples?
SELECT * / no WHERE, reading too much. Aggregating in ABAP instead of SQL. SELECT in a loop.
Already working on SAP and stuck on a live ticket?Get an expert SAP developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support