Db2 Training Class 008

Embed Size (px)

Citation preview

  • 7/24/2019 Db2 Training Class 008

    1/14

    www.mainframes-online-training.weebly.com Polsani Anil Kumar

    DB2 SQL Select

    DB2 Training class 08

    http://www.mainframes-online-training.weebly.com/http://www.mainframes-online-training.weebly.com/http://www.mainframes-online-training.weebly.com/
  • 7/24/2019 Db2 Training Class 008

    2/14

    A Closer Look at the SELECTStatement

  • 7/24/2019 Db2 Training Class 008

    3/14

    Seven Diferent Clauses

    These clauses are processed in the order shown

    1. The DISTI!T clause

    ". The #$%& clause' Identifies the Table ame

    (. The )*+$+ clause

    ,. The $%P /0 clause

    . The *A2I clause3. The %$D+$ /0 clause

    4. The #+T!* #I$ST clause

  • 7/24/2019 Db2 Training Class 008

    4/14

    DISTICT

    Suppose you wanted to retrie5e e5ery uni6ue 5alue 7no duplicates8 for

    a column in a table

    S+9+!T DISTI!T :ob #$%& employee

    If you wanted to change the name of the column in the result data set

    produced. 0ou could do so by e;ecuting a S+9+!T statement that

    loo

  • 7/24/2019 Db2 Training Class 008

    5/14

    The !"E#E Clause

    The )*+$+ clause is used to tell the D/" Database &anager how to

    select the rows based on a condition.

    Si; common types of )*+$+ clause

    $elationalPredicates' , =, =,

    /+T)++' BETWEEN 60000.00 AND 70000.00

    9IK+' LIKE S_ and 'S%'

    I' IN (W01,W02

    +=ISTS

    IS99

    +ach of these predicates can be used alone> or they can be combined

  • 7/24/2019 Db2 Training Class 008

    6/14

    !"#$ B& a)*+ -AIN! a)*+

    !"#$ B& a)*+

    The $%P /0 clause is used to tell the D/" Database &anager how

    to organi?e rows of data returned in the result data set produced in

    response to a 6uery.

    +=' S+9+!T )$KD+PT>&A=7+SA98 #$%& +&PTA/9+ $%P

    /0 )$KD+PT@

    T/+ -AIN! a)*+

    The *A2I clause is used to apply further selection criteria tocolumns referenced in a $%P /0 clause. This clause beha5es li e;cept that it refers to data that has already been

    grouped

    +;' S+9+!T )$KD+PT>&A=7+SA98 #$%& +&PTA/9+ $%P /0

    )$KD+PT *A2I &A=7+SA98 3BBB@

  • 7/24/2019 Db2 Training Class 008

    7/14

    #"DE" B& a)*+

    The %$D+$/0clause is used to tell the D/" Database &anager how

    to sort and order the rows that are to be returned in a result data set

    If the ascending order is

    used> and if the descending

    order is used.

    +;1' S+9+!T +&P%>+A&+>+SA9 #$%& +&PTA/9+

    %$D+$ /0 +&P%@+;"' S+9+!T +&P%>+A&+>+SA9 #$%& +&PTA/9+

    %$D+$ /0 +A&+>+SA9 D+S!@

  • 7/24/2019 Db2 Training Class 008

    8/14

    $ETC" $I#ST Clause

    The #+T!*#I$STclause is used to limit the number of rows returned

    to the result data set produced in response to a 6uery.

    )hen used> the #+T!* #I$ST clause is followed by a positi5e integer

    5alue and the words $%)S %90.

    +;'

    S+9+!T +&P%>+A&+>)$KD+PT #$%& +&PTA/9+ #+T!*

    #I$ST " $%)S %90@

  • 7/24/2019 Db2 Training Class 008

    9/14

    Built I $unction

    A built-in functionis a function that is supplied with D/" D/. The

    result of a function is a single 5alue deri5ed by applying the operation

    of the function to the arguments.

    S&

    &A=

    &I

    A2

    !%T78E !%T7DISTI!T !%98

    9+T*

    !%!AT

    S/ST$

  • 7/24/2019 Db2 Training Class 008

    10/14

    C%&T $unction

    The !%T function returns the number of rows or 5alues in a set of

    rows or 5alues. The argument of !%T78 is a set of rows.

    SELET #$NT( "#3 E3TAB4

    The argument of !%T7DISTI!T expression8 is a set of 5alues. Thefunction is applied to the set of 5alues deri5ed from the argument 5alues

    by the elimination of null 5alues and duplicate 5alues.

    SELET #$NT(ESAL "#3 E3TAB4

  • 7/24/2019 Db2 Training Class 008

    11/14

    'A( 'I ) S&'

    The &A=E&I column function returns the ma;imum 5alue in a set of

    5alues in a group. The function is applied to the set of 5alues deri5ed from

    the argument 5alues by the elimination of null 5alues.

    The specification of DISTI!T has no effect on the result and is not

    ad5ised.

    SELET 3A53IN(ESAL AS 3A5_SAL "#3 E3TAB4

    The S& function returns the sum of a set of numbers. The function is

    applied to the set of 5alues deri5ed from the argument 5alues by the

    elimination of null 5alues. If DISTI!T is specified> duplicate 5alues areeliminated.

    The result can be null. If the function is applied to the empty set> the result

    is a null 5alue. %therwise> the result is the sum of the 5alues in the set.

    SELET S$3(ESAL AS ESAL "#3 E3TAB4

  • 7/24/2019 Db2 Training Class 008

    12/14

    A*+ $unction

    The A2 function returns the a5erage of a set of numbers. The

    argument 5alues must be any built-in numeric data type and their sum

    must be within the range of the data type of the result.

    The result can be null. If set of 5alues is empty> %therwise> the result

    is the a5erage 5alue of the set.

    A2 disregards any 99S in any column in5ol5ed

    SELET A!(ESAL AS ESAL "#3 E3TAB4

  • 7/24/2019 Db2 Training Class 008

    13/14

    C%CAT S&BST# ) LE+T"

    The !%!AT function combines two arguments. The result of the

    function is a string that consists of the first argument string followed

    by the second.

    SELET #NAT(I"STN3E, LASTNA3E "#3 E3L#&EE

    The S/ST$ and S/ST$I functions return a substring of a

    string.

    SELET S$BST"(ENA3E, ,8 "#3 E3L#&EE4

    The 9+T* function returns the length of a 5alue

    SELET LEN!T-(EADD" "#3 E3L#&EE4

  • 7/24/2019 Db2 Training Class 008

    14/14www mainframes online training weebly com Polsani Anil Kumar

    Thank ,ou

    http://www.mainframes-online-training.weebly.com/http://www.mainframes-online-training.weebly.com/http://www.mainframes-online-training.weebly.com/