0% found this document useful (0 votes)
242 views3 pages

Understanding SQL Subqueries in Detail

This document discusses subqueries in ABAP programming. It defines subqueries as SELECT statements contained within conditions of WHERE or HAVING clauses. It describes the syntax of subqueries and explains that they can be nested and correlated. It also discusses scalar subqueries that return a single column and how subqueries can be used in conditions for comparisons. Examples are provided to illustrate correlated, scalar, and multiple-line subqueries.

Uploaded by

vtheamth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views3 pages

Understanding SQL Subqueries in Detail

This document discusses subqueries in ABAP programming. It defines subqueries as SELECT statements contained within conditions of WHERE or HAVING clauses. It describes the syntax of subqueries and explains that they can be nested and correlated. It also discusses scalar subqueries that return a single column and how subqueries can be used in conditions for comparisons. Examples are provided to illustrate correlated, scalar, and multiple-line subqueries.

Uploaded by

vtheamth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Subqueries (SAP Library - ABAP Programming (BC-ABA))

Page 1 of 3

Subqueries
AsubqueryisaspecialSELECTstatementcontainingasubquerywithinparticularconditionsoftheWHERE
[Link]:
(SELECT<result>
FROM<source>
[WHERE<condition>]
[GROUPBY<fields>]
[HAVING<cond>])
Asyoucansee,thissyntaxisrestrictedincomparisonwiththefullSELECTstatement,sinceitcontainsno
INTOorORDERBYclause.
Youcannestsubqueries,thatis,theWHEREandHAVINGclausesofsubqueriescanthemselvescontaina
[Link],itisknownas
[Link]
previouscondition.

Scalar Subqueries
Inascalarsubquery,theselectionintheSELECTclauseisrestrictedtoonecolumnoraggregateexpression.
Theexpression<result>oftheSELECTclauseis:
...<line>[<agg>]<s>
YoucanonlyenterasinglefieldintheSELECTclause.

Subqueries in Conditions
Anon-scalarsubquerycanonlyhaveaWHEREorHAVINGclause
inthe[NOT]EXISTS<subquery>[Link]
containsatleastone[no]line.

Scalar Subqueries in Conditions


Aswellasintheabovecondition,youcanalsousescalarsubqueriesinfurtherconditions.

Checking a Value of the Subquery


Thefollowingisapossibleconditionwithscalarsubqueries:
...<s>NOTIN<subquery>...
Theconditionistrueifthevalueof<s>is[not]containedintheresultssetofthescalarsubquery<subquery>.

Scalar Subqueries in Comparisons


Theotherconditionscanallbecomparisonswhoseoperatorsarecontainedinthetableforcomparisonswith
[Link],dependingonwhetherthesubqueryselectioncontains
oneormorelines.
Single-line Subquery
Iftheselectioninthesubqueryonlycontainsoneline,usethefollowingforthecomparison:
...<s><operator><subquery>...
Thevalueof<s>[Link]
false.
Thesubquerymayonlycontainoneline,[Link]-linesubquerycanbe
createdbycompletelydefiningthekeyintheWHEREclause.
Multiple-line Subquery
Iftheselectionfromthesubqueryreturnsmorethanoneline,youmustwritethecomparisonasfollows:
...<s><operator>ALL|ANY|SOME<subquery>...
IfyouusetheALLprefix,[Link]
usetheANYorSOMEprefix,theconditionisonlytrueifthecomparisonistrueforatleastonelineofthe
[Link](=orEQ)inconjunctionwithANYorSOMEhasthesameeffectastheIN
operatorforcheckingavalue.
IftheselectionfromthesubquerycontainsseverallinesandyoudonotusetheALL,ANY,orSOME
expression,aruntimeerroroccurs.

[Link] 9/1/2010

Subqueries (SAP Library - ABAP Programming (BC-ABA))

Page 2 of 3

Correlated,non-scalarsubquery:
REPORTdemo_select_subquery_1.
DATA:name_tabTYPETABLEOFscarr-carrname,
nameLIKELINEOFname_tab.
SELECTcarrname
INTOTABLEname_tab
FROMscarr
WHEREEXISTS(select*
FROMspfli
WHEREcarrid=scarr~carridAND
cityfrom='NEWYORK').
LOOPATname_tabINTOname.
WRITE:/name.
ENDLOOP.
ThisexampleselectsalllinesfromdatabasetableSCARRforairlinesthatflyfromNewYork.

Scalarsubquery:
REPORTdemo_select_subquery_2.
DATA:carr_idTYPEspfli-carridVALUE'LH',
conn_idTYPEspfli-connidVALUE'0400'.
DATA:cityTYPEsgeocity-city,
latiTYPEpDECIMALS2,
longiTYPEpDECIMALS2.
SELECTSINGLEcitylatitudelongitude
INTO(city,lati,longi)
FROMsgeocity
WHEREcityIN(selectcityfrom
FROMspfli
WHEREcarrid=carr_idAND
connid=conn_id).
WRITE:city,lati,longi.
ThisexamplereadsthelatitudeandlongitudeofthedeparturecityofflightLH402fromdatabase
tableSGEOCITY.

Scalarsubquery:
REPORTdemo_select_subquery_3.
DATA:waTYPEsflight,
planeLIKEwa-planetype,
seatsLIKEwa-seatsmax.
SELECTcarridconnidplanetypeseatsmaxMAX(seatsocc)
INTO(wa-carrid,wa-connid,wa-planetype,
wa-seatsmax,wa-seatsocc)
FROMsflight
GROUPBYcarridconnidplanetypeseatsmax
ORDERBYcarridconnid.
WRITE:/wa-carrid,
wa-connid,
wa-planetype,
wa-seatsmax,
wa-seatsocc.
HIDE:wa-carrid,wa-connid,wa-seatsmax.
ENDSELECT.
ATLINE-SELECTION.
WINDOWSTARTINGAT453ENDINGAT8513.
WRITE:'AlternativePlaneTypes',
'for',wa-carrid,wa-connid.

[Link] 9/1/2010

Subqueries (SAP Library - ABAP Programming (BC-ABA))

Page 3 of 3

ULINE.
SELECTplanetypeseatsmax
INTO(plane,seats)
FROMsaplaneASplane
WHEREseatsmax<wa-seatsmaxAND
seatsmax>=ALL(selectseatsocc
FROMsflight
WHEREcarrid=wa-carridAND
connid=wa-connid)
ORDERBYseatsmax.
WRITE:/plane,seats.
ENDSELECT.
Thelistoutput,afterdouble-clickingaline,lookslikethis:

Thedetaillistdisplaysallaircrafttypesthathavefewerseatsthanthecurrently-allocatedaircraft
type,butenoughtocarryallofthepassengerscurrentlybookedontheflight.

[Link] 9/1/2010

You might also like