sql notes for beginners

Upload: tejas-jadhav

Post on 04-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 SQL Notes For Beginners

    1/107

    TUTORIAL SOURCE : W3SCHOOLS

    Introduction to SQL

    SQL is a standard language for accessing and mani ulating data!ases"

    What is SQL? SQL stands for Structured Quer# Language SQL lets #ou access and mani ulate data!ases

    SQL is an A$SI %American $ational Standards Institute& standard

    What Can SQL do? SQL can e'ecute (ueries against a data!ase SQL can retrie)e data from a data!ase

    SQL can insert records in a data!ase

    SQL can u date records in a data!ase SQL can delete records from a data!ase

    SQL can create ne* data!ases

    SQL can create ne* ta!les in a data!ase

    SQL can create stored rocedures in a data!ase

    SQL can create )ie*s in a data!ase

    SQL can set ermissions on ta!les+ rocedures+ and )ie*s

    SQL is a Standard - BUT....

    Alt,oug, SQL is an A$SI %American $ational Standards Institute& standard+ t,ere are man#different )ersions of t,e SQL language"

  • 8/13/2019 SQL Notes For Beginners

    2/107

    Ho*e)er+ to !e com liant *it, t,e A$SI standard+ t,e# all su ort at least t,e ma-or commands%suc, as SELECT+ U./ATE+ /ELETE+ I$SERT+ WHERE& in a similar manner"

    Note: 0ost of t,e SQL data!ase rograms also ,a)e t,eir o*n ro rietar# e'tensions in additionto t,e SQL standard1

    Using SQL in Your Web Site

    To !uild a *e! site t,at s,o*s some data from a data!ase+ #ou *ill need t,e follo*ing:

    An R/20S data!ase rogram %i"e" 0S Access+ SQL Ser)er+ 0#SQL& A ser)er side scri ting language+ li4e .H. or AS.

    SQL

    HT0L 5 CSS

    RDBMS

    R/20S stands for Relational /ata!ase 0anagement S#stem"

    R/20S is t,e !asis for SQL+ and for all modern data!ase s#stems li4e 0S SQL Ser)er+ I20/26+ Oracle+ 0#SQL+ and 0icrosoft Access"

    T,e data in R/20S is stored in data!ase o!-ects called ta!les"

    A ta!le is a collections of related data entries and it consists of columns and ro*s"

    Database Tab es

    A data!ase most often contains one or more ta!les" Eac, ta!le is identified !# a name %e"g"7Customers7 or 7Orders7&" Ta!les contain records %ro*s& *it, data"

    2elo* is an e'am le of a ta!le called 7.ersons7:

    !"#d LastNa$e %irstNa$e &ddress Cit'8 Hansen Ola Timotei)n 89 Sandnes6 S)endson To)e 2org)n 63 Sandnes3 .ettersen ari Storgt 69 Sta)anger

  • 8/13/2019 SQL Notes For Beginners

    3/107

    T,e ta!le a!o)e contains t,ree records %one for eac, erson& and fi)e columns %.;Id+ Last$ame+

  • 8/13/2019 SQL Notes For Beginners

    4/107

    T,e //L art of SQL ermits data!ase ta!les to !e created or deleted" It also define inde'es%4e#s&+ s ecif# lin4s !et*een ta!les+ and im ose constraints !et*een ta!les" T,e most im ortant//L statements in SQL are:

    CR,&T, D&T&B&S, creates a ne* data!ase

    &LT,R D&T&B&S, modifies a data!ase CR,&T, T&BL, creates a ne* ta!le

    &LT,R T&BL, modifies a ta!le

    DR ! T&BL, deletes a ta!le

    CR,&T, #ND, creates an inde' %searc, 4e#&

    DR ! #ND, deletes an inde'

    SQL SELECT Statement

    T,is c,a ter *ill e' lain t,e SELECT and t,e SELECT = statements"

    The SQL S,L,CT State$ent

    T,e SELECT statement is used to select data from a data!ase"

    T,e result is stored in a result ta!le+ called t,e result set"

    SQL SELECT SyntaxSELECT column_name(s)FROM table_name

    and

    SELECT * FROM table_name

    Note: SQL is not case sensiti)e" SELECT is t,e same as select"

    &n SQL S,L,CT ,/a$) e

    T,e 7.ersons7 ta!le:

  • 8/13/2019 SQL Notes For Beginners

    5/107

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select t,e content of t,e columns named 7Last$ame7 and 7

  • 8/13/2019 SQL Notes For Beginners

    6/107

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    SQL SELECT DISTINCT Statement

    T,is c,a ter *ill e' lain t,e SELECT /ISTI$CT statement"

    The SQL S,L,CT D#ST#NCT State$ent

    In a ta!le+ some of t,e columns ma# contain du licate )alues" T,is is not a ro!lem+ ,o*e)er+sometimes #ou *ill *ant to list onl# t,e different %distinct& )alues in a ta!le"

    T,e /ISTI$CT 4e#*ord can !e used to return onl# distinct %different& )alues"

    SQL SELECT DISTINCT SyntaxSELECT ST 'CT column_name(s)FROM table_name

    S,L,CT D#ST#NCT ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select onl# t,e distinct )alues from t,e column named 7Cit#7 from t,e ta!lea!o)e"

    We use t,e follo*ing SELECT statement:

  • 8/13/2019 SQL Notes For Beginners

    7/107

    SELECT ST 'CT Cit+ FROM %e"sons

    T,e result set *ill loo4 li4e t,is:

    City

    Sandnes

    Stavan#e"

    SQL WHERE Clause

    T,e WHERE clause is used to filter records"

    The W1,R, C ause

    T,e WHERE clause is used to e'tract onl# t,ose records t,at fulfill a s ecified criterion"

    SQL WHERE SyntaxSELECT column_name(s)

    FROM table_name,HERE column_name o-e"ato" value

    W1,R, C ause ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

  • 8/13/2019 SQL Notes For Beginners

    8/107

    $o* *e *ant to select onl# t,e ersons li)ing in t,e cit# 7Sandnes7 from t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons

    ,HERE Cit+./Sandnes/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Quotes &round Te/t %ie ds

    SQL uses single (uotes around te't )alues %most data!ase s#stems *ill also acce t dou!le(uotes&"

    Alt,oug,+ numeric )alues s,ould not !e enclosed in (uotes"

  • 8/13/2019 SQL Notes For Beginners

    9/107

    SELECT * FROM %e"sons ,HERE 3ea"./1456/

    )erators & o2ed in the W1,R, C auseWit, t,e WHERE clause+ t,e follo*ing o erators can !e used:

    Operator Description

    . E7ual

    89 'ot e7ual

    9 :"eate" t an

    8 Less t an

    9. :"eate" t an o" e7ual

    8. Less t an o" e7ual

    !ET,EE'

    !et2een an inclusive "an#e

    L &E Sea"c ;o" a -atte"n

    ' ; +ou

  • 8/13/2019 SQL Notes For Beginners

    10/107

  • 8/13/2019 SQL Notes For Beginners

    11/107

  • 8/13/2019 SQL Notes For Beginners

    12/107

    SQL ORDER !" SyntaxSELECT column_name(s)FROM table_nameOR ER !3 column_name(s) >SC? ESC

    RD,R BY ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Tom Ain#vn $ Stavan#e"

    $o* *e *ant to select all t,e ersons from t,e ta!le a!o)e+ ,o*e)er+ *e *ant to sort t,e ersons !# t,eir last name"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons OR ER !3 Last'ame

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    @ 'ilsen Tom Ain#vn $ Stavan#e"

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    Svendson Tove !o"#vn $ Sandnes

    RD,R BY D,SC ,/a$) e

  • 8/13/2019 SQL Notes For Beginners

    13/107

    $o* *e *ant to select all t,e ersons from t,e ta!le a!o)e+ ,o*e)er+ *e *ant to sort t,e ersonsdescending !# t,eir last name"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sonsOR ER !3 Last'ame ESC

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Tom Ain#vn $ Stavan#e"

    1 Hansen Ola Timoteivn 10 Sandnes

    SQL INSERT INTO Statement

    T,e I$SERT I$TO statement is used to insert ne* records in a ta!le"

    The #NS,RT #NT State$ent

    T,e I$SERT I$TO statement is used to insert a ne* ro* in a ta!le"

    SQL INSERT INTO Syntax

    It is ossi!le to *rite t,e I$SERT I$TO statement in t*o forms"

    T,e first form doesn t s ecif# t,e column names *,ere t,e data *ill !e inserted+ onl# t,eir)alues:

    'SERT 'TO table_nameA>LBES (value1 value value$ )

  • 8/13/2019 SQL Notes For Beginners

    14/107

    T,e second form s ecifies !ot, t,e column names and t,e )alues to !e inserted:

    'SERT 'TO table_name (column1 column column$ )A>LBES (value1 value value$ )

    SQL #NS,RT #NT ,/a$) e

    We ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to insert a ne* ro* in t,e 7.ersons7 ta!le"

    We use t,e follo*ing SQL statement:

    'SERT 'TO %e"sonsA>LBES (@ /'ilsen/ /Do an/ /!a

  • 8/13/2019 SQL Notes For Beginners

    15/107

    T,e follo*ing SQL statement *ill add a ne* ro*+ !ut onl# add data in t,e 7.;Id7+ 7Last$ame7and t,e 7

  • 8/13/2019 SQL Notes For Beginners

    16/107

    SQL U!D&T, ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Do an !aTE %e"sonsSET >dd"ess./'issestien 5 / Cit+./Sandnes/,HERE Last'ame./T essem/ >' Fi"st'ame./Da

  • 8/13/2019 SQL Notes For Beginners

    17/107

    B% >TE %e"sonsSET >dd"ess./'issestien 5 / Cit+./Sandnes/

    T,e 7.ersons7 ta!le *ould ,a)e loo4ed li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola 'issestien 5 Sandnes

    Svendson Tove 'issestien 5 Sandnes

    $ %ette"sen &a"i 'issestien 5 Sandnes

    @ 'ilsen Do an 'issestien 5 Sandnes

    6 T essem Da

  • 8/13/2019 SQL Notes For Beginners

    18/107

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Do an !a

  • 8/13/2019 SQL Notes For Beginners

    19/107

    Note: 2e )er# careful *,en deleting records" Bou cannot undo t,is statement1

    SQL TO Clause

    The T ! C ause

    T,e TO. clause is used to s ecif# t,e num!er of records to return"

    T,e TO. clause can !e )er# useful on large ta!les *it, t,ousands of records" Returning a largenum!er of records can im act on erformance"

    Note: $ot all data!ase s#stems su ort t,e TO. clause"

    SQL Ser'er SyntaxSELECT TO% numbe"?-e"cent column_name(s)FROM table_name

    SQL S,L,CT T ! ,4ui5a ent in M'SQL and ra* e

    (ySQL SyntaxSELECT column_name(s)FROM table_name

    L M T numbe"

    Exam leSELECT *FROM %e"sonsL M T 6

    Oracle SyntaxSELECT column_name(s)

    FROM table_name,HERE RO,'BM 8. numbe"

    Exam leSELECT *FROM %e"sons ,HERE RO,'BM 8.6

  • 8/13/2019 SQL Notes For Beginners

    20/107

    SQL T ! ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Tom Ain#vn $ Stavan#e"

    $o* *e *ant to select onl# t,e t*o first records in t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT TO% * FROM %e"sons

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    SQL T ! !,RC,NT ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

  • 8/13/2019 SQL Notes For Beginners

    21/107

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    @ 'ilsen Tom Ain#vn $ Stavan#e"

    $o* *e *ant to select onl# 9F of t,e records in t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT TO% 60 %ERCE'T * FROM %e"sons

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    SQL LI#E O erator

    T,e LI E o erator is used in a WHERE clause to searc, for a s ecified attern in a column"

    The L#(, )erator

    T,e LI E o erator is used to searc, for a s ecified attern in a column"

    SQL LI#E SyntaxSELECT column_name(s)FROM table_name,HERE column_name L &E -atte"n

    L#(, )erator ,/a$) e

    T,e 7.ersons7 ta!le:

  • 8/13/2019 SQL Notes For Beginners

    22/107

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select t,e ersons li)ing in a cit# t,at starts *it, 7s7 from t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ L &E /sG/

    T,e 7F7 sign can !e used to define *ildcards %missing letters in t,e attern& !ot, !efore andafter t,e attern"

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $e't+ *e *ant to select t,e ersons li)ing in a cit# t,at ends *it, an 7s7 from t,e 7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ L &E /Gs/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

  • 8/13/2019 SQL Notes For Beginners

    23/107

    $e't+ *e *ant to select t,e ersons li)ing in a cit# t,at contains t,e attern 7ta)7 from t,e7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ L &E /GtavG/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    It is also ossi!le to select t,e ersons li)ing in a cit# t,at $OT contains t,e attern 7ta)7 fromt,e 7.ersons7 ta!le+ !# using t,e $OT 4e#*ord"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ 'OT L &E /GtavG/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    SQL Wildcards

    SQL *ildcards can !e used *,en searc,ing for data in a data!ase"

    SQL Wi d*ards

    SQL *ildcards can su!stitute for one or more c,aracters *,en searc,ing for data in a data!ase"

  • 8/13/2019 SQL Notes For Beginners

    24/107

    SQL *ildcards must !e used *it, t,e SQL LI E o erator"

    Wit, SQL+ t,e follo*ing *ildcards can !e used:

    Wildcard Description

    G > substitute ;o" e"o o" mo"e c a"acte"s

    _ > substitute ;o" e=actl+ one c a"acte"

    Ic a"listJ >n+ sin#le c a"acte" in c a"list

    IKc a"listJ

    or

    G1c,arlist

    >n+ sin#le c a"acte" not in c a"list

    SQL Wi d*ard ,/a$) es

    We ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    Using the 6 Wi d*ard

    $o* *e *ant to select t,e ersons li)ing in a cit# t,at starts *it, 7sa7 from t,e 7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ L &E /saG/

    T,e result set *ill loo4 li4e t,is:

  • 8/13/2019 SQL Notes For Beginners

    25/107

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $e't+ *e *ant to select t,e ersons li)ing in a cit# t,at contains t,e attern 7nes7 from t,e7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Cit+ L &E /GnesG/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Using the " Wi d*ard $o* *e *ant to select t,e ersons *it, a first name t,at starts *it, an# c,aracter+ follo*ed !#7la7 from t,e 7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Fi"st'ame L &E /_la/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

  • 8/13/2019 SQL Notes For Beginners

    26/107

    $e't+ *e *ant to select t,e ersons *it, a last name t,at starts *it, 7S7+ follo*ed !# an#c,aracter+ follo*ed !# 7end7+ follo*ed !# an# c,aracter+ follo*ed !# 7on7 from t,e 7.ersons7ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Last'ame L &E /S_end_on/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    Using the 7*har ist8 Wi d*ard

    $o* *e *ant to select t,e ersons *it, a last name t,at starts *it, 7!7 or 7s7 or 7 7 from t,e7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons

    ,HERE Last'ame L &E /Ibs-JG/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $e't+ *e *ant to select t,e ersons *it, a last name t,at do not start *it, 7!7 or 7s7 or 7 7 fromt,e 7.ersons7 ta!le"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Last'ame L &E /I bs-JG/

  • 8/13/2019 SQL Notes For Beginners

    27/107

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    SQL IN O erator

    The #N )erator

    T,e I$ o erator allo*s #ou to s ecif# multi le )alues in a WHERE clause"

    SQL IN SyntaxSELECT column_name(s)FROM table_name,HERE column_name ' (value1 value )

    #N )erator ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select t,e ersons *it, a last name e(ual to 7Hansen7 or 7.ettersen7 from t,eta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Last'ame ' (/Hansen/ /%ette"sen/)

  • 8/13/2019 SQL Notes For Beginners

    28/107

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    SQL !ETWEEN O erator

    T,e 2ETWEE$ o erator is used in a WHERE clause to select a range of data !et*een t*o)alues"

    The B,TW,,N )erator

    T,e 2ETWEE$ o erator selects a range of data !et*een t*o )alues" T,e )alues can !enum!ers+ te't+ or dates"

    SQL !ETWEEN SyntaxSELECT column_name(s)FROM table_name,HERE column_name!ET,EE' value1 >' value

    B,TW,,N )erator ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

  • 8/13/2019 SQL Notes For Beginners

    29/107

    $o* *e *ant to select t,e ersons *it, a last name al ,a!eticall# !et*een 7Hansen7 and7.ettersen7 from t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM %e"sons,HERE Last'ame!ET,EE' /Hansen/ >' /%ette"sen/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Note: T,e 2ETWEE$ o erator is treated differentl# in different data!ases"

    In some data!ases+ ersons *it, t,e Last$ame of 7Hansen7 or 7.ettersen7 *ill not !e listed+ !ecause t,e 2ETWEE$ o erator onl# selects fields t,at are !et*een and e'cluding t,e test)alues&"

    In ot,er data!ases+ ersons *it, t,e Last$ame of 7Hansen7 or 7.ettersen7 *ill !e listed+ !ecauset,e 2ETWEE$ o erator selects fields t,at are !et*een and including t,e test )alues&"

    And in ot,er data!ases+ ersons *it, t,e Last$ame of 7Hansen7 *ill !e listed+ !ut 7.ettersen7*ill not !e listed %li4e t,e e'am le a!o)e&+ !ecause t,e 2ETWEE$ o erator selects fields

    !et*een t,e test )alues+ including t,e first test )alue and e'cluding t,e last test )alue"

    T,erefore: C,ec4 ,o* #our data!ase treats t,e 2ETWEE$ o erator"

    ,/a$) e 9

    To dis la# t,e ersons outside t,e range in t,e re)ious e'am le+ use $OT 2ETWEE$:

    SELECT * FROM %e"sons

    ,HERE Last'ame'OT !ET,EE' /Hansen/ >' /%ette"sen/

    T,e result set *ill loo4 li4e t,is:

    P_Id LastName FirstName Address City

  • 8/13/2019 SQL Notes For Beginners

    30/107

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    SQL Alias

    Wit, SQL+ an alias name can !e gi)en to a ta!le or to a column"

    SQL & ias

    Bou can gi)e a ta!le or a column anot,er name !# using an alias" T,is can !e a good t,ing to doif #ou ,a)e )er# long or com le' ta!le names or column names"

    An alias name could !e an#t,ing+ !ut usuall# it is s,ort"

    SQL Alias Syntax )or Ta*lesSELECT column_name(s)FROM table_name>S alias_name

    SQL Alias Syntax )or ColumnsSELECT column_name >S alias_nameFROM table_name

    & ias ,/a$) e

    Assume *e ,a)e a ta!le called 7.ersons7 and anot,er ta!le called 7.roduct;Orders7" We *ill

    gi)e t,e ta!le aliases of 7 7 and 7 o7 res ecti)el#"

    $o* *e *ant to list all t,e orders t,at 7Ola Hansen7 is res onsi!le for"

    We use t,e follo*ing SELECT statement:

    SELECT -o O"de" - Last'ame - Fi"st'ameFROM %e"sons >S -

  • 8/13/2019 SQL Notes For Beginners

    31/107

    %"oduct_O"de"s >S -o,HERE - Last'ame./Hansen/ >' - Fi"st'ame./Ola/

    T,e same SELECT statement *it,out aliases:

    SELECT %"oduct_O"de"s O"de" %e"sons Last'ame %e"sons Fi"st'ameFROM %e"sons%"oduct_O"de"s,HERE %e"sons Last'ame./Hansen/ >' %e"sons Fi"st'ame./Ola/

    As #ou ll see from t,e t*o SELECT statements a!o)e aliases can ma4e (ueries easier to !ot,*rite and to read"

    SQL +oins

    SQL -oins are used to (uer# data from t*o or more ta!les+ !ased on a relations,i !et*eencertain columns in t,ese ta!les"

    SQL #N

    T,e DOI$ 4e#*ord is used in an SQL statement to (uer# data from t*o or more ta!les+ !ased ona relations,i !et*een certain columns in t,ese ta!les"

    Ta!les in a data!ase are often related to eac, ot,er *it, 4e#s"

    A rimar# 4e# is a column %or a com!ination of columns& *it, a uni(ue )alue for eac, ro*"Eac, rimar# 4e# )alue must !e uni(ue *it,in t,e ta!le" T,e ur ose is to !ind data toget,er+across ta!les+ *it,out re eating all of t,e data in e)er# ta!le"

    Loo4 at t,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

  • 8/13/2019 SQL Notes For Beginners

    32/107

    $ote t,at t,e 7.;Id7 column is t,e rimar# 4e# in t,e 7.ersons7 ta!le" T,is means t,at no t*oro*s can ,a)e t,e same .;Id" T,e .;Id distinguis,es t*o ersons e)en if t,e# ,a)e t,e samename"

    $e't+ *e ,a)e t,e 7Orders7 ta!le:

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@ 5@ 16

    $ote t,at t,e 7O;Id7 column is t,e rimar# 4e# in t,e 7Orders7 ta!le and t,at t,e 7.;Id7 columnrefers to t,e ersons in t,e 7.ersons7 ta!le *it,out using t,eir names"

    $otice t,at t,e relations,i !et*een t,e t*o ta!les a!o)e is t,e 7.;Id7 column"

    Di++erent SQL #Ns

    2efore *e continue *it, e'am les+ *e *ill list t,e t# es of DOI$ #ou can use+ and t,edifferences !et*een t,em"

    JOIN Retu"n "o2s 2 en t e"e is at least one matc in bot tables LEFT JOIN Retu"n all "o2s ;"om t e le;t table even i; t e"e a"e no matc es in t e "i# t

    table

    RIG T JOIN Retu"n all "o2s ;"om t e "i# t table even i; t e"e a"e no matc es in t e le;ttable

    F!LL JOIN Retu"n "o2s 2 en t e"e is a matc in one o; t e tables

    SQL INNER +OIN #ey$ord

    SQL #NN,R #N (e'2ord

    T,e I$$ER DOI$ 4e#*ord return ro*s *,en t,ere is at least one matc, in !ot, ta!les"

  • 8/13/2019 SQL Notes For Beginners

    33/107

    SQL INNER +OIN SyntaxSELECT column_name(s)FROM table_name1

    ''ER DO ' table_nameO' table_name1 column_name.table_name column_name

    !S: I$$ER DOI$ is t,e same as DOI$"

    SQL #NN,R #N ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    T,e 7Orders7 ta!le:

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@ 5@ 16

    $o* *e *ant to list all t,e ersons *it, an# orders"

    We use t,e follo*ing SELECT statement:

    SELECT %e"sons Last'ame %e"sons Fi"st'ame O"de"s O"de"'oFROM %e"sons

    ''ER DO ' O"de"sO' %e"sons %_ d.O"de"s %_ d

  • 8/13/2019 SQL Notes For Beginners

    34/107

    OR ER !3 %e"sons Last'ame

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    T,e I$$ER DOI$ 4e#*ord return ro*s *,en t,ere is at least one matc, in !ot, ta!les" If t,ere

    are ro*s in 7.ersons7 t,at do not ,a)e matc,es in 7Orders7+ t,ose ro*s *ill $OT !e listed"

    SQL LE,T +OIN #ey$ord

    SQL L,%T #N (e'2ord

    T,e LE

  • 8/13/2019 SQL Notes For Beginners

    35/107

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    T,e 7Orders7 ta!le:

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@ 5@ 16

    $o* *e *ant to list all t,e ersons and t,eir orders if an#+ from t,e ta!les a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT %e"sons Last'ame %e"sons Fi"st'ame O"de"s O"de"'oFROM %e"sons

    LEFT DO ' O"de"sO' %e"sons %_ d.O"de"s %_ dOR ER !3 %e"sons Last'ame

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    Svendson Tove

  • 8/13/2019 SQL Notes For Beginners

    36/107

  • 8/13/2019 SQL Notes For Beginners

    37/107

    @ @65 1

    6 $@ 5@ 16

    $o* *e *ant to list all t,e orders *it, containing ersons if an#+ from t,e ta!les a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT %e"sons Last'ame %e"sons Fi"st'ame O"de"s O"de"'oFROM %e"sonsR :HT DO ' O"de"sO' %e"sons %_ d.O"de"s %_ dOR ER !3 %e"sons Last'ame

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    $@ 5@

    T,e RIJHT DOI$ 4e#*ord returns all t,e ro*s from t,e rig,t ta!le %Orders&+ e)en if t,ere are nomatc,es in t,e left ta!le %.ersons&"

    SQL ,%LL +OIN #ey$ord

    SQL %ULL #N (e'2ordT,e

  • 8/13/2019 SQL Notes For Beginners

    38/107

    FBLL DO ' table_nameO' table_name1 column_name.table_name column_name

    SQL %ULL #N ,/a$) e

    T,e 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    T,e 7Orders7 ta!le:

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@ 5@ 16

    $o* *e *ant to list all t,e ersons and t,eir orders+ and all t,e orders *it, t,eir ersons"

    We use t,e follo*ing SELECT statement:

    SELECT %e"sons Last'ame %e"sons Fi"st'ame O"de"s O"de"'o

    FROM %e"sonsFBLL DO ' O"de"sO' %e"sons %_ d.O"de"s %_ dOR ER !3 %e"sons Last'ame

    T,e result set *ill loo4 li4e t,is:

  • 8/13/2019 SQL Notes For Beginners

    39/107

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    Svendson Tove

    $@ 5@

    T,e

  • 8/13/2019 SQL Notes For Beginners

    40/107

    SQL %NION ALL SyntaxSELECT column_name(s) FROM table_name1B' O' >LLSELECT column_name(s) FROM table_name

    !S: T,e column names in t,e result set of a U$IO$ are al*a#s e(ual to t,e column names in t,efirst SELECT statement in t,e U$IO$"

    SQL UN# N ,/a$) e

    Loo4 at t,e follo*ing ta!les:

  • 8/13/2019 SQL Notes For Beginners

    41/107

  • 8/13/2019 SQL Notes For Beginners

    42/107

    Tu"ne" Sall+

    &ent Cla"!LE %e"sons(%_ d intLast'ame va"c a"( 66)Fi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66))

    T,e .;Id column is of t# e int and *ill ,old a num!er" T,e Last$ame+

  • 8/13/2019 SQL Notes For Beginners

    46/107

    Constraints are used to limit t,e t# e of data t,at can go into a ta!le"

    Constraints can !e s ecified *,en a ta!le is created %*it, t,e CREATE TA2LE statement& orafter t,e ta!le is created %*it, t,e ALTER TA2LE statement&"

    We *ill focus on t,e follo*ing constraints: 'OT 'BLL B' NBE

    %R M>R3 &E3

    FORE :' &E3

    CHEC&

    EF>BLT

    T,e ne't c,a ters *ill descri!e eac, constraint in details"

    SQL NOT N%LL Constraint

    2# default+ a ta!le column can ,old $ULL )alues"

    SQL N T NULL ConstraintT,e $OT $ULL constraint enforces a column to $OT acce t $ULL )alues"

    T,e $OT $ULL constraint enforces a field to al*a#s contain a )alue" T,is means t,at #oucannot insert a ne* record+ or u date a record *it,out adding a )alue to t,is field"

    T,e follo*ing SQL enforces t,e 7.;Id7 column and t,e 7Last$ame7 column to not acce t $ULL )alues:

    CRE>TE T>!LE %e"sons

    (%_ d int 'OT 'BLLLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66))

  • 8/13/2019 SQL Notes For Beginners

    47/107

    SQL %NIQ%E Constraint

    SQL UN#QU, ConstraintT,e U$IQUE constraint uni(uel# identifies eac, record in a data!ase ta!le"

    T,e U$IQUE and .RI0ARB EB constraints !ot, ro)ide a guarantee for uni(ueness for acolumn or set of columns"

    A .RI0ARB EB constraint automaticall# ,as a U$IQUE constraint defined on it"

    $ote t,at #ou can ,a)e man# U$IQUE constraints er ta!le+ !ut onl# one .RI0ARB EBconstraint er ta!le"

    SQL UN#QU, Constraint on CR,&T, T&BL,

    T,e follo*ing SQL creates a U$IQUE constraint on t,e 7.;Id7 column *,en t,e 7.ersons7ta!le is created:

    M'SQL:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLLLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)B' NBE (%_ d))

    SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLL B' NBELast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)

  • 8/13/2019 SQL Notes For Beginners

    48/107

    )

    To allo* naming of a U$IQUE constraint+ and for defining a U$IQUE constraint on multi lecolumns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLLLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)CO'STR> 'T uc_%e"son B' NBE (%_ d Last'ame)

    )

    SQL UN#QU, Constraint on &LT,R T&BL,

    To create a U$IQUE constraint on t,e 7.;Id7 column *,en t,e ta!le is alread# created+ use t,efollo*ing SQL:

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    >LTER T>!LE %e"sons> B' NBE (%_ d)

    To allo* naming of a U$IQUE constraint+ and for defining a U$IQUE constraint on multi lecolumns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    >LTER T>!LE %e"sons> CO'STR> 'T uc_%e"son B' NBE (%_ d Last'ame)

    To DR ! a UN#QU, Constraint

    To dro a U$IQUE constraint+ use t,e follo*ing SQL:

  • 8/13/2019 SQL Notes For Beginners

    49/107

    M'SQL:

    >LTER T>!LE %e"sonsRO% ' E uc_%e"son

    SQL Ser5er > ra* e > MS &**ess:

    >LTER T>!LE %e"sonsRO% CO'STR> 'T uc_%e"son

    SQL RI(AR" #E" Constraint

    SQL !R#M&RY (,Y ConstraintT,e .RI0ARB EB constraint uni(uel# identifies eac, record in a data!ase ta!le"

    .rimar# 4e#s must contain uni(ue )alues"

    A rimar# 4e# column cannot contain $ULL )alues"

    Eac, ta!le s,ould ,a)e a rimar# 4e#+ and eac, ta!le can ,a)e onl# O$E rimar# 4e#"

    SQL !R#M&RY (,Y Constraint on CR,&T, T&BL,

    T,e follo*ing SQL creates a .RI0ARB EB on t,e 7.;Id7 column *,en t,e 7.ersons7 ta!le iscreated:

    M'SQL:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLL

    Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)%R M>R3 &E3 (%_ d))

  • 8/13/2019 SQL Notes For Beginners

    50/107

    SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLL %R M>R3 &E3

    Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66))

    To allo* naming of a .RI0ARB EB constraint+ and for defining a .RI0ARB EB constrainton multi le columns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLLLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)CO'STR> 'T - %R M>R3 &E3 (%_ d)

    To allo* naming of a .RI0ARB EB constraint+ and for defining a .RI0ARB EB constrainton multi le columns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

  • 8/13/2019 SQL Notes For Beginners

    51/107

    >LTER T>!LE %e"sons> CO'STR> 'T - 'T -

  • 8/13/2019 SQL Notes For Beginners

    52/107

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65

    @ @65 1

    $ote t,at t,e 7.;Id7 column in t,e 7Orders7 ta!le oints to t,e 7.;Id7 column in t,e 7.ersons7ta!le"

    T,e 7.;Id7 column in t,e 7.ersons7 ta!le is t,e .RI0ARB EB in t,e 7.ersons7 ta!le"

    T,e 7.;Id7 column in t,e 7Orders7 ta!le is a ra* e > MS &**ess:

    CRE>TE T>!LE O"de"s(

  • 8/13/2019 SQL Notes For Beginners

    53/107

    O_ d int 'OT 'BLL %R M>R3 &E3O"de"'o int 'OT 'BLL%_ d int FORE :' &E3 REFERE'CES %e"sons(%_ d))

    To allo* naming of a SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE O"de"s(O_ d int 'OT 'BLLO"de"'o int 'OT 'BLL%_ d int

    %R M>R3 &E3 (O_ d)CO'STR> 'T ; 'T ;

  • 8/13/2019 SQL Notes For Beginners

    54/107

    To DR ! a % R,#;N (,Y Constraint

    To dro a

  • 8/13/2019 SQL Notes For Beginners

    55/107

    Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)CHEC& (%_ d90)

    )

    SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLL CHEC& (%_ d90)Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)

    Cit+ va"c a"( 66))

    To allo* naming of a CHEC constraint+ and for defining a CHEC constraint on multi lecolumns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLLLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)CO'STR> 'T c

  • 8/13/2019 SQL Notes For Beginners

    56/107

    > CHEC& (%_ d90)

    To allo* naming of a CHEC constraint+ and for defining a CHEC constraint on multi lecolumns+ use t,e follo*ing SQL s#nta':

    M'SQL > SQL Ser5er > ra* e > MS &**ess:

    >LTER T>!LE %e"sons> CO'STR> 'T c

  • 8/13/2019 SQL Notes For Beginners

    57/107

    Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66) EF>BLT /Sandnes/)

    T,e /ETE T>!LE O"de"s(O_ d int 'OT 'BLLO"de"'o int 'OT 'BLL%_ d intO"de" ate date EF>BLT :ET >TE()

    )

    SQL D,%&ULT Constraint on &LT,R T&BL,

    To create a /EBLT /S>' 'ES/

    To DR ! a D,%&ULT Constraint

    To dro a /E

  • 8/13/2019 SQL Notes For Beginners

    58/107

    >LTER T>!LE %e"sons>LTER Cit+ RO% EF>BLT

    SQL Ser5er > ra* e > MS &**ess:

    >LTER T>!LE %e"sons>LTER COLBM' Cit+ RO% EF>BLT

    SQL CREATE INDE. Statement

    T,e CREATE I$/EK statement is used to create inde'es in ta!les"

    Inde'es allo* t,e data!ase a lication to find data fast *it,out reading t,e *,ole ta!le"

    #nde/es

    An inde' can !e created in a ta!le to find data more (uic4l# and efficientl#"

    T,e users cannot see t,e inde'es+ t,e# are -ust used to s eed u searc,es5(ueries"

    Note: U dating a ta!le *it, inde'es ta4es more time t,an u dating a ta!le *it,out %!ecause t,einde'es also need an u date&" So #ou s,ould onl# create inde'es on columns %and ta!les& t,at*ill !e fre(uentl# searc,ed against"

    SQL CREATE INDE. Syntax

    Creates an inde' on a ta!le" /u licate )alues are allo*ed:

    CRE>TE ' E inde=_nameO' table_name (column_name)

    SQL CREATE %NIQ%E INDE. Syntax

    Creates a uni(ue inde' on a ta!le" /u licate )alues are not allo*ed:

    CRE>TE B' NBE ' E inde=_nameO' table_name (column_name)

  • 8/13/2019 SQL Notes For Beginners

    59/107

    Note: T,e s#nta' for creating inde'es )aries amongst different data!ases" T,erefore: C,ec4 t,es#nta' for creating inde'es in #our data!ase"

    CR,&T, #ND, ,/a$) e

    T,e SQL statement !elo* creates an inde' named 7.Inde'7 on t,e 7Last$ame7 column in t,e7.ersons7 ta!le:

    CRE>TE ' E % nde=O' %e"sons (Last'ame)

    If #ou *ant to create an inde' on a com!ination of columns+ #ou can list t,e column names*it,in t,e arent,eses+ se arated !# commas:

    CRE>TE ' E % nde=O' %e"sons (Last'ame Fi"st'ame)

    SQL DRO INDE./ DRO TA!LE/ and DRO DATA!ASE

    Inde'es+ ta!les+ and data!ases can easil# !e deleted5remo)ed *it, t,e /RO. statement"

    The DR ! #ND, State$ent

    T,e /RO. I$/EK statement is used to delete an inde' in a ta!le"

    DRO INDE. Syntax )or (S Access0RO% ' E inde=_name O' table_name

    DRO INDE. Syntax )or (S SQL Ser'er0RO% ' E table_name inde=_name

    DRO INDE. Syntax )or D!12Oracle0RO% ' E inde=_name

    DRO INDE. Syntax )or (ySQL0>LTER T>!LE table_name RO% ' E inde=_name

  • 8/13/2019 SQL Notes For Beginners

    60/107

    The DR ! T&BL, State$entT,e /RO. TA2LE statement is used to delete a ta!le"

    RO% T>!LE table_name

    The DR ! D&T&B&S, State$ent

    T,e /RO. /ATA2ASE statement is used to delete a data!ase"

    RO% >T>!>SE database_name

    The TRUNC&T, T&BL, State$ent

    W,at if *e onl# *ant to delete t,e data inside t,e ta!le+ and not t,e ta!le itself

    T,en+ use t,e TRU$CATE TA2LE statement:

    TRB'C>TE T>!LE table_name

    SQL ALTER TA!LE Statement

    The &LT,R T&BL, State$ent

    T,e ALTER TA2LE statement is used to add+ delete+ or modif# columns in an e'isting ta!le"

    SQL ALTER TA!LE Syntax

    To add a column in a ta!le+ use t,e follo*ing s#nta':

    >LTER T>!LE table_name

  • 8/13/2019 SQL Notes For Beginners

    61/107

  • 8/13/2019 SQL Notes For Beginners

    62/107

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    Change Data T')e ,/a$) e

    $o* *e *ant to c,ange t,e data t# e of t,e column named 7/ateOf2irt,7 in t,e 7.ersons7 ta!le"

    We use t,e follo*ing SQL statement:

    >LTER T>!LE %e"sons>LTER COLBM' ateO;!i"t +ea"

    $otice t,at t,e 7/ateOf2irt,7 column is no* of t# e #ear and is going to ,old a #ear in a t*odigit or four digit format"

    DR ! C LUMN ,/a$) e

    $e't+ *e *ant to delete t,e column named 7/ateOf2irt,7 in t,e 7.ersons7 ta!le"

    We use t,e follo*ing SQL statement:

    >LTER T>!LE %e"sonsRO% COLBM' ateO;!i"t

    T,e 7.ersons7 ta!le *ill no* li4e t,is:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

  • 8/13/2019 SQL Notes For Beginners

    63/107

    SQL A%TO INCRE(ENT ,ield

    Auto increment allo*s a uni(ue num!er to !e generated *,en a ne* record is inserted into ata!le"

    &UT #NCR,M,NT a %ie d

    Mer# often *e *ould li4e t,e )alue of t,e rimar# 4e# field to !e created automaticall# e)er#time a ne* record is inserted"

    We *ould li4e to create an auto increment field in a ta!le"

    S'nta/ +or M'SQL

    T,e follo*ing SQL statement defines t,e 7.;Id7 column to !e an auto increment rimar# 4e#field in t,e 7.ersons7 ta!le:

    CRE>TE T>!LE %e"sons(%_ d int 'OT 'BLL >BTO_ 'CREME'TLast'ame va"c a"( 66) 'OT 'BLL

    Fi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66)%R M>R3 &E3 (%_ d))

    0#SQL uses t,e AUTO;I$CRE0E$T 4e#*ord to erform an auto increment feature"

    2# default+ t,e starting )alue for AUTO;I$CRE0E$T is 8+ and it *ill increment !# 8 for eac,ne* record"

    To let t,e AUTO;I$CRE0E$T se(uence start *it, anot,er )alue+ use t,e follo*ing SQLstatement:

    >LTER T>!LE %e"sons >BTO_ 'CREME'T.100

  • 8/13/2019 SQL Notes For Beginners

    64/107

    To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7column %a uni(ue )alue *ill !e added automaticall#&:

    'SERT 'TO %e"sons (Fi"st'ame Last'ame)A>LBES (/La"s/ /Monsen/)

    T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column*ould !e assigned a uni(ue )alue" T,e 7R3 &E3 E'T T3Last'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66))

    T,e 0S SQL Ser)er uses t,e I/E$TITB 4e#*ord to erform an auto increment feature"

    2# default+ t,e starting )alue for I/E$TITB is 8+ and it *ill increment !# 8 for eac, ne* record"

    To s ecif# t,at t,e 7.;Id7 column s,ould start at )alue 89 and increment !# + c,ange t,eidentit# to I/E$TITB%89+ &"

    To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7column %a uni(ue )alue *ill !e added automaticall#&:

    'SERT 'TO %e"sons (Fi"st'ame Last'ame)

    A>LBES (/La"s/ /Monsen/)

    T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column*ould !e assigned a uni(ue )alue" T,e 7

  • 8/13/2019 SQL Notes For Beginners

    65/107

    S'nta/ +or &**ess

    T,e follo*ing SQL statement defines t,e 7.;Id7 column to !e an auto increment rimar# 4e#field in t,e 7.ersons7 ta!le:

    CRE>TE T>!LE %e"sons(%_ d %R M>R3 &E3 >BTO 'CREME'TLast'ame va"c a"( 66) 'OT 'BLLFi"st'ame va"c a"( 66)>dd"ess va"c a"( 66)Cit+ va"c a"( 66))

    T,e 0S Access uses t,e AUTOI$CRE0E$T 4e#*ord to erform an auto increment feature"

    2# default+ t,e starting )alue for AUTOI$CRE0E$T is 8+ and it *ill increment !# 8 for eac,ne* record"

    To s ecif# t,at t,e 7.;Id7 column s,ould start at )alue 89 and increment !# + c,ange t,eautoincrement to AUTOI$CRE0E$T%89+ &"

    To insert a ne* record into t,e 7.ersons7 ta!le+ *e *ill not ,a)e to s ecif# a )alue for t,e 7.;Id7column %a uni(ue )alue *ill !e added automaticall#&:

    'SERT 'TO %e"sons (Fi"st'ame Last'ame)

    A>LBES (/La"s/ /Monsen/)

    T,e SQL statement a!o)e *ould insert a ne* record into t,e 7.ersons7 ta!le" T,e 7.;Id7 column*ould !e assigned a uni(ue )alue" T,e 7

  • 8/13/2019 SQL Notes For Beginners

    66/107

  • 8/13/2019 SQL Notes For Beginners

    67/107

    ,HERE condition

    Note: A )ie* al*a#s s,o*s u to date data1 T,e data!ase engine recreates t,e data+ using t,e)ie* s SQL statement+ e)er# time a user (ueries a )ie*"

    SQL CR,&T, #,W ,/a$) es

    If #ou ,a)e t,e $ort,*ind data!ase #ou can see t,at it ,as se)eral )ie*s installed !# default"

    T,e )ie* 7Current .roduct List7 lists all acti)e roducts % roducts t,at are not discontinued&from t,e 7.roducts7 ta!le" T,e )ie* is created *it, t,e follo*ing SQL:

    CRE>TE A E, ICu""ent %"oduct ListJ >SSELECT %"oduct %"oduct'ame

    FROM %"oducts,HERE iscontinued.'o

    We can (uer# t,e )ie* a!o)e as follo*s:

    SELECT * FROM ICu""ent %"oduct ListJ

    Anot,er )ie* in t,e $ort,*ind sam le data!ase selects e)er# roduct in t,e 7.roducts7 ta!le*it, a unit rice ,ig,er t,an t,e a)erage unit rice:

    CRE>TE A E, I%"oducts >bove >ve"a#e %"iceJ >SSELECT %"oduct'ame Bnit%"iceFROM %"oducts,HERE Bnit%"ice9(SELECT >A:(Bnit%"ice) FROM %"oducts)

    We can (uer# t,e )ie* a!o)e as follo*s:

    SELECT * FROM I%"oducts >bove >ve"a#e %"iceJ

    Anot,er )ie* in t,e $ort,*ind data!ase calculates t,e total sale for eac, categor# in 8NN " $otet,at t,is )ie* selects its data from anot,er )ie* called 7.roduct Sales for 8NN 7:

    CRE>TE A E, ICate#o"+ Sales Fo" 144 J >SSELECT ST 'CT Cate#o"+'ame Sum(%"oductSales) >S Cate#o"+SalesFROM I%"oduct Sales ;o" 144 J:ROB% !3 Cate#o"+'ame

  • 8/13/2019 SQL Notes For Beginners

    68/107

    We can (uer# t,e )ie* a!o)e as follo*s:

    SELECT * FROM ICate#o"+ Sales Fo" 144 J

    We can also add a condition to t,e (uer#" $o* *e *ant to see t,e total sale onl# for t,e categor#72e)erages7:

    SELECT * FROM ICate#o"+ Sales Fo" 144 J,HERE Cate#o"+'ame./!eve"a#es/

    SQL U)dating a ie2

    Bou can u date a )ie* !# using t,e follo*ing s#nta':

    SQL CREATE OR RE LACE 3IEW SyntaxCRE>TE OR RE%L>CE A E, vie2_name >SSELECT column_name(s)FROM table_name,HERE condition

    $o* *e *ant to add t,e 7Categor#7 column to t,e 7Current .roduct List7 )ie*" We *ill u datet,e )ie* *it, t,e follo*ing SQL:

    CRE>TE A E, ICu""ent %"oduct ListJ >SSELECT %"oduct %"oduct'ame Cate#o"+FROM %"oducts,HERE iscontinued.'o

    SQL Dro))ing a ie2

    Bou can delete a )ie* *it, t,e /RO. MIEW command"

    SQL DRO 3IEW SyntaxRO% A E, vie2_name

  • 8/13/2019 SQL Notes For Beginners

    69/107

    SQL Date ,unctions

    SQL DatesT,e most difficult art *,en *or4ing *it, dates is to !e sure t,at t,e format of t,e date #ou

    are tr#ing to insert+ matc,es t,e format of t,e date column in t,e data!ase"

    As long as #our data contains onl# t,e date ortion+ #our (ueries *ill *or4 as e' ected"Ho*e)er+ if a time ortion is in)ol)ed+ it gets com licated"

    2efore tal4ing a!out t,e com lications of (uer#ing for dates+ *e *ill loo4 at t,e most im ortant !uilt in functions for *or4ing *it, dates"

    M'SQL Date %un*tions

    T,e follo*ing ta!le lists t,e most im ortant !uilt in date functions in 0#SQL:

    F%nction Description

    'O,() Retu"ns t e cu""ent date and time

    CBR >TE() Retu"ns t e cu""ent date

    CBRT ME() Retu"ns t e cu""ent time

    >TE() E=t"acts t e date -a"t o; a date o" datePtime e=-"ession

    E TR>CT() Retu"ns a sin#le -a"t o; a datePtime

    >TE_> () >dds a s-eci;ied time inte"val to a date

    >TE_SB!() Subt"acts a s-eci;ied time inte"val ;"om a date

    >TE FF() Retu"ns t e numbe" o; da+s bet2een t2o dates

    >TE_FORM>T() is-la+s datePtime data in di;;e"ent ;o"mats

    SQL Ser5er Date %un*tions

    http://www.w3schools.com/sql/func_now.asphttp://www.w3schools.com/sql/func_curdate.asphttp://www.w3schools.com/sql/func_curtime.asphttp://www.w3schools.com/sql/func_date.asphttp://www.w3schools.com/sql/func_extract.asphttp://www.w3schools.com/sql/func_date_add.asphttp://www.w3schools.com/sql/func_date_sub.asphttp://www.w3schools.com/sql/func_datediff_mysql.asphttp://www.w3schools.com/sql/func_date_format.asphttp://www.w3schools.com/sql/func_now.asphttp://www.w3schools.com/sql/func_curdate.asphttp://www.w3schools.com/sql/func_curtime.asphttp://www.w3schools.com/sql/func_date.asphttp://www.w3schools.com/sql/func_extract.asphttp://www.w3schools.com/sql/func_date_add.asphttp://www.w3schools.com/sql/func_date_sub.asphttp://www.w3schools.com/sql/func_datediff_mysql.asphttp://www.w3schools.com/sql/func_date_format.asp
  • 8/13/2019 SQL Notes For Beginners

    70/107

    T,e follo*ing ta!le lists t,e most im ortant !uilt in date functions in SQL Ser)er:

    F%nction Description

    :ET >TE() Retu"ns t e cu""ent date and time

    >TE%>RT() Retu"ns a sin#le -a"t o; a datePtime

    >TE> () >dds o" subt"acts a s-eci;ied time inte"val ;"om a date

    >TE FF() Retu"ns t e time bet2een t2o dates

    CO'AERT() is-la+s datePtime data in di;;e"ent ;o"mats

    SQL Date Data T')es

    M'SQL comes *it, t,e follo*ing data t# es for storing a date or a date5time )alue in t,edata!ase:

    >TE Q ;o"mat 3333QMMQ >TET ME Q ;o"mat 3333QMMQ HH MM SS

    T MEST>M% Q ;o"mat 3333QMMQ HH MM SS

    3E>R Q ;o"mat 3333 o" 33

    SQL Ser5er comes *it, t,e follo*ing data t# es for storing a date or a date5time )alue in t,edata!ase:

    >TE Q ;o"mat 3333QMMQ >TET ME Q ;o"mat 3333QMMQ HH MM SS

    SM>LL >TET ME Q ;o"mat 3333QMMQ HH MM SS

    T MEST>M% Q ;o"mat a uni7ue numbe"

    Note: T,e date t# es are c,osen for a column *,en #ou create a ne* ta!le in #our data!ase1

  • 8/13/2019 SQL Notes For Beginners

    71/107

    Assume *e ,a)e t,e follo*ing 7Orders7 ta!le:

    OrderId Prod%ctName OrderDate

    1 :eitost 00 Q11Q11

    Camembe"t %ie""ot 00 Q11Q04

    $ Mo a"ella di :iovanni 00 Q11Q11

    @ Masca"-one Fabioli 00 Q10Q 4

    $o* *e *ant to select t,e records *it, an Order/ate of 7699P 88 887 from t,e ta!le a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT * FROM O"de"s ,HERE O"de" ate./ 00 Q11Q11/

    T,e result set *ill loo4 li4e t,is:

    OrderId Prod%ctName OrderDate

    1 :eitost 00 Q11Q11

    $ Mo a"ella di :iovanni 00 Q11Q11

    $o*+ assume t,at t,e 7Orders7 ta!le loo4s li4e t,is %notice t,e time com onent in t,e7Order/ate7 column&:

    OrderId Prod%ctName OrderDate

    1 :eitost 00 Q11Q11 1$ $ @@

    Camembe"t %ie""ot 00 Q11Q04 16 @6 1

    $ Mo a"ella di :iovanni 00 Q11Q11 11 1 01

    @ Masca"-one Fabioli 00 Q10Q 4 1@ 65 64

    If *e use t,e same SELECT statement as a!o)e:

    SELECT * FROM O"de"s ,HERE O"de" ate./ 00 Q11Q11/

  • 8/13/2019 SQL Notes For Beginners

    72/107

    *e *ill get no result1 T,is is !ecause t,e (uer# is loo4ing onl# for dates *it, no time ortion"

    Ti): If #ou *ant to 4ee #our (ueries sim le and eas# to maintain+ do not allo* timecom onents in #our dates1

    SQL N%LL 3alues

    $ULL )alues re resent missing un4no*n data"

    2# default+ a ta!le column can ,old $ULL )alues"

    T,is c,a ter *ill e' lain t,e IS $ULL and IS $OT $ULL o erators"

    SQL NULL a ues

    If a column in a ta!le is o tional+ *e can insert a ne* record or u date an e'isting record *it,outadding a )alue to t,is column" T,is means t,at t,e field *ill !e sa)ed *it, a $ULL )alue"

    $ULL )alues are treated differentl# from ot,er )alues"

    $ULL is used as a lace,older for un4no*n or ina lica!le )alues"

    Note: It is not ossi!le to com are $ULL and 9 t,e# are not e(ui)alent"

    SQL Wor=ing 2ith NULL a ues

    Loo4 at t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Stavan#e"

    Su ose t,at t,e 7Address7 column in t,e 7.ersons7 ta!le is o tional" T,is means t,at if *einsert a record *it, no )alue for t,e 7Address7 column+ t,e 7Address7 column *ill !e sa)ed *it,a $ULL )alue"

  • 8/13/2019 SQL Notes For Beginners

    73/107

    Ho* can *e test for $ULL )alues

    It is not ossi!le to test for $ULL )alues *it, com arison o erators+ suc, as @+ >+ or >?"

    We *ill ,a)e to use t,e IS $ULL and IS $OT $ULL o erators instead"

    SQL #S NULL

    Ho* do *e select onl# t,e records *it, $ULL )alues in t,e 7Address7 column

    We *ill ,a)e to use t,e IS $ULL o erator:

    SELECT Last'ame Fi"st'ame >dd"ess FROM %e"sons,HERE >dd"ess S 'BLL

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName Address

    Hansen Ola

    %ette"sen &a"i

    Ti): Al*a#s use IS $ULL to loo4 for $ULL )alues"

    SQL #S N T NULL

    Ho* do *e select onl# t,e records *it, no $ULL )alues in t,e 7Address7 column

    We *ill ,a)e to use t,e IS $OT $ULL o erator:

    SELECT Last'ame Fi"st'ame >dd"ess FROM %e"sons,HERE >dd"ess S 'OT 'BLL

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName Address

    Svendson Tove !o"#vn $

  • 8/13/2019 SQL Notes For Beginners

    74/107

    In t,e ne't c,a ter *e *ill loo4 at t,e IS$ULL%&+ $ML%&+ I

  • 8/13/2019 SQL Notes For Beginners

    75/107

    ra* e

    Oracle does not ,a)e an IS$ULL%& function" Ho*e)er+ *e can use t,e $ML%& function toac,ie)e t,e same result:

    SELECT %"oduct'ame Bnit%"ice*(Bnits nStoc< 'AL(BnitsOnO"de" 0))FROM %"oducts

    M'SQL

    0#SQL does ,a)e an IS$ULL%& function" Ho*e)er+ it *or4s a little !it different from0icrosoft s IS$ULL%& function"

    In 0#SQL *e can use t,e Illo2s 2 ole numbe"s ;"om 0 to 66 1 b+te

  • 8/13/2019 SQL Notes For Beginners

    76/107

    nte#e" >llo2s 2 ole numbe"s bet2een Q$ 5 and $ 5 b+tes

    Lon# >llo2s 2 ole numbe"s bet2een Q 1@ @ $ 5@ and 1@ @ $ 5@ @ b+tes

    Sin#le Sin#le -"ecision ;loatin#Q-oint ,ill andle most decimals @ b+tes

    ouble ouble -"ecision ;loatin#Q-oint ,ill andle most decimals b+tes

    Cu""enc+ Bse ;o" cu""enc+ Holds u- to 16 di#its o; 2 ole dolla"s -lus @decimal -laces Tip) 3ou can c oose 2 ic count"+/s cu""enc+ touse

    b+tes

    >uto'umbe" >uto'umbe" ;ields automaticall+ #ive eac "eco"d its o2n numbe"usuall+ sta"tin# at 1

    @ b+tes

    atePTime Bse ;o" dates and times b+tes

    3esP'o > lo#ical ;ield can be dis-la+ed as 3esP'o T"uePFalse o" OnPO;;n code use t e constants T"ue and False (e7uivalent to Q1 and 0)

    Note) 'ull values a"e not allo2ed in 3esP'o ;ields

    1 bit

    Ole Ob ect Can sto"e -ictu"es audio video o" ot e" !LO!s (!ina"+ La"#eO! ects)

    u- to1:!

    H+-e"lin< Contain linR(si e) Holds a ;i=ed len#t st"in# (can contain lette"s numbe"s and s-ecialc a"acte"s) T e ;i=ed si e is s-eci;ied in -a"ent esis Can sto"e u- to 66c a"acte"s

    A>RCH>R(si e) Holds a va"iable len#t st"in# (can contain lette"s numbe"s and s-ecial

  • 8/13/2019 SQL Notes For Beginners

    77/107

    c a"acte"s) T e ma=imum si e is s-eci;ied in -a"ent esis Can sto"e u- to66 c a"acte"s Note) ; +ou -ut a #"eate" value t an 66 it 2ill be conve"ted

    to a TE T t+-e

    T '3TE T Holds a st"in# 2it a ma=imum len#t o; 66 c a"acte"s

    TE T Holds a st"in# 2it a ma=imum len#t o; 56 6$6 c a"acte"s

    !LO! Fo" !LO!s (!ina"+ La"#e O! ects) Holds u- to 56 6$6 b+tes o; data

    ME BMTE T Holds a st"in# 2it a ma=imum len#t o; 15 16 c a"acte"s

    ME BM!LO! Fo" !LO!s (!ina"+ La"#e O! ects) Holds u- to 15 16 b+tes o; data

    LO':TE T Holds a st"in# 2it a ma=imum len#t o; @ 4@ 45 46 c a"acte"s

    LO':!LO! Fo" !LO!s (!ina"+ La"#e O! ects) Holds u- to @ 4@ 45 46 b+tes o; data

    E'BM(= + etc ) Let +ou ente" a list o; -ossible values 3ou can list u- to 566$6 values in anE'BM list ; a value is inse"ted t at is not in t e list a blan< value 2ill beinse"ted

    Note: T,e )alues are sorted in t,e order #ou enter t,em"

    Bou enter t,e ossi!le )alues in t,is format: E$U0% K + B + &SET Simila" to E'BM e=ce-t t at SET ma+ contain u- to 5@ list items and can

    sto"e mo"e t an one c oice

    Nu$ber t')es:

    Data type Description

    T '3 'T(si e) Q1 to 1 no"mal 0 to 66 B'S :'E * T e ma=imum numbe" o; di#itsma+ be s-eci;ied in -a"ent esis

    SM>LL 'T(si e) Q$ 5 to $ 5 no"mal 0 to 566$6 B'S :'E * T e ma=imum numbe" o;di#its ma+ be s-eci;ied in -a"ent esis

    ME BM 'T(si e) Q $ 50 to $ 50 no"mal 0 to 15 16 B'S :'E * T e ma=imumnumbe" o; di#its ma+ be s-eci;ied in -a"ent esis

    'T(si e) Q 1@ @ $5@ to 1@ @ $5@ no"mal 0 to @ 4@45 46 B'S :'E * T ema=imum numbe" o; di#its ma+ be s-eci;ied in -a"ent esis

    ! : 'T(si e) Q4 $$ 0$5 6@ 6 0 to 4 $$ 0$5 6@ 6 0 no"mal 0 to1 @@5 @@0 $ 04661516 B'S :'E * T e ma=imum numbe" o; di#its ma+

  • 8/13/2019 SQL Notes For Beginners

    78/107

  • 8/13/2019 SQL Notes For Beginners

    79/107

    =E)en if /ATETI0E and TI0ESTA0. return t,e same format+ t,e# *or4 )er# differentl#" Inan I$SERT or U./ATE (uer#+ t,e TI0ESTA0. automaticall# set itself to t,e current date andtime" TI0ESTA0. also acce ts )arious formats+ li4e BBBB00//HH00SS+BB00//HH00SS+ BBBB00//+ or BB00//"

    SQL Ser5er Data T')es

    Chara*ter strings:

    Data type Description &tora(e

    c a"(n) Fi=edQlen#t c a"acte" st"in# Ma=imum 000 c a"acte"s n

    va"c a"(n) Aa"iableQlen#t c a"acte" st"in# Ma=imum 000 c a"acte"s

    va"c a"(ma=) Aa"iableQlen#t c a"acte" st"in# Ma=imum 1 0 $ @1 @c a"acte"s

    te=t Aa"iableQlen#t c a"acte" st"in# Ma=imum :! o; te=t data

    Uni*ode strings:

    Data type Description &tora(e

    nc a"(n) Fi=edQlen#t Bnicode data Ma=imum @ 000 c a"acte"s

    nva"c a"(n) Aa"iableQlen#t Bnicode data Ma=imum @ 000 c a"acte"s

    nva"c a"(ma=) Aa"iableQlen#t Bnicode data Ma=imum 6$5 0 41 c a"acte"s

    nte=t Aa"iableQlen#t Bnicode data Ma=imum :! o; te=t data

    Binar' t')es:

    Data type Description &tora(e

    bit >llo2s 0 1 o" 'BLL

    bina"+(n) Fi=edQlen#t bina"+ data Ma=imum 000 b+tes

    va"bina"+(n) Aa"iableQlen#t bina"+ data Ma=imum 000 b+tes

    va"bina"+(ma=) Aa"iableQlen#t bina"+ data Ma=imum :!

  • 8/13/2019 SQL Notes For Beginners

    80/107

    ima#e Aa"iableQlen#t bina"+ data Ma=imum :!

    Nu$ber t')es:

    Data type Description &tora(e

    tin+int >llo2s 2 ole numbe"s ;"om 0 to 66 1 b+te

    smallint >llo2s 2 ole numbe"s bet2een Q$ 5 and $ 5 b+tes

    int >llo2s 2 ole numbe"s bet2een Q 1@ @ $ 5@ and 1@ @ $ 5@ @ b+tes

    bi#int >llo2s 2 ole numbe"s bet2een Q4 $ $ 0$5 6@ 6 0 and4 $ $ 0$5 6@ 6 0

    b+tes

    decimal(- s) Fi=ed -"ecision and scale numbe"s

    Allo*s num!ers from 89 3P V8 to 89 3P 8"

    T,e arameter indicates t,e ma'imum total num!er of digits t,atcan !e stored %!ot, to t,e left and to t,e rig,t of t,e decimal oint&"

    must !e a )alue from 8 to 3P" /efault is 8P"

    T,e s arameter indicates t,e ma'imum num!er of digits stored tot,e rig,t of t,e decimal oint" s must !e a )alue from 9 to " /efault)alue is 9

    6Q1b+tes

    nume"ic(- s) Fi=ed -"ecision and scale numbe"s

    Allo*s num!ers from 89 3P V8 to 89 3P 8"

    T,e arameter indicates t,e ma'imum total num!er of digits t,atcan !e stored %!ot, to t,e left and to t,e rig,t of t,e decimal oint&"

    must !e a )alue from 8 to 3P" /efault is 8P"

    T,e s arameter indicates t,e ma'imum num!er of digits stored tot,e rig,t of t,e decimal oint" s must !e a )alue from 9 to " /efault)alue is 9

    6Q1b+tes

    smallmone+ Moneta"+ data ;"om Q 1@ @ $5@ to 1@ @ $5@ @ b+tes

    mone+ Moneta"+ data ;"om Q4 $$ 0$ 5 6 @ 6 0 to4 $$ 0$ 5 6 @ 6 0

    b+tes

    ;loat(n) Floatin# -"ecision numbe" data ;"om Q1 4E $0 to 1 4E $0

    T,e n arameter indicates *,et,er t,e field s,ould ,old or P

    @ o" b+tes

  • 8/13/2019 SQL Notes For Beginners

    81/107

    !#tes" float%6 & ,olds a !#te field and float% 3& ,olds an P !#tefield" /efault )alue of n is 3"

    "eal Floatin# -"ecision numbe" data ;"om Q$ @0E $ to $ @0E $ @ b+tes

    Date t')es:

    Data type Description &tora(e

    datetime F"om Danua"+ 1 1 6$ to ecembe" $1 4444 2it an accu"ac+ o;$ $$ milliseconds

    b+tes

    datetime F"om Danua"+ 1 0001 to ecembe" $1 4444 2it an accu"ac+ o;100 nanoseconds

    5Q b+tes

    smalldatetime F"om Danua"+ 1 1400 to Dune 5 0 4 2it an accu"ac+ o; 1minute

    @ b+tes

    date Sto"e a date onl+ F"om Danua"+ 1 0001 to ecembe" $1 4444 $ b+tes

    time Sto"e a time onl+ to an accu"ac+ o; 100 nanoseconds $Q6 b+tes

    datetimeo;;set T e same as datetime 2it t e addition o; a time one o;;set Q10b+tes

    timestam- Sto"es a uni7ue numbe" t at #ets u-dated eve"+ time a "o2 #etsc"eated o" modi;ied T e timestam- value is based u-on an inte"nalcloc< and does not co""es-ond to "eal time Eac table ma+ aveonl+ one timestam- va"iable

    ther data t')es:

    Data type Description

    s7l_va"iant Sto"es u- to 000 b+tes o; data o; va"ious data t+-es e=ce-t te=t nte=t andtimestam-

    uni7ueidenti;ie" Sto"es a #loball+ uni7ue identi;ie" (:B )

    =ml Sto"es ML ;o"matted data Ma=imum :!

    cu"so" Sto"es a "e;e"ence to a cu"so" used ;o" database o-e"ations

    table Sto"es a "esultQset ;o" late" -"ocessin#

  • 8/13/2019 SQL Notes For Beginners

    82/107

    SQL ,unctions

    SQL ,as man# !uilt in functions for erforming calculations on data"

    SQL &ggregate %un*tions

    SQL aggregate functions return a single )alue+ calculated from )alues in a column"

    Useful aggregate functions:

    >A:() Q Retu"ns t e ave"a#e value COB'T() Q Retu"ns t e numbe" o; "o2s

    F RST() Q Retu"ns t e ;i"st value

    L>ST() Q Retu"ns t e last value

    M> () Q Retu"ns t e la"#est value

    M '() Q Retu"ns t e smallest value

    SBM() Q Retu"ns t e sum

    SQL S*a ar +un*tions

    SQL scalar functions return a single )alue+ !ased on t,e in ut )alue"

    Useful scalar functions:

    BC>SE() Q Conve"ts a ;ield to u--e" case LC>SE() Q Conve"ts a ;ield to lo2e" case

    M () Q E=t"act c a"acte"s ;"om a te=t ;ield LE'() Q Retu"ns t e len#t o; a te=t ;ield

    ROB' () Q Rounds a nume"ic ;ield to t e numbe" o; decimals s-eci;ied

    'O,() Q Retu"ns t e cu""ent s+stem date and time

    FORM>T() Q Fo"mats o2 a ;ield is to be dis-la+ed

  • 8/13/2019 SQL Notes For Beginners

    83/107

    Ti): T,e aggregate functions and t,e scalar functions *ill !e e' lained in details in t,e ne'tc,a ters"

    SQL A3-45 ,unction

    The & ;@A %un*tion

    T,e AMJ%& function returns t,e a)erage )alue of a numeric column"

    SQL A3-45 SyntaxSELECT >A:(column_name) FROM table_name

    SQL & ;@A ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find t,e a)erage )alue of t,e 7Order.rice7 fields"

    We use t,e follo*ing SQL statement:

    SELECT >A:(O"de"%"ice) >S O"de">ve"a#e FROM O"de"s

    T,e result set *ill loo4 li4e t,is:

  • 8/13/2019 SQL Notes For Beginners

    84/107

    OrderA*era(e

    460

    $o* *e *ant to find t,e customers t,at ,a)e an Order.rice )alue ,ig,er t,an t,e a)erageOrder.rice )alue"

    We use t,e follo*ing SQL statement:

    SELECT Custome" FROM O"de"s,HERE O"de"%"ice9(SELECT >A:(O"de"%"ice) FROM O"de"s)

    T,e result set *ill loo4 li4e t,is:

    C%stomer

    Hansen

    'ilsen

    Densen

    SQL CO%NT45 ,unction

    T,e COU$T%& function returns t,e num!er of ro*s t,at matc,es a s ecified criteria"

    SQL CO%NT4column6name5 Syntax

    T,e COU$T%column;name& function returns t,e num!er of )alues %$ULL )alues *ill not !ecounted& of t,e s ecified column:

    SELECT COB'T(column_name) FROM table_name

    SQL CO%NT475 Syntax

    T,e COU$T%=& function returns t,e num!er of records in a ta!le:

    SELECT COB'T(*) FROM table_name

  • 8/13/2019 SQL Notes For Beginners

    85/107

    SQL CO%NT4DISTINCT column6name5 Syntax

    T,e COU$T%/ISTI$CT column;name& function returns t,e num!er of distinct )alues of t,es ecified column:

    SELECT COB'T( ST 'CT column_name) FROM table_name

    Note: COU$T%/ISTI$CT& *or4s *it, ORACLE and 0icrosoft SQL Ser)er+ !ut not *it,0icrosoft Access"

    SQL C UNT@*o u$n"na$eA ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to count t,e num!er of orders from 7Customer $ilsen7"

    We use t,e follo*ing SQL statement:

    SELECT COB'T(Custome") >S Custome"'ilsen FROM O"de"s,HERE Custome"./'ilsen/

    T,e result of t,e SQL statement a!o)e *ill !e 6+ !ecause t,e customer $ilsen ,as made 6 ordersin total:

    C%stomerNilsen

  • 8/13/2019 SQL Notes For Beginners

    86/107

    SQL C UNT@0A ,/a$) e

    If *e omit t,e WHERE clause+ li4e t,is:

    SELECT COB'T(*) >S 'umbe"O;O"de"s FROM O"de"s

    T,e result set *ill loo4 li4e t,is:

    N%m+erO"Orders

    5

    *,ic, is t,e total num!er of ro*s in t,e ta!le"

    SQL C UNT@D#ST#NCT *o u$n"na$eA ,/a$) e

    $o* *e *ant to count t,e num!er of uni(ue customers in t,e 7Orders7 ta!le"

    We use t,e follo*ing SQL statement:

    SELECT COB'T( ST 'CT Custome") >S 'umbe"O;Custome"s FROM O"de"s

    T,e result set *ill loo4 li4e t,is:

    N%m+erO"C%stomers

    $

    *,ic, is t,e num!er of uni(ue customers %Hansen+ $ilsen+ and Densen& in t,e 7Orders7 ta!le"

    SQL ,IRST45 ,unction

    The %#RST@A %un*tion

    T,e

  • 8/13/2019 SQL Notes For Beginners

    87/107

    SQL ,IRST45 SyntaxSELECT F RST(column_name) FROM table_name

    SQL %#RST@A ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find t,e first )alue of t,e 7Order.rice7 column"

    We use t,e follo*ing SQL statement:SELECT F RST(O"de"%"ice) >S Fi"stO"de"%"ice FROM O"de"s

    Ti): Wor4around if

  • 8/13/2019 SQL Notes For Beginners

    88/107

  • 8/13/2019 SQL Notes For Beginners

    89/107

    LastOrderPrice

    100

    SQL (A.45 ,unction

    The M& @A %un*tion

    T,e 0AK%& function returns t,e largest )alue of t,e selected column"

    SQL (A.45 Syntax

    SELECT M> (column_name) FROM table_name

    SQL M& @A ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find t,e largest )alue of t,e 7Order.rice7 column"

    We use t,e follo*ing SQL statement:

    SELECT M> (O"de"%"ice) >S La"#estO"de"%"ice FROM O"de"s

  • 8/13/2019 SQL Notes For Beginners

    90/107

    T,e result set *ill loo4 li4e t,is:

    Lar(estOrderPrice

    000

    SQL (IN45 ,unction

    The M#N@A %un*tion

    T,e 0I$%& function returns t,e smallest )alue of t,e selected column"

    SQL (IN45 SyntaxSELECT M '(column_name) FROM table_name

    SQL M#N@A ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find t,e smallest )alue of t,e 7Order.rice7 column"

    We use t,e follo*ing SQL statement:

  • 8/13/2019 SQL Notes For Beginners

    91/107

  • 8/13/2019 SQL Notes For Beginners

    92/107

    We use t,e follo*ing SQL statement:

    SELECT SBM(O"de"%"ice) >S O"de"Total FROM O"de"s

    T,e result set *ill loo4 li4e t,is:

    OrderTotal

    6 00

    SQL -RO% !" Statement

    Aggregate functions often need an added JROU. 2B statement"

    The ;R U! BY State$ent

    T,e JROU. 2B statement is used in con-unction *it, t,e aggregate functions to grou t,eresult set !# one or more columns"

    SQL -RO% !" Syntax

    SELECT column_name a##"e#ate_;unction(column_name)FROM table_name,HERE column_name o-e"ato" value:ROB% !3 column_name

    SQL ;R U! BY ,/a$) e

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

  • 8/13/2019 SQL Notes For Beginners

    93/107

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find t,e total sum %total order& of eac, customer"

    We *ill ,a)e to use t,e JROU. 2B statement to grou t,e customers"

    We use t,e follo*ing SQL statement:

    SELECT Custome" SBM(O"de"%"ice) FROM O"de"s:ROB% !3 Custome"

    T,e result set *ill loo4 li4e t,is:

    C%stomer &!,-OrderPrice.

    Hansen 000

    'ilsen 1 00

    Densen 000

    $ice1 Isn t it :&

    Let s see *,at ,a ens if *e omit t,e JROU. 2B statement:

    SELECT Custome" SBM(O"de"%"ice) FROM O"de"s

    T,e result set *ill loo4 li4e t,is:

    C%stomer &!,-OrderPrice.Hansen 6 00

    'ilsen 6 00

    Hansen 6 00

    Hansen 6 00

  • 8/13/2019 SQL Notes For Beginners

    94/107

    Densen 6 00

    'ilsen 6 00

    T,e result set a!o)e is not *,at *e *anted"

    ,/) anation o+ 2h' the abo5e S,L,CT state$ent *annot be used: T,e SELECT statementa!o)e ,as t*o columns s ecified %Customer and SU0%Order.rice&" T,e 7SU0%Order.rice&7returns a single )alue %t,at is t,e total sum of t,e 7Order.rice7 column&+ *,ile 7Customer7returns )alues %one )alue for eac, ro* in t,e 7Orders7 ta!le&" T,is *ill t,erefore not gi)e us t,ecorrect result" Ho*e)er+ #ou ,a)e seen t,at t,e JROU. 2B statement sol)es t,is ro!lem"

    ;R U! BY More Than ne Co u$nWe can also use t,e JROU. 2B statement on more t,an one column+ li4e t,is:

    SELECT Custome" O"de" ate SBM(O"de"%"ice) FROM O"de"s:ROB% !3 Custome" O"de" ate

    SQL HA3IN- Clause

    The 1& #N; C ause

    T,e HAMI$J clause *as added to SQL !ecause t,e WHERE 4e#*ord could not !e used *it,aggregate functions"

    SQL HA3IN- SyntaxSELECT column_name a##"e#ate_;unction(column_name)FROM table_name,HERE column_name o-e"ato" value:ROB% !3 column_nameH>A ': a##"e#ate_;unction(column_name) o-e"ato" value

    SQL 1& #N; ,/a$) e

  • 8/13/2019 SQL Notes For Beginners

    95/107

    We ,a)e t,e follo*ing 7Orders7 ta!le:

    O_Id OrderDate OrderPrice C%stomer

    1 00 P11P1 1000 Hansen

    00 P10P $ 1500 'ilsen

    $ 00 P04P0 00 Hansen

    @ 00 P04P0$ $00 Hansen

    6 00 P0 P$0 000 Densen

    5 00 P10P0@ 100 'ilsen

    $o* *e *ant to find if an# of t,e customers ,a)e a total order of less t,an 6999"

    We use t,e follo*ing SQL statement:

    SELECT Custome" SBM(O"de"%"ice) FROM O"de"s:ROB% !3 Custome"H>A ': SBM(O"de"%"ice)8 000

    T,e result set *ill loo4 li4e t,is:

    C%stomer &!,-OrderPrice.

    'ilsen 1 00

    $o* *e *ant to find if t,e customers 7Hansen7 or 7Densen7 ,a)e a total order of more t,an8 99"

    We add an ordinar# WHERE clause to t,e SQL statement:

    SELECT Custome" SBM(O"de"%"ice) FROM O"de"s

    ,HERE Custome"./Hansen/ OR Custome"./Densen/:ROB% !3 Custome"H>A ': SBM(O"de"%"ice)91600

    T,e result set *ill loo4 li4e t,is:

    C%stomer &!,-OrderPrice.

  • 8/13/2019 SQL Notes For Beginners

    96/107

    Hansen 000

    Densen 000

    SQL %CASE45 ,unction

    The UC&S,@A %un*tion

    T,e UCASE%& function con)erts t,e )alue of a field to u ercase"

    SQL %CASE45 SyntaxSELECT BC>SE(column_name) FROM table_name

    Syntax )or SQL Ser'erSELECT B%%ER(column_name) FROM table_name

    SQL UC&S,@A ,/a$) eWe ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select t,e content of t,e 7Last$ame7 and 7

  • 8/13/2019 SQL Notes For Beginners

    97/107

    T,e result set *ill loo4 li4e t,is:

    LastName FirstName

    H>'SE' Ola

    SAE' SO' Tove

    %ETTERSE' &a"i

    SQL LCASE45 ,unction

    The LC&S,@A %un*tion

    T,e LCASE%& function con)erts t,e )alue of a field to lo*ercase"

    SQL LCASE45 SyntaxSELECT LC>SE(column_name) FROM table_name

    Syntax )or SQL Ser'erSELECT LO,ER(column_name) FROM table_name

    SQL LC&S,@A ,/a$) e

    We ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

  • 8/13/2019 SQL Notes For Beginners

    98/107

    $o* *e *ant to select t,e content of t,e 7Last$ame7 and 7

  • 8/13/2019 SQL Notes For Beginners

    99/107

    We ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to e'tract t,e first four c,aracters of t,e 7Cit#7 column a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT M (Cit+ 1 @) as SmallCit+ FROM %e"sons

    T,e result set *ill loo4 li4e t,is:

    &mallCity

    Sand

    Sand

    Stav

    SQL LEN45 ,unction

    The L,N@A %un*tion

    T,e LE$%& function returns t,e lengt, of t,e )alue in a te't field"

    SQL LEN45 SyntaxSELECT LE'(column_name) FROM table_name

    SQL L,N@A ,/a$) e

  • 8/13/2019 SQL Notes For Beginners

    100/107

    We ,a)e t,e follo*ing 7.ersons7 ta!le:

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    $ %ette"sen &a"i Sto"#t 0 Stavan#e"

    $o* *e *ant to select t,e lengt, of t,e )alues in t,e 7Address7 column a!o)e"

    We use t,e follo*ing SELECT statement:

    SELECT LE'(>dd"ess) as Len#t O;>dd"ess FROM %e"sons

    T,e result set *ill loo4 li4e t,is:

    Len(t$O"Address

    1

    4

    4

    SQL RO%ND45 ,unction

    The R UND@A %un*tion

    T,e ROU$/%& function is used to round a numeric field to t,e num!er of decimals s ecified"

    SQL RO%ND45 SyntaxSELECT ROB' (column_name decimals) FROM table_name

    Parameter Description

    column_name Re7ui"ed T e ;ield to "ound

  • 8/13/2019 SQL Notes For Beginners

    101/107

    decimals Re7ui"ed S-eci;ies t e numbe" o; decimals to be "etu"ned

    SQL R UND@A ,/a$) e

    We ,a)e t,e follo*ing 7.roducts7 ta!le:

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10 @6

    Masca"-one 1000 # $ 65

    $ :o"#on ola 1000 # 16 5

    $o* *e *ant to dis la# t,e roduct name and t,e rice rounded to t,e nearest integer"

    We use t,e follo*ing SELECT statement:

    SELECT %"oduct'ame ROB' (Bnit%"ice 0) as Bnit%"ice FROM %"oducts

    T,e result set *ill loo4 li4e t,is:

    Prod%ctName !nitPrice

    Da"lsbe"# 10

    Masca"-one $$

    :o"#on ola 15

    SQL NOW45 ,unction

    The N W@A %un*tion

    T,e $OW%& function returns t,e current s#stem date and time"

  • 8/13/2019 SQL Notes For Beginners

    102/107

    SQL NOW45 SyntaxSELECT 'O,() FROM table_name

    SQL N W@A ,/a$) e

    We ,a)e t,e follo*ing 7.roducts7 ta!le:

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10 @6

    Masca"-one 1000 # $ 65

    $ :o"#on ola 1000 # 16 5

    $o* *e *ant to dis la# t,e roducts and rices er toda# s date"

    We use t,e follo*ing SELECT statement:

    SELECT %"oduct'ame Bnit%"ice 'o2() as %e" ate FROM %"oducts

    T,e result set *ill loo4 li4e t,is:

    Prod%ctName !nitPrice PerDate

    Da"lsbe"# 10 @6 10P P 00 11 6 0 >M

    Masca"-one $ 65 10P P 00 11 6 0 >M

    :o"#on ola 16 5 10P P 00 11 6 0 >M

    SQL ,OR(AT45 ,unction

    The % RM&T@A %un*tion

    T,e

  • 8/13/2019 SQL Notes For Beginners

    103/107

    SQL ,OR(AT45 SyntaxSELECT FORM>T(column_name ;o"mat) FROM table_name

    Parameter Description

    column_name Re7ui"ed T e ;ield to be ;o"matted

    ;o"mat Re7ui"ed S-eci;ies t e ;o"mat

    SQL % RM&T@A ,/a$) e

    We ,a)e t,e follo*ing 7.roducts7 ta!le:

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10 @6

    Masca"-one 1000 # $ 65

    $ :o"#on ola 1000 # 16 5

    $o* *e *ant to dis la# t,e roducts and rices er toda# s date %*it, toda# s date dis la#ed int,e follo*ing format 7BBBB 00 //7&"

    We use t,e follo*ing SELECT statement:

    SELECT %"oduct'ame Bnit%"ice FORM>T('o2() /3333QMMQ /) as %e" ateFROM %"oducts

    T,e result set *ill loo4 li4e t,is:

    Prod%ctName !nitPrice PerDate

    Da"lsbe"# 10 @6 00 Q10Q0

    Masca"-one $ 65 00 Q10Q0

    :o"#on ola 16 5 00 Q10Q0

  • 8/13/2019 SQL Notes For Beginners

    104/107

    SQL Quic8 Re)erence ,rom W9Sc:ools

    &/L &tatement &ynta0

    >' P OR SELECT column_name(s)FROM table_name,HERE condition>' ?OR condition

    >LTER T>!LE >LTER T>!LE table_name> column_name datat+-e

    or

    ALTER TA2LE ta!le;name/RO. COLU0$ column;name

    >S (alias) SELECT column_name >S column_aliasFROM table_name

    or

    SELECT column;name' value

    CRE>TE >T>!>SE CRE>TE >T>!>SE database_name

    CRE>TE T>!LE CRE>TE T>!LE table_name(column_name1 data_t+-ecolumn_name data_t+-ecolumn_name data_t+-e

    )

    CRE>TE ' E CRE>TE ' E inde=_nameO' table_name (column_name)

    or

  • 8/13/2019 SQL Notes For Beginners

    105/107

    CREATE U$IQUE I$/EK inde';nameO$ ta!le;name %column;name&

    CRE>TE A E, CRE>TE A E, vie2_name >SSELECT column_name(s)FROM table_name,HERE condition

    ELETE ELETE FROM table_name,HERE some_column.some_value

    or

    /ELETE !>SE RO% >T>!>SE database_name

    RO% ' E RO% ' E table_name inde=_name (SNL Se"ve")RO% ' E inde=_name O' table_name (MS >ccess)RO% ' E inde=_name ( ! PO"acle)

    >LTER T>!LE table_nameRO% ' E inde=_name (M+SNL)

    RO% T>!LE RO% T>!LE table_name

    :ROB% !3 SELECT column_name a##"e#ate_;unction(column_name)FROM table_name,HERE column_name o-e"ato" value:ROB% !3 column_name

    H>A ': SELECT column_name a##"e#ate_;unction(column_name)FROM table_name,HERE column_name o-e"ato" value:ROB% !3 column_nameH>A ': a##"e#ate_;unction(column_name) o-e"ato" value

    ' SELECT column_name(s)FROM table_name,HERE column_name

    ' (value1 value )

    'SERT 'TO 'SERT 'TO table_name

  • 8/13/2019 SQL Notes For Beginners

    106/107

    A>LBES (value1 value value$ )

    or

    I$SERT I$TO ta!le;name

    %column8+ column6+ column3+"""&MALUES %)alue8+ )alue6+ )alue3+""""&

    ''ER DO ' SELECT column_name(s)FROM table_name1

    ''ER DO ' table_nameO' table_name1 column_name.table_name column_name

    LEFT DO ' SELECT column_name(s)FROM table_name1LEFT DO ' table_nameO' table_name1 column_name.table_name column_name

    R :HT DO ' SELECT column_name(s)FROM table_name1R :HT DO ' table_nameO' table_name1 column_name.table_name column_name

    FBLL DO ' SELECT column_name(s)FROM table_name1FBLL DO ' table_nameO' table_name1 column_name.table_name column_name

    L &E SELECT column_name(s)FROM table_name,HERE column_name L &E -atte"n

    OR ER !3 SELECT column_name(s)FROM table_nameOR ER !3 column_name I>SC? ESCJ

    SELECT SELECT column_name(s)FROM table_name

    SELECT * SELECT *FROM table_name

    SELECT ST 'CT SELECT ST 'CT column_name(s)FROM table_name

    SELECT 'TO SELECT *'TO ne2_table_name I ' e=te"naldatabaseJ

  • 8/13/2019 SQL Notes For Beginners

    107/107

    FROM old_table_name

    or

    SELECT column;name%s&

    I$TO ne*;ta!le;name GI$ e'ternaldata!aseTE T>!LE table_name

    B' O' SELECT column_name(s) FROM table_name1B' O'SELECT column_name(s) FROM table_name

    B' O' >LL SELECT column_name(s) FROM table_name1B' O' >LLSELECT column_name(s) FROM table_name

    B% >TE B% >TE table_nameSET column1.value column .value,HERE some_column.some_value

    ,HERE SELECT column_name(s)FROM table_name,HERE column_name o-e"ato" value