Skip to Content
author's profile photo Former Member
Former Member

SQL select statements

hi,

how do i code the SQL select statements that uses a JOIN function for 2 different tables?

please give me examples of the SQL statements.

thanks. will reward if useful.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 06:51 AM

    DATA: DATE LIKE SFLIGHT-FLDATE,

    CARRID LIKE SFLIGHT-CARRID,

    CONNID LIKE SFLIGHT-CONNID.

    SELECT FCARRID FCONNID F~FLDATE

    INTO (CARRID, CONNID, DATE)

    FROM SFLIGHT AS F INNER JOIN SPFLI AS P

    ON FCARRID = PCARRID AND

    FCONNID = PCONNID

    WHERE P~CITYFROM = 'FRANKFURT'

    AND P~CITYTO = 'NEW YORK'

    AND F~FLDATE BETWEEN '20010910' AND '20010920'

    AND FSEATSOCC < FSEATSMAX.

    WRITE: / DATE, CARRID, CONNID.

    ENDSELECT.

    awrd points if useful

    Bhupal

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 06:51 AM

    check this thread...

    joins

    inner join

    IF p_bsart IS INITIAL.

    SELECT ekko~bukrs

    ekko~lifnr

    ekko~ebeln

    ekko~waers

    ekko~bsart

    ekko~ekorg

    ekko~ekgrp

    ekpo~ebelp

    ekpo~txz01

    ekpo~matnr

    ekpo~werks

    ekpo~menge

    ekpo~meins

    ekpo~netpr

    ekpo~netwr

    INTO TABLE t_itab1 FROM

    ekko INNER JOIN ekpo ON ekkoebeln = ekpoebeln

    WHERE ekko~ebeln IN s_ebeln AND

    ekko~bukrs IN s_bukrs AND

    ekko~lifnr IN s_lifnr AND

    ekko~ekorg IN s_ekorg AND

    ekko~ekgrp IN s_ekgrp AND

    ekpo~matnr IN s_matnr.

    The difference between an INNER JOIN and an OUTER JOIN is the following. If a query on an INNER JOIN of VBAK (outer table) and VBAP (inner table) finds a record in VBAK but no matching records in VBAP, then no data is retrieved from the database because the inner table is empty. If you still want to keep VBAK rows for which there are no matching VBAP rows, you need to use the OUTER JOIN construct available in ABAP/4 Open SQL in 4.x..

    Hi

    Syntax

    ... [(] {dbtab_left AS tabalias_left} | join

    {INNER JOIN}|{LEFT OUTER JOIN}

    {dbtab_right AS tabalias_right ON join_cond} [)] ... .

    Effect

    The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of INNER JOIN or LEFT OUTER JOIN . Depending on the type of join, a join expression can be either an inner ( INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.

    On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.

    AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.

    The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:

    At least one comparison must be specified after ON.

    Individual comparisons may be joined using AND only.

    All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.

    The following language elements may not be used: BETWEEN, LIKE, IN.

    No sub-queries may be used.

    For outer joins, only equality comparisons (=, EQ) are possible.

    If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.

    In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.

    Resulting set for inner join

    The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.

    Resulting set for outer join

    The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.

    Example

    Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.

    PARAMETERS: p_cityfr TYPE spfli-cityfrom,

    p_cityto TYPE spfli-cityto.

    DATA: BEGIN OF wa,

    fldate TYPE sflight-fldate,

    carrname TYPE scarr-carrname,

    connid TYPE spfli-connid,

    END OF wa.

    DATA itab LIKE SORTED TABLE OF wa

    WITH UNIQUE KEY fldate carrname connid.

    SELECT ccarrname pconnid f~fldate

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM ( ( scarr AS c

    INNER JOIN spfli AS p ON pcarrid = ccarrid

    AND p~cityfrom = p_cityfr

    AND p~cityto = p_cityto )

    INNER JOIN sflight AS f ON fcarrid = pcarrid

    AND fconnid = pconnid ).

    LOOP AT itab INTO wa.

    WRITE: / wa-fldate, wa-carrname, wa-connid.

    ENDLOOP.

    Example

    Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.

    PARAMETERS p_cityfr TYPE spfli-cityfrom.

    DATA: BEGIN OF wa,

    carrid TYPE scarr-carrid,

    carrname TYPE scarr-carrname,

    connid TYPE spfli-connid,

    END OF wa,

    itab LIKE SORTED TABLE OF wa

    WITH NON-UNIQUE KEY carrid.

    SELECT scarrid scarrname p~connid

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM scarr AS s

    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid

    AND p~cityfrom = p_cityfr.

    LOOP AT itab INTO wa.

    IF wa-connid = '0000'.

    WRITE: / wa-carrid, wa-carrname.

    ENDIF.

    ENDLOOP

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 06:51 AM

    Hai,

    chek this.

    Joins are used to fetch data fast from Database tables:

    Tables are joined with the proper key fields to fetch the data properly.

    If there are no proper key fields between tables don't use Joins;

    Important thing is that don't USE JOINS FOR CLUSTER tableslike BSEG and KONV.

    Only use for Transparenmt tables.

    You can also use joins for the database VIews to fetch the data.

    JOINS

    ... FROM tabref1 [INNER] JOIN tabref2 ON cond

    Effect

    The data is to be selected from transparent database tables and/or views determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as in variant 1 or are themselves Join expressions. The keyword INNER does not have to be specified. The database tables or views determined by tabref1 and tabref2 must be recognized by the ABAP Dictionary.

    In a relational data structure, it is quite normal for data that belongs together to be split up across several tables to help the process of standardization (see relational databases). To regroup this information into a database query, you can link tables using the join command. This formulates conditions for the columns in the tables involved. The inner join contains all combinations of lines from the database table determined by tabref1 with lines from the table determined by tabref2, whose values together meet the logical condition (join condition) specified using ON>cond.

    Inner join between table 1 and table 2, where column D in both tables in the join condition is set the same:

    Table 1 Table 2










    A

    B

    C

    D

    D

    E

    F

    G

    H










    a1

    b1

    c1

    1

    1

    e1

    f1

    g1

    h1

    a2

    b2

    c2

    1

    3

    e2

    f2

    g2

    h2

    a3

    b3

    c3

    2

    4

    e3

    f3

    g3

    h3

    a4

    b4

    c4

    3






    |-- | | | --|

    \ /

    \ /

    \ /

    \ /

    \/

    Inner Join










    A

    B

    C

    D

    D

    E

    F

    G

    H










    a1

    b1

    c1

    1

    1

    e1

    f1

    g1

    h1

    a2

    b2

    c2

    1

    1

    e1

    f1

    g1

    h1

    a4

    b4

    c4

    3

    3

    e2

    f2

    g2

    h2

    |-- | | | | | | | | --|

    Example

    Output a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:

    DATA: DATE LIKE SFLIGHT-FLDATE,

    CARRID LIKE SFLIGHT-CARRID,

    CONNID LIKE SFLIGHT-CONNID.

    SELECT FCARRID FCONNID F~FLDATE

    INTO (CARRID, CONNID, DATE)

    FROM SFLIGHT AS F INNER JOIN SPFLI AS P

    ON FCARRID = PCARRID AND

    FCONNID = PCONNID

    WHERE P~CITYFROM = 'FRANKFURT'

    AND P~CITYTO = 'NEW YORK'

    AND F~FLDATE BETWEEN '20010910' AND '20010920'

    AND FSEATSOCC < FSEATSMAX.

    WRITE: / DATE, CARRID, CONNID.

    ENDSELECT.

    If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or a table alias.

    Note

    In order to determine the result of a SELECT command where the FROM clause contains a join, the database system first creates a temporary table containing the lines that meet the ON condition. The WHERE condition is then applied to the temporary table. It does not matter in an inner join whether the condition is in the ON or WHEREclause. The following example returns the same solution as the previous one.

    Example

    Output of a list of all flights from Frankfurt to New York between September 10th and 20th, 2001 that are not sold out:

    DATA: DATE LIKE SFLIGHT-FLDATE,

    CARRID LIKE SFLIGHT-CARRID,

    CONNID LIKE SFLIGHT-CONNID.

    SELECT FCARRID FCONNID F~FLDATE

    INTO (CARRID, CONNID, DATE)

    FROM SFLIGHT AS F INNER JOIN SPFLI AS P

    ON FCARRID = PCARRID

    WHERE FCONNID = PCONNID

    AND P~CITYFROM = 'FRANKFURT'

    AND P~CITYTO = 'NEW YORK'

    AND F~FLDATE BETWEEN '20010910' AND '20010920'

    AND FSEATSOCC < FSEATSMAX.

    WRITE: / DATE, CARRID, CONNID.

    ENDSELECT.

    Note

    Since not all of the database systems supported by SAP use the standard syntax for ON conditions, the syntax has been restricted. It only allows those joins that produce the same results on all of the supported database systems:

    Only a table or view may appear to the right of the JOIN operator, not another join expression.

    Only AND is possible in the ON condition as a logical operator.

    Each comparison in the ON condition must contain a field from the right-hand table.

    If an outer join occurs in the FROM clause, all the ON conditions must contain at least one "real" JOIN condition (a condition that contains a field from tabref1 amd a field from tabref2.

    Note

    In some cases, '*' may be specified in the SELECT clause, and an internal table or work area is entered into the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the FROM clause, according to the structure of each table work area. There can then be gaps between table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, not simply by counting the total number of fields. For an example, see below:

    Variant 3

    ... FROM tabref1 LEFT [OUTER] JOIN tabref2 ON cond

    Effect

    Selects the data from the transparent database tables and/or views specified in tabref1 and tabref2. tabref1 und tabref2 both have either the same form as in variant 1 or are themselves join expressions. The keyword OUTER can be omitted. The database tables or views specified in tabref1 and tabref2 must be recognized by the ABAP-Dictionary.

    In order to determine the result of a SELECT command where the FROM clause contains a left outer join, the database system creates a temporary table containing the lines that meet the ON condition. The remaining fields from the left-hand table (tabref1) are then added to this table, and their corresponding fields from the right-hand table are filled with ZERO values. The system then applies the WHERE condition to the table.

    Left outer join between table 1 and table 2 where column D in both tables set the join condition:

    Table 1 Table 2










    A

    B

    C

    D

    D

    E

    F

    G

    H










    a1

    b1

    c1

    1

    1

    e1

    f1

    g1

    h1

    a2

    b2

    c2

    1

    3

    e2

    f2

    g2

    h2

    a3

    b3

    c3

    2

    4

    e3

    f3

    g3

    h3

    a4

    b4

    c4

    3






    |-- | | | --|

    \ /

    \ /

    \ /

    \ /

    \/

    Left Outer Join










    A

    B

    C

    D

    D

    E

    F

    G

    H










    a1

    b1

    c1

    1

    1

    e1

    f1

    g1

    h1

    a2

    b2

    c2

    1

    1

    e1

    f1

    g1

    h1

    a3

    b3

    c3

    2

    NULL

    NULL

    NULL

    NULL

    NULL

    a4

    b4

    c4

    3

    3

    e2

    f2

    g2

    h2

    |-- | | | | | | | | --|

    Example

    Output a list of all custimers with their bookings for October 15th, 2001:

    DATA: CUSTOMER TYPE SCUSTOM,

    BOOKING TYPE SBOOK.

    SELECT SCUSTOMNAME SCUSTOMPOSTCODE SCUSTOM~CITY

    SBOOKFLDATE SBOOKCARRID SBOOKCONNID SBOOKBOOKID

    INTO (CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,

    BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,

    BOOKING-BOOKID)

    FROM SCUSTOM LEFT OUTER JOIN SBOOK

    ON SCUSTOMID = SBOOKCUSTOMID AND

    SBOOK~FLDATE = '20011015'

    ORDER BY SCUSTOMNAME SBOOKFLDATE.

    WRITE: / CUSTOMER-NAME, CUSTOMER-POSTCODE, CUSTOMER-CITY,

    BOOKING-FLDATE, BOOKING-CARRID, BOOKING-CONNID,

    BOOKING-BOOKID.

    ENDSELECT.

    If there are columns with the same name in both tables, you must distinguish between them by prefixing the field descriptor with the table name or using an alias.

    Note

    For the resulting set of a SELECT command with a left outer join in the FROM clause, it is generally of crucial importance whether a logical condition is in the ON or WHERE condition. Since not all of the database systems supported by SAP themselves support the standard syntax and semantics of the left outer join, the syntax has been restricted to those cases that return the same solution in all database systems:

    Only a table or view may come after the JOIN operator, not another join statement.

    The only logical operator allowed in the ON condition is AND.

    Each comparison in the ON condition must contain a field from the right-hand table.

    Comparisons in the WHERE condition must not contain a field from the right-hand table.

    The ON condition must contain at least one "real" JOIN condition (a condition in which a field from tabref1 as well as from tabref2 occurs).

    Note

    In some cases, '*' may be specivied as the field list in the SELECT clause, and an internal table or work area is entered in the INTO clause (instead of a list of fields). If so, the fields are written to the target area from left to right in the order in which the tables appear in the llen in der FROM clause, according to the structure of each table work area. There can be gaps between the table work areas if you use an Alignment Request. For this reason, you should define the target work area with reference to the types of the database tables, as in the following example (not simply by counting the total number of fields).

    Example

    Example of a JOIN with more than two tables: Select all flights from Frankfurt to New York between September 10th and 20th, 2001 where there are available places, and display the name of the airline.

    DATA: BEGIN OF WA,

    FLIGHT TYPE SFLIGHT,

    PFLI TYPE SPFLI,

    CARR TYPE SCARR,

    END OF WA.

    SELECT * INTO WA

    FROM ( SFLIGHT AS F INNER JOIN SPFLI AS P

    ON FCARRID = PCARRID AND

    FCONNID = PCONNID )

    INNER JOIN SCARR AS C

    ON FCARRID = CCARRID

    WHERE P~CITYFROM = 'FRANKFURT'

    AND P~CITYTO = 'NEW YORK'

    AND F~FLDATE BETWEEN '20010910' AND '20010920'

    AND FSEATSOCC < FSEATSMAX.

    WRITE: / WA-CARR-CARRNAME, WA-FLIGHT-FLDATE, WA-FLIGHT-CARRID,

    WA-FLIGHT-CONNID.

    ENDSELECT.

    with regards.

    b.Sowjanya

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 06:51 AM

    select a~bukrs

    a~anln1

    a~anln2

    a~anlkl

    a~txt50

    a~ord41

    a~ord42

    a~ord43

    b~afabe

    b~ndabj

    b~ndabp

    b~kansw

    b~knafa

    b~kaafa

    b~nafag

    b~nafap

    b~aafam

    b~aafag

    c~kostl

    d~afasl

    d~ndjar

    d~ndper

    into corresponding fields of table gt_master

    from ( ( anla as a inner join anlc as b

    on abukrs = bbukrs

    and aanln1 = banln1

    and aanln2 = banln2 )

    inner join anlz as c

    on abukrs = cbukrs

    and aanln1 = canln1

    and aanln2 = canln2 )

    inner join anlb as d

    on abukrs = dbukrs

    and aanln1 = danln1

    and aanln2 = danln2

    where a~bukrs in s_comp.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2008 at 06:52 AM

    hai,

    check syntax.

    Syntax

    ... [(] {dbtab_left [AS tabalias_left]} | join

    {[INNER] JOIN}|{LEFT [OUTER] JOIN}

    {dbtab_right [AS tabalias_right] ON join_cond} [)] ... .

    Effect

    The join syntax represents a recursively nestable join expression. A join expression consists of a left-hand and a right- hand side, which are joined either by means of [INNER] JOIN or LEFT [OUTER] JOIN . Depending on the type of join, a join expression can be either an inner ( INNER) or an outer (LEFT OUTER) join. Every join expression can be enclosed in round brackets. If a join expression is used, the SELECT command circumvents SAP buffering.

    On the left-hand side, either a single database table, a view dbtab_left, or a join expression join can be specified. On the right-hand side, a single database table or a view dbtab_right as well as join conditions join_cond can be specified after ON. In this way, a maximum of 24 join expressions that join 25 database tables or views with each other can be specified after FROM.

    AS can be used to specify an alternative table name tabalias for each of the specified database table names or for every view. A database table or a view can occur multiple times within a join expression and, in this case, have various alternative names.

    The syntax of the join conditions join_cond is the same as that of the sql_cond conditions after the addition WHERE, with the following differences:

    At least one comparison must be specified after ON.

    Individual comparisons may be joined using AND only.

    All comparisons must contain a column in the database table or the view dbtab_right on the right-hand side as an operand.

    The following language elements may not be used: BETWEEN, LIKE, IN.

    No sub-queries may be used.

    For outer joins, only equality comparisons (=, EQ) are possible.

    If an outer join occurs after FROM, the join condition of every join expression must contain at least one comparison between columns on the left-hand and the right-hand side.

    In outer joins, all comparisons that contain columns as operands in the database table or the view dbtab_right on the right-hand side must be specified in the corresponding join condition. In the WHERE condition of the same SELECT command, these columns are not allowed as operands.

    Resulting set for inner join

    The inner join joins the columns of every selected line on the left- hand side with the columns of all lines on the right-hand side that jointly fulfil the join_cond condition. A line in the resulting set is created for every such line on the right-hand side. The content of the column on the left-hand side may be duplicated in this case. If none of the lines on the right-hand side fulfils the join_cond condition, no line is created in the resulting set.

    Resulting set for outer join

    The outer join basically creates the same resulting set as the inner join, with the difference that at least one line is created in the resulting set for every selected line on the left-hand side, even if no line on the right-hand side fulfils the join_cond condition. The columns on the right-hand side that do not fulfil the join_cond condition are filled with null values.

    Example

    Join the columns carrname, connid, fldate of the database tables scarr, spfli and sflight by means of two inner joins. A list is created of the flights from p_cityfr to p_cityto. Alternative names are used for every table.

    PARAMETERS: p_cityfr TYPE spfli-cityfrom,

    p_cityto TYPE spfli-cityto.

    DATA: BEGIN OF wa,

    fldate TYPE sflight-fldate,

    carrname TYPE scarr-carrname,

    connid TYPE spfli-connid,

    END OF wa.

    DATA itab LIKE SORTED TABLE OF wa

    WITH UNIQUE KEY fldate carrname connid.

    SELECT ccarrname pconnid f~fldate

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM ( ( scarr AS c

    INNER JOIN spfli AS p ON pcarrid = ccarrid

    AND p~cityfrom = p_cityfr

    AND p~cityto = p_cityto )

    INNER JOIN sflight AS f ON fcarrid = pcarrid

    AND fconnid = pconnid ).

    LOOP AT itab INTO wa.

    WRITE: / wa-fldate, wa-carrname, wa-connid.

    ENDLOOP.

    Example

    Join the columns carrid, carrname and connid of the database tables scarr and spfli using an outer join. The column connid is set to the null value for all flights that do not fly from p_cityfr. This null value is then converted to the appropriate initial value when it is transferred to the assigned data object. The LOOP returns all airlines that do not fly from p_cityfr.

    PARAMETERS p_cityfr TYPE spfli-cityfrom.

    DATA: BEGIN OF wa,

    carrid TYPE scarr-carrid,

    carrname TYPE scarr-carrname,

    connid TYPE spfli-connid,

    END OF wa,

    itab LIKE SORTED TABLE OF wa

    WITH NON-UNIQUE KEY carrid.

    SELECT scarrid scarrname p~connid

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM scarr AS s

    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid

    AND p~cityfrom = p_cityfr.

    LOOP AT itab INTO wa.

    IF wa-connid = '0000'.

    WRITE: / wa-carrid, wa-carrname.

    ENDIF.

    ENDLOOP.

    regards.

    sowjanya.b

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.