sql queries for non sap applications

Upload: sunilkumara

Post on 25-Feb-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 SQL Queries for Non SAP Applications

    1/65

    SQL Queries for Non SAP Applications:

    SQL SELECT Statement________________________________________This chapter will

    explain the SELECT and the SELECT *

    statements.________________________________________The SQL SELECT Statement

    The SELECT statement is used to select data from a database.

    The result is stored in a result table, called the result-set.

    SQL SELECT Sntax

    SELECT column_name!s"

    #$%& table_name

    and

    SELECT * #$%& table_name

    'ote( SQL is not case sensiti)e. SELECT is the same as

    select.________________________________________n SQL SELECT Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select the content of the columns named +Last'ame+ and +#irst'ame+ from

    the table abo)e.6e use the followin3 SELECT statement(

    SELECT Last'ame,#irst'ame #$%& ersons

    The result-set will loo7 li7e this(

    Last'ame #irst'ame

    /ansen %la

    S)endson To)e

    ettersen 5ari

    SELECT * Example

  • 7/25/2019 SQL Queries for Non SAP Applications

    2/65

    'ow we want to select all the columns from the +ersons+ table.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    Tip( The asteris7 !*" is a 8uic7 wa of selectin3 all columns9

    The result-set will loo7 li7e this(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    SQL SELECT :ST'CT Statement________________________________________This

    chapter will explain the SELECT :ST'CT

    statement.________________________________________

    The SQL SELECT :ST'CT Statement

    n a table, some of the columns ma contain duplicate )alues. This is not a problem, howe)er,

    sometimes ou will want to list onl the different !distinct" )alues in a table.

    The :ST'CT 7eword can be used to return onl distinct !different" )alues.

    SQL SELECT :ST'CT Sntax

    SELECT :ST'CT column_name!s"

    #$%& table_name

    ________________________________________

    SELECT :ST'CT Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

  • 7/25/2019 SQL Queries for Non SAP Applications

    3/65

    'ow we want to select onl the distinct )alues from the column named +Cit+ from the table

    abo)e.

    6e use the followin3 SELECT statement(

    SELECT :ST'CT Cit #$%& ersons

    The result-set will loo7 li7e this(

    Cit

    Sandnes

    Sta)an3er

    SQL 6/E$E Clause

    The 6/E$E clause is used to filter

    records.________________________________________

    The 6/E$E Clause

    The 6/E$E clause is used to extract onl those records that fulfill a specified criterion.

    SQL 6/E$E Sntax

    SELECT column_name!s"

    #$%& table_name

    6/E$E column_name operator )alue

    ________________________________________

    6/E$E Clause Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select onl the persons li)in3 in the cit +Sandnes+ from the table abo)e.

    6e use the followin3 SELECT statement(

  • 7/25/2019 SQL Queries for Non SAP Applications

    4/65

    SELECT * #$%& ersons

    6/E$E Cit;

  • 7/25/2019 SQL Queries for Non SAP Applications

    5/65

    %perator :escription

    ; E8ual

    AB 'ot e8ual

    B reater than

    A Less than

    B; reater than or e8ual

    A; Less than or e8ual

    2ET6EE' 2etween an inclusi)e ran3e

    L5E Search for a pattern

    ' To specif multiple possible )alues for a column

    'ote( n some )ersions of SQL the AB operator ma be written as 9;

    SQL ': D %$ %perators________________________________________

    The ': D %$ operators are used to filter records based on more than one condition.

    ________________________________________

    The ': D %$ %perators

    The ': operator displas a record if both the first condition and the second condition is

    true.

    The %$ operator displas a record if either the first condition or the second condition is true.

    ________________________________________

    ': %perator Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

  • 7/25/2019 SQL Queries for Non SAP Applications

    6/65

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select onl the persons with the first name e8ual to +To)e+ ': the last

    name e8ual to +S)endson+(

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    6/E$E #irst'ame;

  • 7/25/2019 SQL Queries for Non SAP Applications

    7/65

    =ou can also combine ': and %$ !use parenthesis to form complex expressions".

    'ow we want to select onl the persons with the last name e8ual to +S)endson+ ': the first

    name e8ual to +To)e+ %$ to +%la+(

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons 6/E$E

    Last'ame;

  • 7/25/2019 SQL Queries for Non SAP Applications

    8/65

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Tom Gin3)n 14 Sta)an3er

    'ow we want to select all the persons from the table abo)e, howe)er, we want to sort the

    persons b their last name.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    %$:E$ 2= Last'ame

    The result-set will loo7 li7e this(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    F 'ilsen Tom Gin3)n 14 Sta)an3er

    4 ettersen 5ari Stor3t 10 Sta)an3er

    1 S)endson To)e 2or3)n 14 Sandnes

    %$:E$ 2= :ESC Example

    'ow we want to select all the persons from the table abo)e, howe)er, we want to sort the

    persons descendin3 b their last name.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    %$:E$ 2= Last'ame :ESC

  • 7/25/2019 SQL Queries for Non SAP Applications

    9/65

    The result-set will loo7 li7e this(

    _d Last'ame #irst'ame ddress Cit

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Tom Gin3)n 14 Sta)an3er

    /ansen %la Timotei)n 0 Sandnes

    SQL 'SE$T 'T% Statement________________________________________

    The 'SE$T 'T% statement is used to insert new records in a table.

    ________________________________________

    The 'SE$T 'T% Statement

    The 'SE$T 'T% statement is used to insert a new row in a table.

    SQL 'SE$T 'T% Sntax

    t is possible to write the 'SE$T 'T% statement in two forms.

    The first form doesn

  • 7/25/2019 SQL Queries for Non SAP Applications

    10/65

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to insert a new row in the +ersons+ table.

    6e use the followin3 SQL statement(

    'SE$T 'T% ersons

    GLHES !F,

  • 7/25/2019 SQL Queries for Non SAP Applications

    11/65

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Iohan 2a77en 1 Sta)an3er

    @ TJessem Ia7ob

    SQL H:TE Statement________________________________________

    The H:TE statement is used to update records in a table.

    ________________________________________

    The H:TE Statement

    The H:TE statement is used to update existin3 records in a table.

    SQL H:TE Sntax

    H:TE table_name

    SET column;)alue, column1;)alue1,...

    6/E$E some_column;some_)alue

    'ote( 'otice the 6/E$E clause in the H:TE sntax. The 6/E$E clause specifies which

    record or records that should be updated. f ou omit the 6/E$E clause, all records will be

    updated9

    SQL H:TE Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Iohan 2a77en 1 Sta)an3er

    @ TJessem Ia7ob

  • 7/25/2019 SQL Queries for Non SAP Applications

    12/65

    'ow we want to update the person +TJessem, Ia7ob+ in the +ersons+ table.

    6e use the followin3 SQL statement(

    H:TE ersons

    SET ddress;

  • 7/25/2019 SQL Queries for Non SAP Applications

    13/65

    SQL :ELETE Statement________________________________________

    The :ELETE statement is used to delete records in a table.

    ________________________________________

    The :ELETE Statement

    The :ELETE statement is used to delete rows in a table.

    SQL :ELETE Sntax

    :ELETE #$%& table_name

    6/E$E some_column;some_)alue

    'ote( 'otice the 6/E$E clause in the :ELETE sntax. The 6/E$E clause specifies which

    record or records that should be deleted. f ou omit the 6/E$E clause, all records will be

    deleted9

    ________________________________________

    SQL :ELETE Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Iohan 2a77en 1 Sta)an3er

    @ TJessem Ia7ob 'issestien ?K Sandnes

    'ow we want to delete the person +TJessem, Ia7ob+ in the +ersons+ table.

    6e use the followin3 SQL statement(

    :ELETE #$%& ersons

    6/E$E Last'ame;

  • 7/25/2019 SQL Queries for Non SAP Applications

    14/65

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Iohan 2a77en 1 Sta)an3er

    ________________________________________

    :elete ll $ows

    t is possible to delete all rows in a table without deletin3 the table. This means that the table

    structure, attributes, and indexes will be intact(

    :ELETE #$%& table_name

    or

    :ELETE * #$%& table_name

    'ote( 2e )er careful when deletin3 records. =ou cannot undo this statement9

    SQL T% Clause________________________________________

    The T% Clause

    The T% clause is used to specif the number of records to return.

    The T% clause can be )er useful on lar3e tables with thousands of records. $eturnin3 a

    lar3e number of records can impact on performance.

    'ote( 'ot all database sstems support the T% clause.

    SQL Ser)er Sntax

    SELECT T% numberpercent column_name!s"

    #$%& table_name

    ________________________________________

  • 7/25/2019 SQL Queries for Non SAP Applications

    15/65

    SQL SELECT T% E8ui)alent in &SQL and %racle

    &SQL Sntax

    SELECT column_name!s"

    #$%& table_name

    L&T number

    Example

    SELECT *

    #$%& ersons

    L&T @

    %racle Sntax

    SELECT column_name!s"

    #$%& table_name

    6/E$E $%6'H& A; number

    Example

    SELECT *

    #$%& ersons

    6/E$E $%6'H& A;@

    SQL T% Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Tom Gin3)n 14 Sta)an3er

    'ow we want to select onl the two first records in the table abo)e.

    6e use the followin3 SELECT statement(

  • 7/25/2019 SQL Queries for Non SAP Applications

    16/65

    SELECT T% 1 * #$%& ersons

    The result-set will loo7 li7e this(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    ________________________________________

    SQL T% E$CE'T Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    F 'ilsen Tom Gin3)n 14 Sta)an3er

    'ow we want to select onl @0 of the records in the table abo)e.

    6e use the followin3 SELECT statement(

    SELECT T% @0 E$CE'T * #$%& ersons

    The result-set will loo7 li7e this(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    SQL L5E %perator________________________________________

    The L5E operator is used in a 6/E$E clause to search for a specified pattern in a

    column.________________________________________

    The L5E %perator

    The L5E operator is used to search for a specified pattern in a column.

  • 7/25/2019 SQL Queries for Non SAP Applications

    17/65

    SQL L5E Sntax

    SELECT column_name!s"

    #$%& table_name

    6/E$E column_name L5E pattern

    ________________________________________

    Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select the persons li)in3 in a cit that starts with +s+ from the table abo)e.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    6/E$E Cit L5E

  • 7/25/2019 SQL Queries for Non SAP Applications

    18/65

    6/E$E Cit L5E

  • 7/25/2019 SQL Queries for Non SAP Applications

    19/65

    SELECT column_name!s"

    #$%& table_name

    6/E$E column_name ' !)alue,)alue1,..."

    ________________________________________

    ' %perator Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select the persons with a last name e8ual to +/ansen+ or +ettersen+ from

    the table abo)e.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    6/E$E Last'ame ' !

  • 7/25/2019 SQL Queries for Non SAP Applications

    20/65

    SELECT column_name!s"

    #$%& table_name

    6/E$E column_name

    2ET6EE' )alue ': )alue1

    ________________________________________

    2ET6EE' %perator Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ow we want to select the persons with a last name alphabeticall between +/ansen+ and

    +ettersen+ from the table abo)e.

    6e use the followin3 SELECT statement(

    SELECT * #$%& ersons

    6/E$E Last'ame

    2ET6EE'

  • 7/25/2019 SQL Queries for Non SAP Applications

    21/65

    Therefore( Chec7 how our database treats the 2ET6EE' operator.

    ________________________________________

    Example 1

    To displa the persons outside the ran3e in the pre)ious example, use '%T 2ET6EE'(

    SELECT * #$%& ersons

    6/E$E Last'ame

    '%T 2ET6EE'

  • 7/25/2019 SQL Queries for Non SAP Applications

    22/65

    ________________________________________

    lias Example

    ssume we ha)e a table called +ersons+ and another table called +roduct_%rders+. 6e will

    3i)e the table aliases of +p+ and +po+ respecti)el.

    'ow we want to list all the orders that +%la /ansen+ is responsible for.

    6e use the followin3 SELECT statement(

    SELECT po.%rder:, p.Last'ame, p.#irst'ame

    #$%& ersons S p,

    roduct_%rders S po

    6/E$E p.Last'ame;

  • 7/25/2019 SQL Queries for Non SAP Applications

    23/65

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    'ote that the +_d+ column is the primar 7e in the +ersons+ table. This means that no two

    rows can ha)e the same _d. The _d distin3uishes two persons e)en if the ha)e the same

    name.

    'ext, we ha)e the +%rders+ table(

    %_d %rder'o _d

    KKN>@ 4

    1 FF?KN 4

    4 11F@?

    F 1F@?1

    @ 4FK?F @

    'ote that the +%_d+ column is the primar 7e in the +%rders+ table and that the +_d+column refers to the persons in the +ersons+ table without usin3 their names.

    'otice that the relationship between the two tables abo)e is the +_d+ column.

    ________________________________________

    :ifferent SQL I%'s

    2efore we continue with examples, we will list the tpes of I%' ou can use, and the

    differences between them.

    O I%'( $eturn rows when there is at least one match in both tables

    O LE#T I%'( $eturn all rows from the left table, e)en if there are no matches in the

    ri3ht table

    O $/T I%'( $eturn all rows from the ri3ht table, e)en if there are no matches in the

    left table

  • 7/25/2019 SQL Queries for Non SAP Applications

    24/65

    O #HLL I%'( $eturn rows when there is a match in one of the tables

    SQL ''E$ I%' 5eword

    SQL ''E$ I%' 5eword

    The ''E$ I%' 7eword return rows when there is at least one match in both tables.

    SQL ''E$ I%' Sntax

    SELECT column_name!s"

    #$%& table_name

    ''E$ I%' table_name1

    %' table_name.column_name;table_name1.column_name

    S( ''E$ I%' is the same as I%'.

    SQL ''E$ I%' Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    The +%rders+ table(

    %_d %rder'o _d

    KKN>@ 4

    1 FF?KN 4

    4 11F@?

    F 1F@?1

  • 7/25/2019 SQL Queries for Non SAP Applications

    25/65

    @ 4FK?F @

    'ow we want to list all the persons with an orders.

    6e use the followin3 SELECT statement(

    SELECT ersons.Last'ame, ersons.#irst'ame, %rders.%rder'o

    #$%& ersons

    ''E$ I%' %rders

    %' ersons._d;%rders._d

    %$:E$ 2= ersons.Last'ame

    The result-set will loo7 li7e this(

    Last'ame #irst'ame %rder'o

    /ansen %la 11F@?

    /ansen %la 1F@?1

    ettersen 5ari KKN>@

    ettersen 5ari FF?KN

    The ''E$ I%' 7eword return rows when there is at least one match in both tables. fthere are rows in +ersons+ that do not ha)e matches in +%rders+, those rows will '%T be

    listed.

    SQL LE#T I%' 5eword________________________________________

    SQL LE#T I%' 5eword

    The LE#T I%' 7eword returns all rows from the left table !table_name", e)en if there are

    no matches in the ri3ht table !table_name1".

    SQL LE#T I%' Sntax

    SELECT column_name!s"

    #$%& table_name

    LE#T I%' table_name1

    %' table_name.column_name;table_name1.column_name

    S( n some databases LE#T I%' is called LE#T %HTE$ I%'.

    SQL LE#T I%' Example

  • 7/25/2019 SQL Queries for Non SAP Applications

    26/65

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    The +%rders+ table(

    %_d %rder'o _d

    KKN>@ 4

    1 FF?KN 4

    4 11F@?

    F 1F@?1

    @ 4FK?F @

    'ow we want to list all the persons and their orders - if an, from the tables abo)e.

    6e use the followin3 SELECT statement(

    SELECT ersons.Last'ame, ersons.#irst'ame, %rders.%rder'o

    #$%& ersons

    LE#T I%' %rders

    %' ersons._d;%rders._d

    %$:E$ 2= ersons.Last'ame

  • 7/25/2019 SQL Queries for Non SAP Applications

    27/65

    The result-set will loo7 li7e this(

    Last'ame #irst'ame %rder'o

    /ansen %la 11F@?

    /ansen %la 1F@?1

    ettersen 5ari KKN>@

    ettersen 5ari FF?KN

    S)endson To)e

    The LE#T I%' 7eword returns all the rows from the left table !ersons", e)en if there are

    no matches in the ri3ht table !%rders".

    SQL $/T I%' 5eword________________________________________

    SQL $/T I%' 5eword

    The $/T I%' 7eword returns all the rows from the ri3ht table !table_name1", e)en if

    there are no matches in the left table !table_name".

    SQL $/T I%' Sntax

    SELECT column_name!s"

    #$%& table_name

    $/T I%' table_name1

    %' table_name.column_name;table_name1.column_name

    S( n some databases $/T I%' is called $/T %HTE$ I%'.

    ________________________________________

    SQL $/T I%' Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    The +%rders+ table(

    %_d %rder'o _d

  • 7/25/2019 SQL Queries for Non SAP Applications

    28/65

    KKN>@ 4

    1 FF?KN 4

    4 11F@?

    F 1F@?1

    @ 4FK?F @

    'ow we want to list all the orders with containin3 persons - if an, from the tables abo)e.

    6e use the followin3 SELECT statement(

    SELECT ersons.Last'ame, ersons.#irst'ame, %rders.%rder'o

    #$%& ersons

    $/T I%' %rders

    %' ersons._d;%rders._d

    %$:E$ 2= ersons.Last'ame

    The result-set will loo7 li7e this(

    Last'ame #irst'ame %rder'o

    /ansen %la 11F@?

    /ansen %la 1F@?1

    ettersen 5ari KKN>@

    ettersen 5ari FF?KN

    4FK?F

    The $/T I%' 7eword returns all the rows from the ri3ht table !%rders", e)en if there are

    no matches in the left table !ersons".

    SQL #HLL I%' 5eword

    SQL #HLL I%' 5eword

    The #HLL I%' 7eword return rows when there is a match in one of the tables.

    SQL #HLL I%' Sntax

  • 7/25/2019 SQL Queries for Non SAP Applications

    29/65

    SELECT column_name!s"

    #$%& table_name

    #HLL I%' table_name1

    %' table_name.column_name;table_name1.column_name

    SQL #HLL I%' Example

    The +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Timotei)n 0 Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Stor3t 10 Sta)an3er

    The +%rders+ table(

    %_d %rder'o _d

    KKN>@ 4

    1 FF?KN 4

    4 11F@?

    F 1F@?1

    @ 4FK?F @

    'ow we want to list all the persons and their orders, and all the orders with their persons.

    6e use the followin3 SELECT statement(

    SELECT ersons.Last'ame, ersons.#irst'ame, %rders.%rder'o

    #$%& ersons

    #HLL I%' %rders

    %' ersons._d;%rders._d

    %$:E$ 2= ersons.Last'ame

    The result-set will loo7 li7e this(

    Last'ame #irst'ame %rder'o

  • 7/25/2019 SQL Queries for Non SAP Applications

    30/65

    /ansen %la 11F@?

    /ansen %la 1F@?1

    ettersen 5ari KKN>@

    ettersen 5ari FF?KN

    S)endson To)e

    4FK?F

    The #HLL I%' 7eword returns all the rows from the left table !ersons", and all the rows

    from the ri3ht table !%rders". f there are rows in +ersons+ that do not ha)e matches in

    +%rders+, or if there are rows in +%rders+ that do not ha)e matches in +ersons+, those rows

    will be listed as well.

    SQL H'%' %perator________________________________________

    The SQL H'%' operator combines two or more SELECT

    statements.________________________________________

    The SQL H'%' %perator

    The H'%' operator is used to combine the result-set of two or more SELECT statements.

    'otice that each SELECT statement within the H'%' must ha)e the same number of

    columns. The columns must also ha)e similar data tpes. lso, the columns in each SELECTstatement must be in the same order.

    SQL H'%' Sntax

    SELECT column_name!s" #$%& table_name

    H'%'

    SELECT column_name!s" #$%& table_name1

    'ote( The H'%' operator selects onl distinct )alues b default. To allow duplicate )alues,use H'%' LL.

    SQL H'%' LL Sntax

    SELECT column_name!s" #$%& table_name

    H'%' LL

    SELECT column_name!s" #$%& table_name1

    S( The column names in the result-set of a H'%' are alwas e8ual to the column names in

    the first SELECT statement in the H'%'.

  • 7/25/2019 SQL Queries for Non SAP Applications

    31/65

    SQL H'%' Example

    Loo7 at the followin3 tables(

    +Emploees_'orwa+(

    E_: E_'ame

    0 /ansen, %la

    01 S)endson, To)e

    04 S)endson, Stephen

    0F ettersen, 5ari

    +Emploees_HS+(

    E_: E_'ame

    0 Turner, Sall

    01 5ent, Clar7

    04 S)endson, Stephen

    0F Scott, Stephen

    'ow we want to list all the different emploees in 'orwa and HS.

    6e use the followin3 SELECT statement(

    SELECT E_'ame #$%& Emploees_'orwa

    H'%'

    SELECT E_'ame #$%& Emploees_HS

    The result-set will loo7 li7e this(

    E_'ame

    /ansen, %la

    S)endson, To)e

    S)endson, Stephen

    ettersen, 5ari

    Turner, Sall

    5ent, Clar7

  • 7/25/2019 SQL Queries for Non SAP Applications

    32/65

    Scott, Stephen

    'ote( This command cannot be used to list all emploees in 'orwa and HS. n the

    example abo)e we ha)e two emploees with e8ual names, and onl one of them will be

    listed. The H'%' command selects onl distinct )alues.

    SQL H'%' LL Example

    'ow we want to list all emploees in 'orwa and HS(

    SELECT E_'ame #$%& Emploees_'orwa

    H'%' LL

    SELECT E_'ame #$%& Emploees_HS

    $esult

    E_'ame

    /ansen, %la

    S)endson, To)e

    S)endson, Stephen

    ettersen, 5ari

    Turner, Sall

    5ent, Clar7

    S)endson, Stephen

    Scott, Stephen

    SQL 'HLL Galues

    ________________________________________

    'HLL )alues represent missin3 un7nown data.

    2 default, a table column can hold 'HLL )alues.

    This chapter will explain the S 'HLL and S '%T 'HLL operators.

    ________________________________________

    SQL 'HLL Galues

  • 7/25/2019 SQL Queries for Non SAP Applications

    33/65

    f a column in a table is optional, we can insert a new record or update an existin3 record

    without addin3 a )alue to this column. This means that the field will be sa)ed with a 'HLL

    )alue.

    'HLL )alues are treated differentl from other )alues.

    'HLL is used as a placeholder for un7nown or inapplicable )alues.

    'ote( t is not possible to compare 'HLL and 0M the are not e8ui)alent.

    ________________________________________

    SQL 6or7in3 with 'HLL Galues

    Loo7 at the followin3 +ersons+ table(

    _d Last'ame #irst'ame ddress Cit

    /ansen %la Sandnes

    1 S)endson To)e 2or3)n 14 Sandnes

    4 ettersen 5ari Sta)an3er

    Suppose that the +ddress+ column in the +ersons+ table is optional. This means that if we

    insert a record with no )alue for the +ddress+ column, the +ddress+ column will be sa)ed

    with a 'HLL )alue.

    /ow can we test for 'HLL )aluesP

    t is not possible to test for 'HLL )alues with comparison operators, such as ;, A, or AB.

    6e will ha)e to use the S 'HLL and S '%T 'HLL operators instead.

    SQL S 'HLL

    /ow do we select onl the records with 'HLL )alues in the +ddress+ columnP

    6e will ha)e to use the S 'HLL operator(

    SELECT Last'ame,#irst'ame,ddress #$%& ersons

    6/E$E ddress S 'HLL

    The result-set will loo7 li7e this(

    Last'ame #irst'ame ddress

    /ansen %la

    ettersen 5ari

  • 7/25/2019 SQL Queries for Non SAP Applications

    34/65

    Tip( lwas use S 'HLL to loo7 for 'HLL )alues.

    SQL S '%T 'HLL

    /ow do we select onl the records with no 'HLL )alues in the +ddress+ columnP

    6e will ha)e to use the S '%T 'HLL operator(

    SELECT Last'ame,#irst'ame,ddress #$%& ersons

    6/E$E ddress S '%T 'HLL

    The result-set will loo7 li7e this(

    Last'ame #irst'ame ddress

    S)endson To)e 2or3)n 14

    n the next chapter we will loo7 at the S'HLL!", 'GL!", #'HLL!" and C%LESCE!"

    functions.

    2 default, a table column can hold 'HLL

    )alues.________________________________________

    SQL '%T 'HLL Constraint

    The '%T 'HLL constraint enforces a column to '%T accept 'HLL )alues.

    The '%T 'HLL constraint enforces a field to alwas contain a )alue. This means that ou

    cannot insert a new record, or update a record without addin3 a )alue to this field.

    The followin3 SQL enforces the +_d+ column and the +Last'ame+ column to not accept

    'HLL )alues(

    C$ETE T2LE ersons

    !

    _d int '%T 'HLL,

    Last'ame )archar!1@@" '%T 'HLL,

    #irst'ame )archar!1@@",

    ddress )archar!1@@",

    Cit )archar!1@@"

    SQL :ate #unctions________________________________________

    SQL :ates

  • 7/25/2019 SQL Queries for Non SAP Applications

    35/65

    The most difficult part when wor7in3 with dates is to be sure that the format of the date ou

    are trin3 to insert, matches the format of the date column in the database.

    s lon3 as our data contains onl the date portion, our 8ueries will wor7 as expected.

    /owe)er, if a time portion is in)ol)ed, it 3ets complicated.

    2efore tal7in3 about the complications of 8uerin3 for dates, we will loo7 at the most

    important built-in functions for wor7in3 with dates.

    ________________________________________

    &SQL :ate #unctions

    The followin3 table lists the most important built-in date functions in &SQL(

    #unction :escription

    '%6!"

    $eturns the current date and time

    CH$:TE!"

    $eturns the current date

    CH$T&E!"

    $eturns the current time

    :TE!"

    Extracts the date part of a date or datetime expression

    ERT$CT!"

    $eturns a sin3le part of a datetime

    :TE_::!"

    dds a specified time inter)al to a date

    :TE_SH2!"

    Subtracts a specified time inter)al from a date

    :TE:##!"

    $eturns the number of das between two dates

    :TE_#%$&T!"

  • 7/25/2019 SQL Queries for Non SAP Applications

    36/65

    :isplas datetime data in different formats

    SQL Ser)er :ate #unctions

    The followin3 table lists the most important built-in date functions in SQL Ser)er(

    #unction :escription

    ET:TE!"

    $eturns the current date and time

    :TE$T!"

    $eturns a sin3le part of a datetime

    :TE::!"

    dds or subtracts a specified time inter)al from a date

    :TE:##!"

    $eturns the time between two dates

    C%'GE$T!"

    :isplas datetime data in different formats

    ________________________________________

    SQL :ate :ata Tpes

    &SQL comes with the followin3 data tpes for storin3 a date or a datetime )alue in the

    database(

    O :TE - format ====-&&-::

    O :TET&E - format( ====-&&-:: //(&&(SS

    O T&EST& - format( ====-&&-:: //(&&(SS

    O =E$ - format ==== or ==

    SQL Ser)er comes with the followin3 data tpes for storin3 a date or a datetime )alue in the

    database(

    O :TE - format ====-&&-::

    O :TET&E - format( ====-&&-:: //(&&(SS

    O S&LL:TET&E - format( ====-&&-:: //(&&(SS

  • 7/25/2019 SQL Queries for Non SAP Applications

    37/65

    O T&EST& - format( a uni8ue number

    'ote( The date tpes are chosen for a column when ou create a new table in our database9

    #or an o)er)iew of all data tpes a)ailable, 3o to our complete :ata Tpes reference.

    ________________________________________

    SQL 6or7in3 with :ates

    =ou can compare two dates easil if there is no time component in)ol)ed9

    ssume we ha)e the followin3 +%rders+ table(

    %rderd roduct'ame %rder:ate

    eitost 100N--

    1 Camembert ierrot 100N--0>

    4 &oarella di io)anni 100N--

    F &ascarpone #abioli 100N-0-1>

    'ow we want to select the records with an %rder:ate of +100N--+ from the table abo)e.

    6e use the followin3 SELECT statement(

    SELECT * #$%& %rders 6/E$E %rder:ate;

  • 7/25/2019 SQL Queries for Non SAP Applications

    38/65

    f we use the same SELECT statement as abo)e(

    SELECT * #$%& %rders 6/E$E %rder:ate;

  • 7/25/2019 SQL Queries for Non SAP Applications

    39/65

    'SE$T 'T% %rders !roduct'ame" GLHES !

  • 7/25/2019 SQL Queries for Non SAP Applications

    40/65

    Example

    ssume we ha)e the followin3 +%rders+ table(

    %rderd roduct'ame %rder:ate

    Iarlsber3 Cheese 100N-- 4(14(FF.?@K

    The followin3 SELECT statement(

    SELECT roduct'ame, :TE!%rder:ate" S %rder:ate

    #$%& %rders

    6/E$E %rderd;

    will result in this(

    roduct'ame %rder:ate

    Iarlsber3 Cheese 100N--

    SQL Ser)er ET:TE!" #unction

    ________________________________________

    :efinition and Hsa3e

    The ET:TE!" function returns the current date and time from the SQL Ser)er.

    Sntax

    ET:TE!"

    Example

    The followin3 SELECT statement(

    SELECT ET:TE!" S Current:ateTime

    will result in somethin3 li7e this(

    Current:ateTime

    100N-- 1(F@(4F.1F4

    'ote( The time part abo)e 3oes all the wa to milliseconds.

    Example

  • 7/25/2019 SQL Queries for Non SAP Applications

    41/65

    The followin3 SQL creates an +%rders+ table with a datetime column !%rder:ate"(

    C$ETE T2LE %rders

    !%rderd int '%T 'HLL $&$= 5E=,

    roduct'ame )archar!@0" '%T 'HLL,

    %rder:ate datetime '%T 'HLL :E#HLT ET:TE!""

    'otice that the %rder:ate column specifies ET:TE!" as the default )alue. s a result,

    when ou insert a row into the table, the current date and time are automaticall inserted into

    the column.

    'ow we want to insert a record into the +%rders+ table(

    'SE$T 'T% %rders !roduct'ame" GLHES !

  • 7/25/2019 SQL Queries for Non SAP Applications

    42/65

    Galue

    !centur " Galue

    !centur "nput%utput Standard

    - 0 or 00 mon dd hh(mi& !or &" :efault

    0 mmdd HS

    1 01 .mm.dd 'S

    4 04 ddmm 2ritish#rench

    F 0F dd.mm. erman

    @ 0@ dd-mm- talian

    ? 0? dd mon

    K 0K &on dd,

    N 0N hh(mm(ss

    - > or 0> mon dd hh(mi(ss(mmm& !or &" :efaultmillisec

    0 0 mm-dd- HS

    mmdd Iapan

    1 1 mmdd S%

    - 4 or 4 dd mon hh(mi(ss(mmm !1Fh"

    F F hh(mi(ss(mmm !1Fh"

    - 10 or 10 -mm-dd hh(mi(ss !1Fh"

    - 1 or 1 -mm-dd hh(mi(ss.mmm !1Fh"

    - 1? -mm-ddThh(mi(ss.mmm !no spaces" S%N?0

    - 40 dd mon hh(mi(ss(mmm& /iJiri

    - 4 ddmm hh(mi(ss(mmm&/iJiri

    ________________________________________

    Example

  • 7/25/2019 SQL Queries for Non SAP Applications

    43/65

    The followin3 script uses the C%'GE$T!" function to displa different formats. 6e will use

    the ET:TE!" function to 3et the current datetime(

    C%'GE$T!G$C/$!>",ET:TE!""

    C%'GE$T!G$C/$!0",ET:TE!",0"

    C%'GE$T!G$C/$!0",ET:TE!",0"

    C%'GE$T!G$C/$!",ET:TE!",?"

    C%'GE$T!G$C/$!",ET:TE!",0?"

    C%'GE$T!G$C/$!1F",ET:TE!",4"

    The result would loo7 somethin3 li7e this(

    'o) 0F 10 (F@ &

    -0F-

    -0F-10

    0F 'o)

    0F 'o) 10

    0F 'o) 10 (F@(4F(1F4

    SQL #unctions________________________________________

    SQL has man built-in functions for performin3 calculations on data.

    ________________________________________

    SQL 33re3ate #unctions

    SQL a33re3ate functions return a sin3le )alue, calculated from )alues in a column.

    Hseful a33re3ate functions(

    O G!" - $eturns the a)era3e )alue

    O C%H'T!" - $eturns the number of rows

    O #$ST!" - $eturns the first )alue

    O LST!" - $eturns the last )alue

  • 7/25/2019 SQL Queries for Non SAP Applications

    44/65

    O &R!" - $eturns the lar3est )alue

    O &'!" - $eturns the smallest )alue

    O SH&!" - $eturns the sum

    ________________________________________

    SQL Scalar functions

    SQL scalar functions return a sin3le )alue, based on the input )alue.

    Hseful scalar functions(

    O HCSE!" - Con)erts a field to upper case

    O LCSE!" - Con)erts a field to lower case

    O &:!" - Extract characters from a text field

    O LE'!" - $eturns the len3th of a text field

    O $%H':!" - $ounds a numeric field to the number of decimals specified

    O '%6!" - $eturns the current sstem date and time

    O #%$&T!" - #ormats how a field is to be displaed

    Tip( The a33re3ate functions and the scalar functions will be explained in details in the nextchapters.

    SQL G!" #unction________________________________________

    The G!" #unction

    The G!" function returns the a)era3e )alue of a numeric column.

    SQL G!" Sntax

    SELECT G!column_name" #$%& table_name

    SQL G!" Example

    6e ha)e the followin3 +%rders+ table(

  • 7/25/2019 SQL Queries for Non SAP Applications

    45/65

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find the a)era3e )alue of the +%rderrice+ fields.

    6e use the followin3 SQL statement(

    SELECT G!%rderrice" S %rder)era3e #$%& %rders

    The result-set will loo7 li7e this(

    %rder)era3e

    >@0

    'ow we want to find the customers that ha)e an %rderrice )alue hi3her than the a)era3e

    %rderrice )alue.

    6e use the followin3 SQL statement(

    SELECT Customer #$%& %rders

    6/E$E %rderriceB!SELECT G!%rderrice" #$%& %rders"

    The result-set will loo7 li7e this(

    Customer

    /ansen

    'ilsen

    Iensen

    SQL C%H'T!" #unction________________________________________

    The C%H'T!" function returns the number of rows that matches a specified criteria.

  • 7/25/2019 SQL Queries for Non SAP Applications

    46/65

    ________________________________________

    SQL C%H'T!column_name" Sntax

    The C%H'T!column_name" function returns the number of )alues !'HLL )alues will not be

    counted" of the specified column(

    SELECT C%H'T!column_name" #$%& table_name

    SQL C%H'T!*" Sntax

    The C%H'T!*" function returns the number of records in a table(

    SELECT C%H'T!*" #$%& table_name

    SQL C%H'T!:ST'CT column_name" Sntax

    The C%H'T!:ST'CT column_name" function returns the number of distinct )alues of the

    specified column(

    SELECT C%H'T!:ST'CT column_name" #$%& table_name

    'ote( C%H'T!:ST'CT" wor7s with %$CLE and &icrosoft SQL Ser)er, but not with

    &icrosoft ccess.

    ________________________________________

    SQL C%H'T!column_name" Example

    6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to count the number of orders from +Customer 'ilsen+.

    6e use the followin3 SQL statement(

    SELECT C%H'T!Customer" S Customer'ilsen #$%& %rders

  • 7/25/2019 SQL Queries for Non SAP Applications

    47/65

    6/E$E Customer;

  • 7/25/2019 SQL Queries for Non SAP Applications

    48/65

    SQL &R!" Example

    6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find the lar3est )alue of the +%rderrice+ column.

    6e use the followin3 SQL statement(

    SELECT &R!%rderrice" S Lar3est%rderrice #$%& %rders

    The result-set will loo7 li7e this(

    Lar3est%rderrice

    1000

    SQL &'!" #unction________________________________________

    The &'!" #unction

    The &'!" function returns the smallest )alue of the selected column.

    SQL &'!" Sntax

    SELECT &'!column_name" #$%& table_name

    SQL &'!" Example

    6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

  • 7/25/2019 SQL Queries for Non SAP Applications

    49/65

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find the smallest )alue of the +%rderrice+ column.

    6e use the followin3 SQL statement(

    SELECT &'!%rderrice" S Smallest%rderrice #$%& %rders

    The result-set will loo7 li7e this(

    Smallest%rderrice

    00

    SQL SH&!" #unction________________________________________

    The SH&!" #unction

    The SH&!" function returns the total sum of a numeric column.

    SQL SH&!" Sntax

    SELECT SH&!column_name" #$%& table_name

    SQL SH&!" Example

    6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find the sum of all +%rderrice+ fields+.

  • 7/25/2019 SQL Queries for Non SAP Applications

    50/65

    6e use the followin3 SQL statement(

    SELECT SH&!%rderrice" S %rderTotal #$%& %rders

    The result-set will loo7 li7e this(

    %rderTotal

    @K00

    SQL $%H 2= Statement________________________________________

    33re3ate functions often need an added $%H 2= statement.

    The $%H 2= Statement

    The $%H 2= statement is used in conJunction with the a33re3ate functions to 3roup the

    result-set b one or more columns.

    SQL $%H 2= Sntax

    SELECT column_name, a33re3ate_function!column_name"

    #$%& table_name

    6/E$E column_name operator )alue

    $%H 2= column_name

    SQL $%H 2= Example(- 6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find the total sum !total order" of each customer.

    6e will ha)e to use the $%H 2= statement to 3roup the customers.

    6e use the followin3 SQL statement(

  • 7/25/2019 SQL Queries for Non SAP Applications

    51/65

    SELECT Customer,SH&!%rderrice" #$%& %rders

    $%H 2= Customer

    The result-set will loo7 li7e this(

    Customer SH&!%rderrice"

    /ansen 1000

    'ilsen K00

    Iensen 1000

    'ice9 sn

  • 7/25/2019 SQL Queries for Non SAP Applications

    52/65

    SQL /G' Clause

    The /G' Clause

    The /G' clause was added to SQL because the 6/E$E 7eword could not be used

    with a33re3ate functions.

    SQL /G' Sntax

    SELECT column_name, a33re3ate_function!column_name"

    #$%& table_name

    6/E$E column_name operator )alue

    $%H 2= column_name

    /G' a33re3ate_function!column_name" operator )alue

    SQL /G' Example

    6e ha)e the followin3 +%rders+ table(

    %_d %rder:ate %rderrice Customer

    100N1 000 /ansen

    1 100N014 ?00 'ilsen

    4 100N0>01 K00 /ansen

    F 100N0>04 400 /ansen

    @ 100N0N40 1000 Iensen

    ? 100N00F 00 'ilsen

    'ow we want to find if an of the customers ha)e a total order of less than 1000.

    6e use the followin3 SQL statement(

    SELECT Customer,SH&!%rderrice" #$%& %rders

    $%H 2= Customer

    /G' SH&!%rderrice"A1000

    The result-set will loo7 li7e this(

    Customer SH&!%rderrice"

    'ilsen K00

  • 7/25/2019 SQL Queries for Non SAP Applications

    53/65

    'ow we want to find if the customers +/ansen+ or +Iensen+ ha)e a total order of more than

    @00.

    6e add an ordinar 6/E$E clause to the SQL statement(

    SELECT Customer,SH&!%rderrice" #$%& %rders

    6/E$E Customer;

  • 7/25/2019 SQL Queries for Non SAP Applications

    54/65

    4 or3onola 000 3 @.?K

    'ow we want to displa the product name and the price rounded to the nearest inte3er.

    6e use the followin3 SELECT statement(

    SELECT roduct'ame, $%H':!Hnitrice,0" as Hnitrice #$%& roducts

    The result-set will loo7 li7e this(

    roduct'ame Hnitrice

    Iarlsber3 0

    &ascarpone 44

    or3onola ?

    CSE Statement

    E)aluates a list of conditions and returns one of multiple possible result expressions.

    The CSE expression has two formats(

    O The simple CSE expression compares an expression to a set of simple expressionsto determine the result.

    O The searched CSE expression e)aluates a set of 2oolean expressions to determine

    the result.

    2oth formats support an optional ELSE ar3ument.

    CSE can be used in an statement or clause that allows a )alid expression. #or example,

    ou can use CSE in statements such as SELECT, H:TE, :ELETE and SET, and in

    clauses such as select_list, ', 6/E$E, %$:E$ 2=, and /G'.

    Sntax

    Simple CSE expression(

    CSE input_expression

    6/E' when_expression T/E' result_expression U ...n V

    U ELSE else_result_expression V

    E':

    Searched CSE expression(

  • 7/25/2019 SQL Queries for Non SAP Applications

    55/65

    CSE

    6/E' 2oolean_expression T/E' result_expression U ...n V

    U ELSE else_result_expression V

    E':

    r3uments

    ________________________________________

    input_expression

    s the expression e)aluated when the simple CSE format is used. input_expression is an

    )alid expression.

    6/E' when_expression

    s a simple expression to which input_expression is compared when the simple CSE format

    is used. when_expression is an )alid expression. The data tpes of input_expression and

    each when_expression must be the same or must be an implicit con)ersion.

    T/E' result_expression

    s the expression returned when input_expression e8uals when_expression e)aluates to

    T$HE, or 2oolean_expression e)aluates to T$HE. result expression is an )alid expression.

    ELSE else_result_expression

    s the expression returned if no comparison operation e)aluates to T$HE. f this ar3ument is

    omitted and no comparison operation e)aluates to T$HE, CSE returns 'HLL.

    else_result_expression is an )alid expression. The data tpes of else_result_expression and

    an result_expression must be the same or must be an implicit con)ersion.

    6/E' 2oolean_expression

    s the 2oolean expression e)aluated when usin3 the searched CSE format.

    2oolean_expression is an )alid 2oolean expression.

    $eturn Tpes

    ________________________________________

    $eturns the hi3hest precedence tpe from the set of tpes in result_expressions and the

    optional else_result_expression. #or more information, see :ata Tpe recedence !Transact-

    SQL".

    $eturn Galues

    Simple CSE expression(

  • 7/25/2019 SQL Queries for Non SAP Applications

    56/65

    The simple CSE expression operates b comparin3 the first expression to the expression in

    each 6/E' clause for e8ui)alenc. f these expressions are e8ui)alent, the expression in the

    T/E' clause will be returned.

    O llows onl an e8ualit chec7.

    O E)aluates input_expression, and then in the order specified, e)aluates

    input_expression ; when_expression for each 6/E' clause.

    O $eturns the result_expression of the first input_expression ; when_expression that

    e)aluates to T$HE.

    O f no input_expression ; when_expression e)aluates to T$HE, the SQL Ser)er

    :atabase En3ine returns the else_result_expression if an ELSE clause is specified, or a

    'HLL )alue if no ELSE clause is specified.

    Searched CSE expression(

    O E)aluates, in the order specified, 2oolean_expression for each 6/E' clause.

    O $eturns result_expression of the first 2oolean_expression that e)aluates to T$HE.

    O f no 2oolean_expression e)aluates to T$HE, the :atabase En3ine returns the

    else_result_expression if an ELSE clause is specified, or a 'HLL )alue if no ELSE clause is

    specified.

    $emar7s ________________________________________

    SQL Ser)er allows for onl 0 le)els of nestin3 in CSE expressions.

    The CSE expression cannot be used to control the flow of execution of Transact-SQL

    statements, statement bloc7s, user-defined functions, and stored procedures. #or a list of

    control-of-flow methods, see Control-of-#low Lan3ua3e !Transact-SQL".

    The CSE statement e)aluates its conditions se8uentiall and stops with the first condition

    whose condition is satisfied. n some situations, an expression is e)aluated before a CSE

    statement recei)es the results of the expression as its input. Errors in e)aluatin3 these

    expressions are possible. 33re3ate expressions that appear in 6/E' ar3uments to a CSEstatement are e)aluated first, then pro)ided to the CSE statement. #or example, the

    followin3 8uer produces a di)ide b ero error when producin3 the )alue of the &R

    a33re3ate. This occurs prior to e)aluatin3 the CSE expression.

    Transact-SQL

    Cop

    6T/ :ata !)alue" S

  • 7/25/2019 SQL Queries for Non SAP Applications

    57/65

    ! SELECT 0

    H'%' LL

    SELECT "

    SELECT

    CSE

    6/E' &'!)alue" A; 0 T/E' 0

    6/E' &R!)alue" B; 00 T/E'

    E':

    #$%& :ata M

    =ou should onl depend on order of e)aluation of the 6/E' conditions for scalar

    expressions !includin3 non-correlated sub-8ueries that return scalars", not for a33re3ate

    expressions.

    Examples

    ________________________________________

    . Hsin3 a SELECT statement with a simple CSE expression

    6ithin a SELECT statement, a simple CSE expression allows for onl an e8ualit chec7Mno other comparisons are made. The followin3 example uses the CSE expression to chan3e

    the displa of product line cate3ories to ma7e them more understandable.

    Transact-SQL

    Cop

    HSE d)enture6or7s101M

    %

    SELECT roduct'umber, Cate3or ;

    CSE roductLine

    6/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    58/65

    E':,

    'ame

    #$%& roduction.roduct

    %$:E$ 2= roduct'umberM

    %

    Transact-SQL

    Cop

    HSE d)enture6or7s101M

    %

    SELECT roduct'umber, Cate3or ;

    CSE roductLine

    6/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    59/65

    Cop

    HSE d)enture6or7s101M

    %

    SELECT roduct'umber, 'ame, +rice $an3e+ ;

    CSE

    6/E' Listrice ; 0 T/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    60/65

    HSE d)enture6or7s101M

    %

    SELECT #irst'ame, Last'ame, Telephone'umber, +6hen to Contact+ ;

    CSE

    6/E' TelephoneSpecialnstructions S 'HLL T/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    61/65

    6/E$E Territor'ame S '%T 'HLL

    %$:E$ 2= CSE Countr$e3ion'ame 6/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    62/65

    2usinessEntit: and the contact tpe for that person.The CSE expression in the SET

    statement determines the )alue to displa for the column ContactTpe based on the existence

    of the 2usinessEntit: column in the Emploee, Gendor, or Customer tables.

    Transact-SQL

    Cop

    HSE d)enture6or7s101M

    %

    C$ETE #H'CT%' dbo.etContactnformation!X2usinessEntit: int"

    $ETH$'S XretContactnformation T2LE

    !2usinessEntit: int '%T 'HLL,

    #irst'ame n)archar!@0" 'HLL,

    Last'ame n)archar!@0" 'HLL,

    ContactTpe n)archar!@0" 'HLL,

    $&$= 5E= CLHSTE$E: !2usinessEntit: SC""

    S

    -- $eturns the first name, last name and contact tpe for the specified contact.

    2E'

    :ECL$E

    X#irst'ame n)archar!@0",

    XLast'ame n)archar!@0",

    XContactTpe n)archar!@0"M

    -- et common contact information

    SELECT

    X2usinessEntit: ; 2usinessEntit:,

    X#irst'ame ; #irst'ame,

    XLast'ame ; Last'ame

    #$%& erson.erson

  • 7/25/2019 SQL Queries for Non SAP Applications

    63/65

    6/E$E 2usinessEntit: ; X2usinessEntit:M

    SET XContactTpe ;

    CSE

    -- Chec7 for emploee

    6/E' ERSTS!SELECT * #$%& /uman$esources.Emploee S e

    6/E$E e.2usinessEntit: ; X2usinessEntit:"

    T/E'

  • 7/25/2019 SQL Queries for Non SAP Applications

    64/65

    'SE$T XretContactnformation

    SELECT X2usinessEntit:, X#irst'ame, XLast'ame, XContactTpeM

    E':M

    $ETH$'M

    E':M

    %

    SELECT 2usinessEntit:, #irst'ame, Last'ame, ContactTpe

    #$%& dbo.etContactnformation!1100"M

    %

    SELECT 2usinessEntit:, #irst'ame, Last'ame, ContactTpe

    #$%& dbo.etContactnformation!@"M

    . Hsin3 CSE in a /G' clause

    The followin3 example uses the CSE expression in a /G' clause to restrict the rows

    returned b the SELECT statement. The statement returns the the maximum hourl rate for

    each Job title in the /uman$esources.Emploee table. The /G' clause restricts the titlesto those that are held b men with a maximum pa rate 3reater than F0 dollars or women with

    a maximum pa rate 3reater than F1 dollars.

    Transact-SQL

    Cop

    HSE d)enture6or7s101M

    %

    SELECT IobTitle, &R!ph.$ate"S &aximum$ate

    #$%& /uman$esources.Emploee S e

    I%' /uman$esources.Emploeea/istor S ph %' e.2usinessEntit: ;

    ph.2usinessEntit:

    $%H 2= IobTitle

    /G' !&R!CSE 6/E' ender ;

  • 7/25/2019 SQL Queries for Non SAP Applications

    65/65

    ELSE 'HLL E':" B F0.00

    %$ &R!CSE 6/E' ender ;