tips fro sql

Upload: tejas-jadhav

Post on 03-Jun-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Tips Fro SQL

    1/107

    Training from W3Schools

    Introduction to SQL

    SQL is a standard language for accessing and manipulating databases.

    What is SQL?

    SQL stands for Structured Query Language

    SQL lets you access and manipulate databases

    SQL is an ANSI (American National Standards Institute) standard

    What Can SQL do?

    SQL can execute queries against a database

    SQL can retrieve data from a database

    SQL can insert records in a database

    SQL can update records in a database

    SQL can delete records from a database

    SQL can create ne databases

    SQL can create ne tables in a database

    SQL can create stored procedures in a database

    SQL can create vies in a database

    SQL can set permissions on tables! procedures! and vies

    SQL is a Standard - BUT....

    Alt"oug" SQL is an ANSI (American National Standards Institute) standard! t"ere are manydifferent versions of t"e SQL language.

  • 8/12/2019 Tips Fro SQL

    2/107

    #oever! to be compliant it" t"e ANSI standard! t"ey all support at least t"e ma$or commands

    (suc" as S%L%&'! *A'%! *%L%'%! INS%+'! ,#%+%) in a similar manner.

    Note:-ost of t"e SQL database programs also "ave t"eir on proprietary extensions in additionto t"e SQL standard

    Using SQL in Your Web Site

    'o build a eb site t"at s"os some data from a database! you ill need t"e folloing/

    An +*0-S database program (i.e. -S Access! SQL Server! -ySQL)

    A server1side scripting language! li2e # or AS

    SQL

    #'-L 3 &SS

    RDBMS

    +*0-S stands for +elational *atabase -anagement System.

    +*0-S is t"e basis for SQL! and for all modern database systems li2e -S SQL Server! I0-

    *04! 5racle! -ySQL! and -icrosoft Access.

    '"e data in +*0-S is stored in database ob$ects called tables.

    A table is a collections of related data entries and it consists of columns and ros.

    Database Tabes

    A database most often contains one or more tables. %ac" table is identified by a name (e.g.

    6&ustomers6 or 65rders6). 'ables contain records (ros) it" data.

    0elo is an example of a table called 6ersons6/

    !"#d LastNa$e %irstNa$e &ddress Cit'

    7 #ansen 5la 'imoteivn 78 Sandnes

    4 Svendson 'ove 0orgvn 49 Sandnes

    9 ettersen :ari Storgt 48 Stavanger

  • 8/12/2019 Tips Fro SQL

    3/107

    '"e table above contains t"ree records (one for eac" person) and five columns (;Id! LastName!

  • 8/12/2019 Tips Fro SQL

    4/107

    '"e **L part of SQL permits database tables to be created or deleted. It also define indexes

    (2eys)! specify lin2s beteen tables! and impose constraints beteen tables. '"e most important

    **L statements in SQL are/

    CR,&T, D&T&B&S,1 creates a ne database

    &LT,R D&T&B&S,1 modifies a database

    CR,&T, T&BL,1 creates a ne table

    &LT,R T&BL,1 modifies a table

    DR! T&BL,1 deletes a table

    CR,&T, #ND,1 creates an index (searc" 2ey)

    DR! #ND,1 deletes an index

    SQL SELECT Statement

    '"is c"apter ill explain t"e S%L%&' and t"e S%L%&' = statements.

    The SQL S,L,CT State$ent

    '"e S%L%&' statement is used to select data from a database.

    '"e result is stored in a result table! called t"e result1set.

    SQL SELECT Syntax

    SELECT column_name(s)

    FROM table_name

    and

    SELECT * FROM table_name

    Note:SQL is not case sensitive. S%L%&' is t"e same as select.

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

    '"e 6ersons6 table/

  • 8/12/2019 Tips Fro SQL

    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"

    No e ant to select t"e content of t"e columns named 6LastName6 and 6

  • 8/12/2019 Tips Fro SQL

    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

    '"is c"apter ill explain t"e S%L%&' *IS'IN&' statement.

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

    In a table! some of t"e columns may contain duplicate values. '"is is not a problem! "oever!

    sometimes you ill ant to list only t"e different (distinct) values in a table.

    '"e *IS'IN&' 2eyord can be used to return only distinct (different) values.

    SQL SELECT DISTINCT Syntax

    SELECT ST'CT column_name(s)

    FROM table_name

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

    '"e 6ersons6 table/

    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"

    No e ant to select only t"e distinct values from t"e column named 6&ity6 from t"e tableabove.

    ,e use t"e folloing S%L%&' statement/

  • 8/12/2019 Tips Fro SQL

    7/107

    SELECT ST'CT Cit+ FROM %e"sons

    '"e result1set ill loo2 li2e t"is/

    City

    Sandnes

    Stavan#e"

    SQL WHEE Clause

    '"e ,#%+% clause is used to filter records.

    The W1,R, Cause

    '"e ,#%+% clause is used to extract only t"ose records t"at fulfill a specified criterion.

    SQL WHEE Syntax

    SELECT column_name(s)

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

    W1,R, Cause ,/a$)e

    '"e 6ersons6 table/

    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/12/2019 Tips Fro SQL

    8/107

    No e ant to select only t"e persons living in t"e city 6Sandnes6 from t"e table above.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+./Sandnes/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Quotes &round Te/t %ieds

    SQL uses single quotes around text values (most database systems ill also accept doublequotes).

    Alt"oug"! numeric values s"ould not be enclosed in quotes.

  • 8/12/2019 Tips Fro SQL

    9/107

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

    )erators &o2ed in the W1,R, Cause,it" t"e ,#%+% clause! t"e folloing operators can be used/

    Operator Description

    . E7ual

    89 'ot e7ual

    9 :"eate" tan

    8 Less tan

    9. :"eate" tan o" e7ual

    8. Less tan o" e7ual

    !ET,EE

    '!et2een an inclusive "an#e

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

    ' ; +ou ? operator may be ritten as @

    SQL !ND " # #$erators

    '"e AN* 5+ operators are used to filter records based on more t"an one condition.

    The &ND 3 R )erators

    '"e AN* operator displays a record if bot" t"e first condition and t"e second condition is true.

  • 8/12/2019 Tips Fro SQL

    10/107

    '"e 5+ operator displays a record if eit"er t"e first condition or t"e second condition is true.

    &ND )erator ,/a$)e

    '"e 6ersons6 table/

    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"

    No e ant to select only t"e persons it" t"e first name equal to 6'ove6 AN* t"e last name

    equal to 6Svendson6/

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Fi"st'ame./Tove/

    >' Last'ame./Svendson/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    R )erator ,/a$)e

    No e ant to select only t"e persons it" t"e first name equal to 6'ove6 5+ t"e first name

    equal to 65la6/

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Fi"st'ame./Tove/

    OR Fi"st'ame./Ola/

  • 8/12/2019 Tips Fro SQL

    11/107

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Co$bining &ND 3 R

    Bou can also combine AN* and 5+ (use parent"esis to form complex expressions).

    No e ant to select only t"e persons it" t"e last name equal to 6Svendson6 AN* t"e firstname equal to 6'ove6 5+ to 65la6/

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons ,HERE

    Last'ame./Svendson/

    >' (Fi"st'ame./Tove/ OR Fi"st'ame./Ola/)

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    SQL #DE %& 'ey(ord

    '"e 5+*%+ 0B 2eyord is used to sort t"e result1set.

    The RD,R BY (e'2ord

    '"e 5+*%+ 0B 2eyord is used to sort t"e result1set by a specified column.

    '"e 5+*%+ 0B 2eyord sort t"e records in ascending order by default.

    If you ant to sort t"e records in a descending order! you can use t"e *%S& 2eyord.

  • 8/12/2019 Tips Fro SQL

    12/107

    SQL #DE %& Syntax

    SELECT column_name(s)

    FROM table_name

    ORER !3 column_name(s) >SC?ESC

    RD,R BY ,/a$)e

    '"e 6ersons6 table/

    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"

    No e ant to select all t"e persons from t"e table above! "oever! e ant to sort t"e persons

    by t"eir last name.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e 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/12/2019 Tips Fro SQL

    13/107

    No e ant to select all t"e persons from t"e table above! "oever! e ant to sort t"e persons

    descending by t"eir last name.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e 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 INSET INT# Statement

    '"e INS%+' IN'5 statement is used to insert ne records in a table.

    The #NS,RT #NT State$ent

    '"e INS%+' IN'5 statement is used to insert a ne ro in a table.

    SQL INSET INT# Syntax

    It is possible to rite t"e INS%+' IN'5 statement in to forms.

    '"e first form doesnCt specify t"e column names "ere t"e data ill be inserted! only t"eir

    values/

    'SERT 'TO table_name

    A>LBES (value1 value value$)

  • 8/12/2019 Tips Fro SQL

    14/107

    '"e second form specifies bot" t"e column names and t"e values to be inserted/

    'SERT 'TO table_name (column1 column column$)

    A>LBES (value1 value value$)

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

    ,e "ave t"e folloing 6ersons6 table/

    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"

    No e ant to insert a ne ro in t"e 6ersons6 table.

    ,e use t"e folloing SQL statement/

    'SERT 'TO %e"sons

    A>LBES (@/'ilsen/ /Doan/ /!a

  • 8/12/2019 Tips Fro SQL

    15/107

    '"e folloing SQL statement ill add a ne ro! but only add data in t"e 6;Id6! 6LastName6

    and t"e 6

  • 8/12/2019 Tips Fro SQL

    16/107

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

    '"e 6ersons6 table/

    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 Doan !a

  • 8/12/2019 Tips Fro SQL

    17/107

    B%>TE %e"sons

    SET >dd"ess./'issestien 5/ Cit+./Sandnes/

    '"e 6ersons6 table ould "ave loo2ed li2e 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 Doan 'issestien 5 Sandnes

    6 Tessem Da

  • 8/12/2019 Tips Fro SQL

    18/107

  • 8/12/2019 Tips Fro SQL

    19/107

    Note:0e very careful "en deleting records. Bou cannot undo t"is statement

    SQL T#* Clause

    The T! Cause

    '"e '5 clause is used to specify t"e number of records to return.

    '"e '5 clause can be very useful on large tables it" t"ousands of records. +eturning a largenumber of records can impact on performance.

    Note:Not all database systems support t"e '5 clause.

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

    FROM table_name

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

    ,ySQL Syntax

    SELECT column_name(s)

    FROM table_name

    LMT numbe"

    Exam$le

    SELECT *

    FROM %e"sons

    LMT 6

    #racle Syntax

    SELECT column_name(s)

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

    Exam$le

    SELECT *

    FROM %e"sons ,HERE RO,'BM 8.6

  • 8/12/2019 Tips Fro SQL

    20/107

    SQL T! ,/a$)e

    '"e 6ersons6 table/

    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"

    No e ant to select only t"e to first records in t"e table above.

    ,e use t"e folloing S%L%&' statement/

    SELECT TO% * FROM %e"sons

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

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

    '"e 6ersons6 table/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

  • 8/12/2019 Tips Fro SQL

    21/107

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

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

    No e ant to select only E8F of t"e records in t"e table above.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    SQL LI'E #$erator

    '"e LI:% operator is used in a ,#%+% clause to searc" for a specified pattern in a column.

    The L#(, )erator

    '"e LI:% operator is used to searc" for a specified pattern in a column.

    SQL LI'E Syntax

    SELECT column_name(s)

    FROM table_name

    ,HERE column_name L&E -atte"n

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

    '"e 6ersons6 table/

  • 8/12/2019 Tips Fro SQL

    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"

    No e ant to select t"e persons living in a city t"at starts it" 6s6 from t"e table above.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+ L&E /sG/

    '"e 6F6 sign can be used to define ildcards (missing letters in t"e pattern) bot" before andafter t"e pattern.

    '"e result1set ill loo2 li2e 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"

    Next! e ant to select t"e persons living in a city t"at ends it" an 6s6 from t"e 6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+ L&E /Gs/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

  • 8/12/2019 Tips Fro SQL

    23/107

    Next! e ant to select t"e persons living in a city t"at contains t"e pattern 6tav6 from t"e

    6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

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

    It is also possible to select t"e persons living in a city t"at N5' contains t"e pattern 6tav6 from

    t"e 6ersons6 table! by using t"e N5' 2eyord.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+ 'OT L&E /GtavG/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    SQL Wildcards

    SQL ildcards can be used "en searc"ing for data in a database.

    SQL Wid*ards

    SQL ildcards can substitute for one or more c"aracters "en searc"ing for data in a database.

  • 8/12/2019 Tips Fro SQL

    24/107

    SQL ildcards must be used it" t"e SQL LI:% operator.

    ,it" SQL! t"e folloing ildcards can be used/

    Wildcard Description

    G > substitute ;o" e"o o" mo"e ca"acte"s

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

    Ica"listJ >n+ sin#le ca"acte" in ca"list

    IKca"listJ

    or

    Gc"arlistH

    >n+ sin#le ca"acte" not in ca"list

    SQL Wid*ard ,/a$)es

    ,e "ave t"e folloing 6ersons6 table/

    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 Wid*ard

    No e ant to select t"e persons living in a city t"at starts it" 6sa6 from t"e 6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+ L&E /saG/

    '"e result1set ill loo2 li2e t"is/

  • 8/12/2019 Tips Fro SQL

    25/107

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Next! e ant to select t"e persons living in a city t"at contains t"e pattern 6nes6 from t"e

    6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Cit+ L&E /GnesG/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Svendson Tove !o"#vn $ Sandnes

    Using the " Wid*ardNo e ant to select t"e persons it" a first name t"at starts it" any c"aracter! folloed by6la6 from t"e 6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Fi"st'ame L&E /_la/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

  • 8/12/2019 Tips Fro SQL

    26/107

    Next! e ant to select t"e persons it" a last name t"at starts it" 6S6! folloed by any

    c"aracter! folloed by 6end6! folloed by any c"aracter! folloed by 6on6 from t"e 6ersons6

    table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Last'ame L&E /S_end_on/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

    Using the 7*harist8 Wid*ard

    No e ant to select t"e persons it" a last name t"at starts it" 6b6 or 6s6 or 6p6 from t"e

    6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

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

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    Svendson Tove !o"#vn $ Sandnes

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

    Next! e ant to select t"e persons it" a last name t"at do not start it" 6b6 or 6s6 or 6p6 from

    t"e 6ersons6 table.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

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

  • 8/12/2019 Tips Fro SQL

    27/107

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    SQL IN #$erator

    The #N )erator

    '"e IN operator allos you to specify multiple values in a ,#%+% clause.

    SQL IN Syntax

    SELECT column_name(s)

    FROM table_name

    ,HERE column_name ' (value1value)

    #N )erator ,/a$)e

    '"e 6ersons6 table/

    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"

    No e ant to select t"e persons it" a last name equal to 6#ansen6 or 6ettersen6 from t"etable above.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons

    ,HERE Last'ame ' (/Hansen//%ette"sen/)

  • 8/12/2019 Tips Fro SQL

    28/107

    '"e result1set ill loo2 li2e 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 #$erator

    '"e 0%',%%N operator is used in a ,#%+% clause to select a range of data beteen to

    values.

    The B,TW,,N )erator

    '"e 0%',%%N operator selects a range of data beteen to values. '"e values can be

    numbers! text! or dates.

    SQL %ETWEEN Syntax

    SELECT column_name(s)

    FROM table_name

    ,HERE column_name

    !ET,EE' value1 >' value

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

    '"e 6ersons6 table/

    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/12/2019 Tips Fro SQL

    29/107

    No e ant to select t"e persons it" a last name alp"abetically beteen 6#ansen6 and

    6ettersen6 from t"e table above.

    ,e use t"e folloing S%L%&' statement/

    SELECT * FROM %e"sons,HERE Last'ame

    !ET,EE' /Hansen/ >' /%ette"sen/

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

    1 Hansen Ola Timoteivn 10 Sandnes

    Note:'"e 0%',%%N operator is treated differently in different databases.

    In some databases! persons it" t"e LastName of 6#ansen6 or 6ettersen6 ill not be listed!

    because t"e 0%',%%N operator only selects fields t"at are beteen and excluding t"e testvalues).

    In ot"er databases! persons it" t"e LastName of 6#ansen6 or 6ettersen6 ill be listed! because

    t"e 0%',%%N operator selects fields t"at are beteen and including t"e test values).

    And in ot"er databases! persons it" t"e LastName of 6#ansen6 ill be listed! but 6ettersen6ill not be listed (li2e t"e example above)! because t"e 0%',%%N operator selects fields

    beteen t"e test values! including t"e first test value and excluding t"e last test value.

    '"erefore/ &"ec2 "o your database treats t"e 0%',%%N operator.

    ,/a$)e 9

    'o display t"e persons outside t"e range in t"e previous example! use N5' 0%',%%N/

    SELECT * FROM %e"sons

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

    '"e result1set ill loo2 li2e t"is/

    P_Id LastName FirstName Address City

  • 8/12/2019 Tips Fro SQL

    30/107

    Svendson Tove !o"#vn $ Sandnes

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

    SQL !lias

    ,it" SQL! an alias name can be given to a table or to a column.

    SQL &ias

    Bou can give a table or a column anot"er name by using an alias. '"is can be a good t"ing to doif you "ave very long or complex table names or column names.

    An alias name could be anyt"ing! but usually it is s"ort.

    SQL !lias Syntax for Ta-les

    SELECT column_name(s)

    FROM table_name

    >S alias_name

    SQL !lias Syntax for ColumnsSELECT column_name >S alias_name

    FROM table_name

    &ias ,/a$)e

    Assume e "ave a table called 6ersons6 and anot"er table called 6roduct;5rders6. ,e ill

    give t"e table aliases of 6p6 and 6po6 respectively.

    No e ant to list all t"e orders t"at 65la #ansen6 is responsible for.

    ,e use t"e folloing S%L%&' statement/

    SELECT -oO"de" -Last'ame -Fi"st'ame

    FROM %e"sons >S -

  • 8/12/2019 Tips Fro SQL

    31/107

    %"oduct_O"de"s >S -o

    ,HERE -Last'ame./Hansen/ >' -Fi"st'ame./Ola/

    '"e same S%L%&' statement it"out aliases/

    SELECT %"oduct_O"de"sO"de" %e"sonsLast'ame %e"sonsFi"st'ame

    FROM %e"sons

    %"oduct_O"de"s

    ,HERE %e"sonsLast'ame./Hansen/ >' %e"sonsFi"st'ame./Ola/

    As youCll see from t"e to S%L%&' statements above aliases can ma2e queries easier to bot"rite and to read.

    SQL .oins

    SQL $oins are used to query data from to or more tables! based on a relations"ip beteen

    certain columns in t"ese tables.

    SQL #N

    '"e D5IN 2eyord is used in an SQL statement to query data from to or more tables! based on

    a relations"ip beteen certain columns in t"ese tables.

    'ables in a database are often related to eac" ot"er it" 2eys.

    A primary 2ey is a column (or a combination of columns) it" a unique value for eac" ro.

    %ac" primary 2ey value must be unique it"in t"e table. '"e purpose is to bind data toget"er!

    across tables! it"out repeating all of t"e data in every table.

    Loo2 at t"e 6ersons6 table/

    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/12/2019 Tips Fro SQL

    32/107

    Note t"at t"e 6;Id6 column is t"e primary 2ey in t"e 6ersons6 table. '"is means t"at noto

    ros can "ave t"e same ;Id. '"e ;Id distinguis"es to persons even if t"ey "ave t"e same

    name.

    Next! e "ave t"e 65rders6 table/

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@5@ 16

    Note t"at t"e 65;Id6 column is t"e primary 2ey in t"e 65rders6 table and t"at t"e 6;Id6 column

    refers to t"e persons in t"e 6ersons6 table it"out using t"eir names.

    Notice t"at t"e relations"ip beteen t"e to tables above is t"e 6;Id6 column.

    Di++erent SQL #Ns

    0efore e continue it" examples! e ill list t"e types of D5IN you can use! and t"edifferences beteen t"em.

    JOIN Retu"n "o2s 2en te"e is at least one matc in bot tables

    LEFT JOIN Retu"n all "o2s ;"om te le;t table even i; te"e a"e no matces in te "i#t

    table

    RIGT JOIN Retu"n all "o2s ;"om te "i#t table even i; te"e a"e no matces in te le;t

    table

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

    SQL INNE .#IN 'ey(ord

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

    '"e INN%+ D5IN 2eyord return ros "en t"ere is at least one matc" in bot" tables.

  • 8/12/2019 Tips Fro SQL

    33/107

    SQL INNE .#IN Syntax

    SELECT column_name(s)

    FROM table_name1

    ''ER DO' table_name

    O' table_name1column_name.table_namecolumn_name

    !S:INN%+ D5IN is t"e same as D5IN.

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

    '"e 6ersons6 table/

    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 65rders6 table/

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@5@ 16

    No e ant to list all t"e persons it" any orders.

    ,e use t"e folloing S%L%&' statement/

    SELECT %e"sonsLast'ame %e"sonsFi"st'ame O"de"sO"de"'o

    FROM %e"sons

    ''ER DO' O"de"s

    O' %e"sons%_d.O"de"s%_d

  • 8/12/2019 Tips Fro SQL

    34/107

    ORER !3 %e"sonsLast'ame

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    '"e INN%+ D5IN 2eyord return ros "en t"ere is at least one matc" in bot" tables. If t"ere

    are ros in 6ersons6 t"at do not "ave matc"es in 65rders6! t"ose ros ill N5' be listed.

    SQL LE/T .#IN 'ey(ord

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

    '"e L%

  • 8/12/2019 Tips Fro SQL

    35/107

    Svendson Tove !o"#vn $ Sandnes

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

    '"e 65rders6 table/

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@5@ 16

    No e ant to list all t"e persons and t"eir orders 1 if any! from t"e tables above.

    ,e use t"e folloing S%L%&' statement/

    SELECT %e"sonsLast'ame %e"sonsFi"st'ame O"de"sO"de"'o

    FROM %e"sons

    LEFT DO' O"de"sO' %e"sons%_d.O"de"s%_d

    ORER !3 %e"sonsLast'ame

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    Svendson Tove

  • 8/12/2019 Tips Fro SQL

    36/107

    '"e L%

  • 8/12/2019 Tips Fro SQL

    37/107

    @ @65 1

    6 $@5@ 16

    No e ant to list all t"e orders it" containing persons 1 if any! from t"e tables above.

    ,e use t"e folloing S%L%&' statement/

    SELECT %e"sonsLast'ame %e"sonsFi"st'ame O"de"sO"de"'o

    FROM %e"sons

    R:HT DO' O"de"s

    O' %e"sons%_d.O"de"s%_d

    ORER !3 %e"sonsLast'ame

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    $@5@

    '"e +IJ#' D5IN 2eyord returns all t"e ros from t"e rig"t table (5rders)! even if t"ere are no

    matc"es in t"e left table (ersons).

    SQL /)LL .#IN 'ey(ord

    SQL %ULL #N (e'2ord'"e

  • 8/12/2019 Tips Fro SQL

    38/107

    FBLL DO' table_name

    O' table_name1column_name.table_namecolumn_name

    SQL %ULL #N ,/a$)e

    '"e 6ersons6 table/

    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 65rders6 table/

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65 1

    @ @65 1

    6 $@5@ 16

    No e ant to list all t"e persons and t"eir orders! and all t"e orders it" t"eir persons.

    ,e use t"e folloing S%L%&' statement/

    SELECT %e"sonsLast'ame %e"sonsFi"st'ame O"de"sO"de"'o

    FROM %e"sonsFBLL DO' O"de"s

    O' %e"sons%_d.O"de"s%_d

    ORER !3 %e"sonsLast'ame

    '"e result1set ill loo2 li2e t"is/

  • 8/12/2019 Tips Fro SQL

    39/107

    LastName FirstName OrderNo

    Hansen Ola @65

    Hansen Ola @65

    %ette"sen &a"i 46

    %ette"sen &a"i @@5

    Svendson Tove

    $@5@

    '"e

  • 8/12/2019 Tips Fro SQL

    40/107

    SQL )NI#N !LL Syntax

    SELECT column_name(s) FROM table_name1

    B'O' >LL

    SELECT column_name(s) FROM table_name

    !S:'"e column names in t"e result1set of a NI5N are alays equal to t"e column names in t"e

    first S%L%&' statement in t"e NI5N.

    SQL UN#N ,/a$)e

    Loo2 at t"e folloing tables/

  • 8/12/2019 Tips Fro SQL

    41/107

    SELECT E_'ame FROM Em-lo+ees_BS>

    '"e result1set ill loo2 li2e t"is/

    E_Name

    Hansen Ola

    Svendson Tove

    Svendson Ste-en

    %ette"sen &a"i

    Tu"ne" Sall+

    &ent Cla"LL

    SELECT E_'ame FROM Em-lo+ees_BS>

    Resut

    E_Name

    Hansen Ola

    Svendson Tove

    Svendson Ste-en

    %ette"sen &a"i

  • 8/12/2019 Tips Fro SQL

    42/107

    Tu"ne" Sall+

    &ent Cla"R3 &E3

    FORE:' &E3

    CHEC&

    EF>BLT

    '"e next c"apters ill describe eac" constraint in details.

    SQL N#T N)LL Constraint

    0y default! a table column can "old NLL values.

    SQL NT NULL Constraint'"e N5' NLL constraint enforces a column to N5' accept NLL values.

    '"e N5' NLL constraint enforces a field to alays contain a value. '"is means t"at you

    cannot insert a ne record! or update a record it"out adding a value to t"is field.

    '"e folloing SQL enforces t"e 6;Id6 column and t"e 6LastName6 column to not accept

    NLL values/

    CRE>TE T>!LE %e"sons

    (%_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    )

  • 8/12/2019 Tips Fro SQL

    47/107

    SQL )NIQ)E Constraint

    SQL UN#QU, Constraint'"e NIQ% constraint uniquely identifies eac" record in a database table.

    '"e NIQ% and +I-A+B :%B constraints bot" provide a guarantee for uniqueness for a

    column or set of columns.

    A +I-A+B :%B constraint automatically "as a NIQ% constraint defined on it.

    Note t"at you can "ave many NIQ% constraints per table! but only one +I-A+B :%B

    constraint per table.

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

    '"e folloing SQL creates a NIQ% constraint on t"e 6;Id6 column "en t"e 6ersons6table is created/

    M'SQL:

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    B'NBE (%_d)

    )

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL B'NBE

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

  • 8/12/2019 Tips Fro SQL

    48/107

    )

    'o allo naming of a NIQ% constraint! and for defining a NIQ% constraint on multiplecolumns! use t"e folloing SQL syntax/

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    CO'STR>'T uc_%e"son B'NBE (%_dLast'ame)

    )

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

    'o create a NIQ% constraint on t"e 6;Id6 column "en t"e table is already created! use t"e

    folloing SQL/

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

    >LTER T>!LE %e"sons

    > B'NBE (%_d)

    'o allo naming of a NIQ% constraint! and for defining a NIQ% constraint on multiplecolumns! use t"e folloing SQL syntax/

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

    >LTER T>!LE %e"sons

    > CO'STR>'T uc_%e"son B'NBE (%_dLast'ame)

    To DR! a UN#QU, Constraint

    'o drop a NIQ% constraint! use t"e folloing SQL/

  • 8/12/2019 Tips Fro SQL

    49/107

    M'SQL:

    >LTER T>!LE %e"sons

    RO% 'E uc_%e"son

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

    >LTER T>!LE %e"sons

    RO% CO'STR>'T uc_%e"son

    SQL *I,!& 'E& Constraint

    SQL !R#M&RY (,Y Constraint

    '"e +I-A+B :%B constraint uniquely identifies eac" record in a database table.

    rimary 2eys must contain unique values.

    A primary 2ey column cannot contain NLL values.

    %ac" table s"ould "ave a primary 2ey! and eac" table can "ave only 5N% primary 2ey.

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

    '"e folloing SQL creates a +I-A+B :%B on t"e 6;Id6 column "en t"e 6ersons6 table is

    created/

    M'SQL:

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLLFi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    %RM>R3 &E3 (%_d)

    )

  • 8/12/2019 Tips Fro SQL

    50/107

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL %RM>R3 &E3

    Last'ame va"ca"(66) 'OT 'BLLFi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    )

    'o allo naming of a +I-A+B :%B constraint! and for defining a +I-A+B :%B constraint

    on multiple columns! use t"e folloing SQL syntax/

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    CO'STR>'T - %RM>R3 &E3 (%_d)

    'o allo naming of a +I-A+B :%B constraint! and for defining a +I-A+B :%B constrainton multiple columns! use t"e folloing SQL syntax/

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

  • 8/12/2019 Tips Fro SQL

    51/107

    >LTER T>!LE %e"sons

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

    RO% %RM>R3 &E3

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

    >LTER T>!LE %e"sons

    RO% CO'STR>'T -

  • 8/12/2019 Tips Fro SQL

    52/107

    O_Id OrderNo P_Id

    1 46 $

    @@5 $

    $ @65

    @ @65 1

    Note t"at t"e 6;Id6 column in t"e 65rders6 table points to t"e 6;Id6 column in t"e 6ersons6

    table.

    '"e 6;Id6 column in t"e 6ersons6 table is t"e +I-A+B :%B in t"e 6ersons6 table.

    '"e 6;Id6 column in t"e 65rders6 table is a ra*e > MS &**ess:

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

    (

  • 8/12/2019 Tips Fro SQL

    53/107

    O_d int 'OT 'BLL %RM>R3 &E3

    O"de"'o int 'OT 'BLL

    %_d int FORE:' &E3 REFERE'CES %e"sons(%_d)

    )

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

    >LTER T>!LE O"de"s

    > CO'STR>'T ;

  • 8/12/2019 Tips Fro SQL

    54/107

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

    'o drop a ra*e > MS &**ess:

    >LTER T>!LE O"de"s

    RO% CO'STR>'T ;

  • 8/12/2019 Tips Fro SQL

    55/107

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    CHEC& (%_d90)

    )

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL CHEC& (%_d90)

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66))

    'o allo naming of a %&: constraint! and for defining a %&: constraint on multiple

    columns! use t"e folloing SQL syntax/

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

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    CO'STR>'T c

  • 8/12/2019 Tips Fro SQL

    56/107

    > CHEC& (%_d90)

    'o allo naming of a %&: constraint! and for defining a %&: constraint on multiplecolumns! use t"e folloing SQL syntax/

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

    >LTER T>!LE %e"sons

    > CO'STR>'T c ra*e > MS &**ess:

    >LTER T>!LE %e"sons

    RO% CO'STR>'T c

  • 8/12/2019 Tips Fro SQL

    57/107

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66) EF>BLT /Sandnes/

    )

    '"e *%BLT :ET>TE()

    )

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

    'o create a *%LTER T>!LE %e"sons

    >LTER Cit+ SET EF>BLT /S>''ES/

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

    >LTER T>!LE %e"sons

    >LTER COLBM' Cit+ SET EF>BLT /S>''ES/

    To DR! a D,%&ULT Constraint

    'o drop a *%

  • 8/12/2019 Tips Fro SQL

    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 CE!TE INDE1 Statement

    '"e &+%A'% IN*%K statement is used to create indexes in tables.

    Indexes allo t"e database application to find data fast it"out reading t"e "ole table.

    #nde/es

    An index can be created in a table to find data more quic2ly and efficiently.

    '"e users cannot see t"e indexes! t"ey are $ust used to speed up searc"es3queries.

    Note:pdating a table it" indexes ta2es more time t"an updating a table it"out (because t"eindexes also need an update). So you s"ould only create indexes on columns (and tables) t"at

    ill be frequently searc"ed against.

    SQL CE!TE INDE1 Syntax

    &reates an index on a table. *uplicate values are alloed/

    CRE>TE 'E inde=_name

    O' table_name (column_name)

    SQL CE!TE )NIQ)E INDE1 Syntax

    &reates a unique index on a table. *uplicate values are not alloed/

    CRE>TE B'NBE 'E inde=_name

    O' table_name (column_name)

  • 8/12/2019 Tips Fro SQL

    59/107

    Note:'"e syntax for creating indexes varies amongst different databases. '"erefore/ &"ec2 t"e

    syntax for creating indexes in your database.

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

    '"e SQL statement belo creates an index named 6Index6 on t"e 6LastName6 column in t"e6ersons6 table/

    CRE>TE 'E %nde=

    O' %e"sons (Last'ame)

    If you ant to create an index on a combination of columns! you can list t"e column namesit"in t"e parent"eses! separated by commas/

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

    SQL D#* INDE12 D#* T!%LE2 and D#* D!T!%!SE

    Indexes! tables! and databases can easily be deleted3removed it" t"e *+5 statement.

    The DR! #ND, State$ent

    '"e *+5 IN*%K statement is used to delete an index in a table.

    D#* INDE1 Syntax for ,S !ccess

    RO% 'E inde=_name O' table_name

    D#* INDE1 Syntax for ,S SQL Ser+er

    RO% 'E table_nameinde=_name

    D#* INDE1 Syntax for D%45#racle

    RO% 'E inde=_name

    D#* INDE1 Syntax for ,ySQL

    >LTER T>!LE table_name RO% 'E inde=_name

  • 8/12/2019 Tips Fro SQL

    60/107

    The DR! T&BL, State$ent'"e *+5 'A0L% statement is used to delete a table.

    RO% T>!LE table_name

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

    '"e *+5 *A'A0AS% statement is used to delete a database.

    RO% >T>!>SE database_name

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

    ,"at if e only ant to delete t"e data inside t"e table! and not t"e table itself

    '"en! use t"e '+N&A'% 'A0L% statement/

    TRB'C>TE T>!LE table_name

    SQL !LTE T!%LE Statement

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

    '"e AL'%+ 'A0L% statement is used to add! delete! or modify columns in an existing table.

    SQL !LTE T!%LE Syntax

    'o add a column in a table! use t"e folloing syntax/

    >LTER T>!LE table_name

  • 8/12/2019 Tips Fro SQL

    61/107

    > column_name datat+-e

    'o delete a column in a table! use t"e folloing syntax (notice t"at some database systems donCtallo deleting a column)/

    >LTER T>!LE table_name

    RO% COLBM' column_name

    'o c"ange t"e data type of a column in a table! use t"e folloing syntax/

    >LTER T>!LE table_name

    >LTER COLBM' column_name datat+-e

    SQL &LT,R T&BL, ,/a$)e

    Loo2 at t"e 6ersons6 table/

    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"

    No e ant to add a column named 6*ate5f0irt"6 in t"e 6ersons6 table.

    ,e use t"e folloing SQL statement/

    >LTER T>!LE %e"sons

    > ateO;!i"t date

    Notice t"at t"e ne column! 6*ate5f0irt"6! is of type date and is going to "old a date. '"e datatype specifies "at type of data t"e column can "old.

  • 8/12/2019 Tips Fro SQL

    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

    No e ant to c"ange t"e data type of t"e column named 6*ate5f0irt"6 in t"e 6ersons6 table.

    ,e use t"e folloing SQL statement/

    >LTER T>!LE %e"sons

    >LTER COLBM' ateO;!i"t +ea"

    Notice t"at t"e 6*ate5f0irt"6 column is no of type year and is going to "old a year in a to1digit or four1digit format.

    DR! CLUMN ,/a$)e

    Next! e ant to delete t"e column named 6*ate5f0irt"6 in t"e 6ersons6 table.

    ,e use t"e folloing SQL statement/

    >LTER T>!LE %e"sons

    RO% COLBM' ateO;!i"t

    '"e 6ersons6 table ill no li2e 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/12/2019 Tips Fro SQL

    63/107

    SQL !)T# INCE,ENT /ield

    Auto1increment allos a unique number to be generated "en a ne record is inserted into a

    table.

    &UT #NCR,M,NT a %ied

    Mery often e ould li2e t"e value of t"e primary 2ey field to be created automatically everytime a ne record is inserted.

    ,e ould li2e to create an auto1increment field in a table.

    S'nta/ +or M'SQL

    '"e folloing SQL statement defines t"e 6;Id6 column to be an auto1increment primary 2ey

    field in t"e 6ersons6 table/

    CRE>TE T>!LE %e"sons

    (

    %_d int 'OT 'BLL >BTO_'CREME'T

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)>dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    %RM>R3 &E3 (%_d)

    )

    -ySQL uses t"e A'5;IN&+%-%N' 2eyord to perform an auto1increment feature.

    0y default! t"e starting value for A'5;IN&+%-%N' is 7! and it ill increment by 7 for eac"

    ne record.

    'o let t"e A'5;IN&+%-%N' sequence start it" anot"er value! use t"e folloing SQL

    statement/

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

  • 8/12/2019 Tips Fro SQL

    64/107

    'o insert a ne record into t"e 6ersons6 table! e ill not "ave to specify a value for t"e 6;Id6

    column (a unique value ill be added automatically)/

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

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

    '"e SQL statement above ould insert a ne record into t"e 6ersons6 table. '"e 6;Id6 column

    ould be assigned a unique value. '"e 6TE T>!LE %e"sons

    (

    %_d int %RM>R3 &E3 E'TT3

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    )

    '"e -S SQL Server uses t"e I*%N'I'B 2eyord to perform an auto1increment feature.

    0y default! t"e starting value for I*%N'I'B is 7! and it ill increment by 7 for eac" ne record.

    'o specify t"at t"e 6;Id6 column s"ould start at value 78 and increment by E! c"ange t"e

    identity to I*%N'I'B(78!E).

    'o insert a ne record into t"e 6ersons6 table! e ill not "ave to specify a value for t"e 6;Id6column (a unique value ill be added automatically)/

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

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

    '"e SQL statement above ould insert a ne record into t"e 6ersons6 table. '"e 6;Id6 columnould be assigned a unique value. '"e 6

  • 8/12/2019 Tips Fro SQL

    65/107

    S'nta/ +or &**ess

    '"e folloing SQL statement defines t"e 6;Id6 column to be an auto1increment primary 2ey

    field in t"e 6ersons6 table/

    CRE>TE T>!LE %e"sons(

    %_d %RM>R3 &E3 >BTO'CREME'T

    Last'ame va"ca"(66) 'OT 'BLL

    Fi"st'ame va"ca"(66)

    >dd"ess va"ca"(66)

    Cit+ va"ca"(66)

    )

    '"e -S Access uses t"e A'5IN&+%-%N' 2eyord to perform an auto1increment feature.

    0y default! t"e starting value for A'5IN&+%-%N' is 7! and it ill increment by 7 for eac"ne record.

    'o specify t"at t"e 6;Id6 column s"ould start at value 78 and increment by E! c"ange t"e

    autoincrement to A'5IN&+%-%N'(78!E).

    'o insert a ne record into t"e 6ersons6 table! e ill not "ave to specify a value for t"e 6;Id6

    column (a unique value ill be added automatically)/

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

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

    '"e SQL statement above ould insert a ne record into t"e 6ersons6 table. '"e 6;Id6 column

    ould be assigned a unique value. '"e 6

  • 8/12/2019 Tips Fro SQL

    66/107

    M'A>LBE 1

    ST>RT ,TH 1

    'CREME'T !3 1

    C>CHE 10

    '"e code above creates a sequence ob$ect called seq;person! t"at starts it" 7 and ill increment

    by 7. It ill also cac"e up to 78 values for performance. '"e cac"e option specifies "o many

    sequence values ill be stored in memory for faster access.

    'o insert a ne record into t"e 6ersons6 table! e ill "ave to use t"e nextval function (t"is

    function retrieves t"e next value from seq;person sequence)/

    'SERT 'TO %e"sons (%_dFi"st'ameLast'ame)

    A>LBES (se7_-e"sonne=tval/La"s//Monsen/)

    '"e SQL statement above ould insert a ne record into t"e 6ersons6 table. '"e 6;Id6 columnould be assigned t"e next number from t"e seq;person sequence. '"e 6

  • 8/12/2019 Tips Fro SQL

    67/107

    ,HERE condition

    Note:A vie alays s"os up1to1date data '"e database engine recreates t"e data! using t"evieCs SQL statement! every time a user queries a vie.

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

    If you "ave t"e Nort"ind database you can see t"at it "as several vies installed by default.

    '"e vie 6&urrent roduct List6 lists all active products (products t"at are not discontinued)

    from t"e 6roducts6 table. '"e vie is created it" t"e folloing SQL/

    CRE>TE AE, ICu""ent %"oduct ListJ >S

    SELECT %"oduct%"oduct'ame

    FROM %"oducts,HERE iscontinued.'o

    ,e can query t"e vie above as follos/

    SELECT * FROM ICu""ent %"oduct ListJ

    Anot"er vie in t"e Nort"ind sample database selects every product in t"e 6roducts6 table

    it" a unit price "ig"er t"an t"e average unit price/

    CRE>TE AE, I%"oducts >bove >ve"a#e %"iceJ >S

    SELECT %"oduct'ameBnit%"ice

    FROM %"oducts

    ,HERE Bnit%"ice9(SELECT >A:(Bnit%"ice) FROM %"oducts)

    ,e can query t"e vie above as follos/

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

    Anot"er vie in t"e Nort"ind database calculates t"e total sale for eac" category in 7O. Notet"at t"is vie selects its data from anot"er vie called 6roduct Sales for 7O6/

    CRE>TE AE, ICate#o"+ Sales Fo" 144J >S

    SELECT ST'CT Cate#o"+'ameSum(%"oductSales) >S Cate#o"+Sales

    FROM I%"oduct Sales ;o" 144J

    :ROB% !3 Cate#o"+'ame

  • 8/12/2019 Tips Fro SQL

    68/107

    ,e can query t"e vie above as follos/

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

    ,e can also add a condition to t"e query. No e ant to see t"e total sale only for t"e category

    60everages6/

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

    ,HERE Cate#o"+'ame./!eve"a#es/

    SQL U)dating a ie2

    Bou can update a vie by using t"e folloing syntax/

    SQL CE!TE # E*L!CE 6IEW Syntax

    CRE>TE OR RE%L>CE AE, vie2_name >S

    SELECT column_name(s)

    FROM table_name

    ,HERE condition

    No e ant to add t"e 6&ategory6 column to t"e 6&urrent roduct List6 vie. ,e ill update

    t"e vie it" t"e folloing SQL/

    CRE>TE AE, ICu""ent %"oduct ListJ >S

    SELECT %"oduct%"oduct'ameCate#o"+

    FROM %"oducts

    ,HERE iscontinued.'o

    SQL Dro))ing a ie2

    Bou can delete a vie it" t"e *+5 MI%, command.

    SQL D#* 6IEW Syntax

    RO% AE, vie2_name

  • 8/12/2019 Tips Fro SQL

    69/107

    SQL Date /unctions

    SQL Dates'"e most difficult part "en or2ing it" dates is to be sure t"at t"e format of t"e date you

    are trying to insert! matc"es t"e format of t"e date column in t"e database.

    As long as your data contains only t"e date portion! your queries ill or2 as expected.#oever! if a time portion is involved! it gets complicated.

    0efore tal2ing about t"e complications of querying for dates! e ill loo2 at t"e most important

    built1in functions for or2ing it" dates.

    M'SQL Date %un*tions

    '"e folloing table lists t"e most important built1in date functions in -ySQL/

    F%nction Description

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

    CBR>TE() Retu"ns te cu""ent date

    CBRTME() Retu"ns te cu""ent time

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

    ETR>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

    >TEFF() Retu"ns te 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/12/2019 Tips Fro SQL

    70/107

    '"e folloing table lists t"e most important built1in date functions in SQL Server/

    F%nction Description

    :ET>TE() Retu"ns te 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

    >TEFF() Retu"ns te time bet2een t2o dates

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

    SQL Date Data T')es

    M'SQLcomes it" t"e folloing data types for storing a date or a date3time value in t"e

    database/

    >TE Q ;o"mat 3333QMMQ

    >TETME Q ;o"mat 3333QMMQ HHMMSS

    TMEST>M% Q ;o"mat 3333QMMQ HHMMSS

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

    SQL Ser5ercomes it" t"e folloing data types for storing a date or a date3time value in t"edatabase/

    >TE Q ;o"mat 3333QMMQ

    >TETME Q ;o"mat 3333QMMQ HHMMSS

    SM>LL>TETME Q ;o"mat 3333QMMQ HHMMSS

    TMEST>M% Q ;o"mat a uni7ue numbe"

    Note:'"e date types are c"osen for a column "en you create a ne table in your database

  • 8/12/2019 Tips Fro SQL

    71/107

    Assume e "ave t"e folloing 65rders6 table/

    OrderId Prod%ctName OrderDate

    1 :eitost 00Q11Q11

    Camembe"t %ie""ot 00Q11Q04

    $ Moa"ella di :iovanni 00Q11Q11

    @ Masca"-one Fabioli 00Q10Q4

    No e ant to select t"e records it" an 5rder*ate of 6488P1771776 from t"e table above.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    OrderId Prod%ctName OrderDate

    1 :eitost 00Q11Q11

    $ Moa"ella di :iovanni 00Q11Q11

    No! assume t"at t"e 65rders6 table loo2s li2e t"is (notice t"e time component in t"e

    65rder*ate6 column)/

    OrderId Prod%ctName OrderDate

    1 :eitost 00Q11Q11 1$$@@

    Camembe"t %ie""ot 00Q11Q04 16@61

    $ Moa"ella di :iovanni 00Q11Q11 11101

    @ Masca"-one Fabioli 00Q10Q4 1@6564

    If e use t"e same S%L%&' statement as above/

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

  • 8/12/2019 Tips Fro SQL

    72/107

    e ill get no result '"is is because t"e query is loo2ing only for dates it" no time portion.

    Ti):If you ant to 2eep your queries simple and easy to maintain! do not allo time

    components in your dates

    SQL N)LL 6alues

    NLL values represent missing un2non data.

    0y default! a table column can "old NLL values.

    '"is c"apter ill explain t"e IS NLL and IS N5' NLL operators.

    SQL NULL aues

    If a column in a table is optional! e can insert a ne record or update an existing record it"out

    adding a value to t"is column. '"is means t"at t"e field ill be saved it" a NLL value.

    NLL values are treated differently from ot"er values.

    NLL is used as a place"older for un2non or inapplicable values.

    Note:It is not possible to compare NLL and 8 t"ey are not equivalent.

    SQL Wor=ing 2ith NULL aues

    Loo2 at t"e folloing 6ersons6 table/

    P_Id LastName FirstName Address City

    1 Hansen Ola Sandnes

    Svendson Tove !o"#vn $ Sandnes

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

    Suppose t"at t"e 6Address6 column in t"e 6ersons6 table is optional. '"is means t"at if e

    insert a record it" no value for t"e 6Address6 column! t"e 6Address6 column ill be saved it"

    a NLL value.

  • 8/12/2019 Tips Fro SQL

    73/107

    #o can e test for NLL values

    It is not possible to test for NLL values it" comparison operators! suc" as @! >! or >?.

    ,e ill "ave to use t"e IS NLL and IS N5' NLL operators instead.

    SQL #S NULL

    #o do e select only t"e records it" NLL values in t"e 6Address6 column

    ,e ill "ave to use t"e IS NLL operator/

    SELECT Last'ameFi"st'ame>dd"ess FROM %e"sons

    ,HERE >dd"ess S 'BLL

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName Address

    Hansen Ola

    %ette"sen &a"i

    Ti):Alays use IS NLL to loo2 for NLL values.

    SQL #S NT NULL

    #o do e select only t"e records it" no NLL values in t"e 6Address6 column

    ,e ill "ave to use t"e IS N5' NLL operator/

    SELECT Last'ameFi"st'ame>dd"ess FROM %e"sons

    ,HERE >dd"ess S 'OT 'BLL

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName Address

    Svendson Tove !o"#vn $

  • 8/12/2019 Tips Fro SQL

    74/107

    In t"e next c"apter e ill loo2 at t"e ISNLL()! NML()! I

  • 8/12/2019 Tips Fro SQL

    75/107

    ra*e

    5racle does not "ave an ISNLL() function. #oever! e can use t"e NML() function to

    ac"ieve t"e same result/

    SELECT %"oduct'ameBnit%"ice*(BnitsnStoc

  • 8/12/2019 Tips Fro SQL

    76/107

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

    Lon# >llo2s 2ole numbe"s bet2een Q1@@$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; 2ole dolla"s -lus @

    decimal -laces Tip)3ou can coose 2ic count"+/s cu""enc+ to

    use

    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 te constants T"ue and False (e7uivalent to Q1 and 0)

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

    1 bit

    Ole Obect Can sto"e -ictu"es audio video o" ote" !LO!s (!ina"+ La"#e

    O!ects)

    u- to

    1:!

    H+-e"lin< Contain linR(sie) Holds a ;i=ed len#t st"in# (can contain lette"s numbe"s and s-ecial

    ca"acte"s) Te ;i=ed sie is s-eci;ied in -a"entesis Can sto"e u- to 66

    ca"acte"s

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

  • 8/12/2019 Tips Fro SQL

    77/107

    ca"acte"s) Te ma=imum sie is s-eci;ied in -a"entesis Can sto"e u- to

    66 ca"acte"s Note); +ou -ut a #"eate" value tan 66 it 2ill be conve"ted

    to a TET t+-e

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

    TET Holds a st"in# 2it a ma=imum len#t o; 566$6 ca"acte"s

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

    MEBMTET Holds a st"in# 2it a ma=imum len#t o; 1516 ca"acte"s

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

    LO':TET Holds a st"in# 2it a ma=imum len#t o; @4@4546 ca"acte"s

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

    E'BM(=+etc) Let +ou ente" a list o; -ossible values 3ou can list u- to 566$6 values in an

    E'BM list ; a value is inse"ted tat is not in te list a blan< value 2ill be

    inse"ted

    Note:'"e values are sorted in t"e order you enter t"em.

    Bou enter t"e possible values in t"is format/ %N-(CKC!CBC!CRC)

    SET Simila" to E'BM e=ce-t tat SET ma+ contain u- to 5@ list items and can

    sto"e mo"e tan one coice

    Nu$ber t')es:

    Data type Description

    T'3'T(sie) Q1 to 1 no"mal 0 to 66 B'S:'E* Te ma=imum numbe" o; di#its

    ma+ be s-eci;ied in -a"entesis

    SM>LL'T(sie) Q$5 to $5 no"mal 0 to 566$6 B'S:'E* Te ma=imum numbe" o;

    di#its ma+ be s-eci;ied in -a"entesis

    MEBM'T(sie) Q$50 to $50 no"mal 0 to 1516 B'S:'E* Te ma=imumnumbe" o; di#its ma+ be s-eci;ied in -a"entesis

    'T(sie) Q1@@$5@ to 1@@$5@ no"mal 0 to @4@4546 B'S:'E* Te

    ma=imum numbe" o; di#its ma+ be s-eci;ied in -a"entesis

    !:'T(sie) Q4$$0$56@60 to 4$$0$56@60 no"mal 0 to

    1@@5@@0$04661516 B'S:'E* Te ma=imum numbe" o; di#its ma+

  • 8/12/2019 Tips Fro SQL

    78/107

    be s-eci;ied in -a"entesis

    FLO>T(sied) > small numbe" 2it a ;loatin# decimal -oint Te ma=imum numbe" o; di#its

    ma+ be s-eci;ied in te sie -a"amete" Te ma=imum numbe" o; di#its to te

    "i#t o; te decimal -oint is s-eci;ied in te d -a"amete"

    OB!LE(sied) > la"#e numbe" 2it a ;loatin# decimal -oint Te ma=imum numbe" o; di#its

    ma+ be s-eci;ied in te sie -a"amete" Te ma=imum numbe" o; di#its to te

    "i#t o; te decimal -oint is s-eci;ied in te d -a"amete"

    ECM>L(sied) > OB!LE sto"ed as a st"in# allo2in# ;o" a ;i=ed decimal -oint Te

    ma=imum numbe" o; di#its ma+ be s-eci;ied in te sie -a"amete" Te

    ma=imum numbe" o; di#its to te "i#t o; te decimal -oint is s-eci;ied in te

    d -a"amete"

    ='"e integer types "ave an extra option called NSIJN%*. Normally! t"e integer goes from annegative to positive value. Adding t"e NSIJN%* attribute ill move t"at range up so it starts

    at ero instead of a negative number.

    Date t')es:

    Data type Description

    >TE() > date Fo"mat 3333QMMQ

    Note:'"e supported range is from C7888187187C to C174197C

    >TETME() *> date and time combination Fo"mat 3333QMMQ HHMMSS

    Note:'"e supported range is from C7888187187 88/88/88C to C17419749/E/EC

    TMEST>M%() *> timestam- TMEST>M% values a"e sto"ed as te numbe" o; seconds

    since te Bni= e-oc (/140Q01Q01 000000/ BTC) Fo"mat 3333QMMQ

    HHMMSS

    Note:'"e supported range is from C7O8187187 88/88/87C '& to C489P1871

    8 89/7/8OC '&

    TME() > time Fo"mat HHMMSS

    Note:'"e supported range is from C1P9P/E/EC to CP9P/E/EC

    3E>R() > +ea" in t2oQdi#it o" ;ou"Qdi#it ;o"mat

    Note:Malues alloed in four1digit format/ 787 to 47EE. Malues alloed in

    to1digit format/ O8 to T! representing years from 7O8 to 48T

  • 8/12/2019 Tips Fro SQL

    79/107

    =%ven if *A'%'I-% and 'I-%S'A- return t"e same format! t"ey or2 very differently. In

    an INS%+' or *A'% query! t"e 'I-%S'A- automatically set itself to t"e current date and

    time. 'I-%S'A- also accepts various formats! li2e BBBB--**##--SS!BB--**##--SS! BBBB--**! or BB--**.

    SQL Ser5er Data T')es

    Chara*ter strings:

    Data type Description &tora(e

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

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

    va"ca"(ma=) Aa"iableQlen#t ca"acte" st"in# Ma=imum 10$@1@

    ca"acte"s

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

    Uni*ode strings:

    Data type Description &tora(e

    nca"(n) Fi=edQlen#t Bnicode data Ma=imum @000 ca"acte"s

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

    nva"ca"(ma=) Aa"iableQlen#t Bnicode data Ma=imum 6$5041 ca"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/12/2019 Tips Fro SQL

    80/107

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

    Nu$ber t')es:

    Data type Description &tora(e

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

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

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

    bi#int >llo2s 2ole numbe"s bet2een Q4$$0$56@60 and

    4$$0$56@60

    b+tes

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

    Allos numbers from 178U9P V7 to 78U9P W7.

    '"e p parameter indicates t"e maximum total number of digits t"at

    can be stored (bot" to t"e left and to t"e rig"t of t"e decimal point).p must be a value from 7 to 9P. *efault is 7P.

    '"e s parameter indicates t"e maximum number of digits stored to

    t"e rig"t of t"e decimal point. s must be a value from 8 to p. *efault

    value is 8

    6Q1

    b+tes

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

    Allos numbers from 178U9P V7 to 78U9P W7.

    '"e p parameter indicates t"e maximum total number of digits t"atcan be stored (bot" to t"e left and to t"e rig"t of t"e decimal point).

    p must be a value from 7 to 9P. *efault is 7P.

    '"e s parameter indicates t"e maximum number of digits stored to

    t"e rig"t of t"e decimal point. s must be a value from 8 to p. *efaultvalue is 8

    6Q1b+tes

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

    mone+ Moneta"+ data ;"om Q4$$0$56@60 to

    4$$0$56@60

    b+tes

    ;loat(n) Floatin# -"ecision numbe" data ;"om Q14E $0 to 14E $0

    '"e n parameter indicates "et"er t"e field s"ould "old or P

    @ o"

    b+tes

  • 8/12/2019 Tips Fro SQL

    81/107

    bytes. float(4) "olds a 1byte field and float(E9) "olds an P1bytefield. *efault value of n is E9.

    "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 16$ 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 04 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 Te same as datetime 2it te addition o; a time one o;;set Q10

    b+tes

    timestam- Sto"es a uni7ue numbe" tat #ets u-dated eve"+ time a "o2 #ets

    c"eated o" modi;ied Te timestam- value is based u-on an inte"nal

    cloc< and does not co""es-ond to "eal time Eac table ma+ ave

    onl+ 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 and

    timestam-

    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/12/2019 Tips Fro SQL

    82/107

    SQL /unctions

    SQL "as many built1in functions for performing calculations on data.

    SQL &ggregate %un*tions

    SQL aggregate functions return a single value! calculated from values in a column.

    seful aggregate functions/

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

    FRST() Q Retu"ns te ;i"st value

    L>ST() Q Retu"ns te last value

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

    M'() Q Retu"ns te smallest value

    SBM() Q Retu"ns te sum

    SQL S*aar +un*tions

    SQL scalar functions return a single value! based on t"e input value.

    seful 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 ca"acte"s ;"om a te=t ;ield

    LE'() Q Retu"ns te len#t o; a te=t ;ield

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

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

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

  • 8/12/2019 Tips Fro SQL

    83/107

    Ti):'"e aggregate functions and t"e scalar functions ill be explained in details in t"e next

    c"apters.

    SQL !6078 /unction

    The &;@A %un*tion

    '"e AMJ() function returns t"e average value of a numeric column.

    SQL !6078 Syntax

    SELECT >A:(column_name) FROM table_name

    SQL &;@A ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e average value of t"e 65rderrice6 fields.

    ,e use t"e folloing SQL statement/

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

    '"e result1set ill loo2 li2e t"is/

  • 8/12/2019 Tips Fro SQL

    84/107

    OrderA*era(e

    460

    No e ant to find t"e customers t"at "ave an 5rderrice value "ig"er t"an t"e average5rderrice value.

    ,e use t"e folloing SQL statement/

    SELECT Custome" FROM O"de"s

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

    '"e result1set ill loo2 li2e t"is/

    C%stomer

    Hansen

    'ilsen

    Densen

    SQL C#)NT78 /unction

    '"e &5N'() function returns t"e number of ros t"at matc"es a specified criteria.

    SQL C#)NT7column9name8 Syntax

    '"e &5N'(column;name) function returns t"e number of values (NLL values ill not be

    counted) of t"e specified column/

    SELECT COB'T(column_name) FROM table_name

    SQL C#)NT7:8 Syntax

    '"e &5N'(=) function returns t"e number of records in a table/

    SELECT COB'T(*) FROM table_name

  • 8/12/2019 Tips Fro SQL

    85/107

    SQL C#)NT7DISTINCT column9name8 Syntax

    '"e &5N'(*IS'IN&' column;name) function returns t"e number of distinct values of t"e

    specified column/

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

    Note:&5N'(*IS'IN&') or2s it" 5+A&L% and -icrosoft SQL Server! but not it"

    -icrosoft Access.

    SQL CUNT@*ou$n"na$eA ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to count t"e number of orders from 6&ustomer Nilsen6.

    ,e use t"e folloing SQL statement/

    SELECT COB'T(Custome") >S Custome"'ilsen FROM O"de"s

    ,HERE Custome"./'ilsen/

    '"e result of t"e SQL statement above ill be 4! because t"e customer Nilsen "as made 4 ordersin total/

    C%stomerNilsen

  • 8/12/2019 Tips Fro SQL

    86/107

    SQL CUNT@0A ,/a$)e

    If e omit t"e ,#%+% clause! li2e t"is/

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

    '"e result1set ill loo2 li2e t"is/

    N%m+erO"Orders

    5

    "ic" is t"e total number of ros in t"e table.

    SQL CUNT@D#ST#NCT *ou$n"na$eA ,/a$)e

    No e ant to count t"e number of unique customers in t"e 65rders6 table.

    ,e use t"e folloing SQL statement/

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

    '"e result1set ill loo2 li2e t"is/

    N%m+erO"C%stomers

    $

    "ic" is t"e number of unique customers (#ansen! Nilsen! and Densen) in t"e 65rders6 table.

    SQL /IST78 /unction

    The %#RST@A %un*tion

    '"e

  • 8/12/2019 Tips Fro SQL

    87/107

    SQL /IST78 Syntax

    SELECT FRST(column_name) FROM table_name

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

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e first value of t"e 65rderrice6 column.

    ,e use t"e folloing SQL statement/

    SELECT FRST(O"de"%"ice) >S Fi"stO"de"%"ice FROM O"de"s

    Ti):,or2around if

  • 8/12/2019 Tips Fro SQL

    88/107

    SQL L!ST78 /unction

    The L&ST@A %un*tion'"e LAS'() function returns t"e last value of t"e selected column.

    SQL L!ST78 Syntax

    SELECT L>ST(column_name) FROM table_name

    SQL L&ST@A ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e last value of t"e 65rderrice6 column.

    ,e use t"e folloing SQL statement/

    SELECT L>ST(O"de"%"ice) >S LastO"de"%"ice FROM O"de"s

    Ti):,or2around if LAS'() function is not supported/

    SELECT O"de"%"ice FROM O"de"s ORER !3 O_d ESC LMT 1

    '"e result1set ill loo2 li2e t"is/

  • 8/12/2019 Tips Fro SQL

    89/107

    LastOrderPrice

    100

    SQL ,!178 /unction

    The M&@A %un*tion

    '"e -AK() function returns t"e largest value of t"e selected column.

    SQL ,!178 Syntax

    SELECT M>(column_name) FROM table_name

    SQL M&@A ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e largest value of t"e 65rderrice6 column.

    ,e use t"e folloing SQL statement/

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

  • 8/12/2019 Tips Fro SQL

    90/107

    '"e result1set ill loo2 li2e t"is/

    Lar(estOrderPrice

    000

    SQL ,IN78 /unction

    The M#N@A %un*tion

    '"e -IN() function returns t"e smallest value of t"e selected column.

    SQL ,IN78 Syntax

    SELECT M'(column_name) FROM table_name

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

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e smallest value of t"e 65rderrice6 column.

    ,e use t"e folloing SQL statement/

  • 8/12/2019 Tips Fro SQL

    91/107

    SELECT M'(O"de"%"ice) >S SmallestO"de"%"ice FROM O"de"s

    '"e result1set ill loo2 li2e t"is/

    &mallestOrderPrice

    100

    SQL S),78 /unction

    The SUM@A %un*tion

    '"e S-() function returns t"e total sum of a numeric column.

    SQL S),78 Syntax

    SELECT SBM(column_name) FROM table_name

    SQL SUM@A ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e sum of all 65rderrice6 fields6.

  • 8/12/2019 Tips Fro SQL

    92/107

    ,e use t"e folloing SQL statement/

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

    '"e result1set ill loo2 li2e t"is/

    OrderTotal

    600

    SQL 0#)* %& Statement

    Aggregate functions often need an added J+5 0B statement.

    The ;RU! BY State$ent

    '"e J+5 0B statement is used in con$unction it" t"e aggregate functions to group t"e

    result1set by one or more columns.

    SQL 0#)* %& 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 ;RU! BY ,/a$)e

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

  • 8/12/2019 Tips Fro SQL

    93/107

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find t"e total sum (total order) of eac" customer.

    ,e ill "ave to use t"e J+5 0B statement to group t"e customers.

    ,e use t"e folloing SQL statement/

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

    :ROB% !3 Custome"

    '"e result1set ill loo2 li2e t"is/

    C%stomer &!,-OrderPrice.

    Hansen 000

    'ilsen 100

    Densen 000

    Nice IsnCt it /)

    LetCs see "at "appens if e omit t"e J+5 0B statement/

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

    '"e result1set ill loo2 li2e t"is/

    C%stomer &!,-OrderPrice.

    Hansen 600

    'ilsen 600

    Hansen 600

    Hansen 600

  • 8/12/2019 Tips Fro SQL

    94/107

    Densen 600

    'ilsen 600

    '"e result1set above is not "at e anted.

    ,/)anation o+ 2h' the abo5e S,L,CT state$ent *annot be used:'"e S%L%&' statement

    above "as to columns specified (&ustomer and S-(5rderrice). '"e 6S-(5rderrice)6

    returns a single value (t"at is t"e total sum of t"e 65rderrice6 column)! "ile 6&ustomer6returns T values (one value for eac" ro in t"e 65rders6 table). '"is ill t"erefore not give us t"e

    correct result. #oever! you "ave seen t"at t"e J+5 0B statement solves t"is problem.

    ;RU! BY More Than ne Cou$n

    ,e can also use t"e J+5 0B statement on more t"an one column! li2e t"is/

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

    :ROB% !3 Custome"O"de"ate

    SQL H!6IN0 Clause

    The 1N; Cause

    '"e #AMINJ clause as added to SQL because t"e ,#%+% 2eyord could not be used it"

    aggregate functions.

    SQL H!6IN0 Syntax

    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': a##"e#ate_;unction(column_name) o-e"ato" value

    SQL 1N; ,/a$)e

  • 8/12/2019 Tips Fro SQL

    95/107

    ,e "ave t"e folloing 65rders6 table/

    O_Id OrderDate OrderPrice C%stomer

    1 00P11P1 1000 Hansen

    00P10P$ 1500 'ilsen

    $ 00P04P0 00 Hansen

    @ 00P04P0$ $00 Hansen

    6 00P0P$0 000 Densen

    5 00P10P0@ 100 'ilsen

    No e ant to find if any of t"e customers "ave a total order of less t"an 4888.

    ,e use t"e folloing SQL statement/

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

    :ROB% !3 Custome"

    H>A': SBM(O"de"%"ice)8000

    '"e result1set ill loo2 li2e t"is/

    C%stomer &!,-OrderPrice.

    'ilsen 100

    No e ant to find if t"e customers 6#ansen6 or 6Densen6 "ave a total order of more t"an

    7E88.

    ,e add an ordinary ,#%+% 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

    '"e result1set ill loo2 li2e t"is/

    C%stomer &!,-OrderPrice.

  • 8/12/2019 Tips Fro SQL

    96/107

    Hansen 000

    Densen 000

    SQL )C!SE78 /unction

    The UC&S,@A %un*tion

    '"e &AS%() function converts t"e value of a field to uppercase.

    SQL )C!SE78 Syntax

    SELECT BC>SE(column_name) FROM table_name

    Syntax for SQL Ser+er

    SELECT B%%ER(column_name) FROM table_name

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

    ,e "ave t"e folloing 6ersons6 table/

    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"

    No e ant to select t"e content of t"e 6LastName6 and 6

  • 8/12/2019 Tips Fro SQL

    97/107

    '"e result1set ill loo2 li2e t"is/

    LastName FirstName

    H>'SE' Ola

    SAE'SO' Tove

    %ETTERSE' &a"i

    SQL LC!SE78 /unction

    The LC&S,@A %un*tion

    '"e L&AS%() function converts t"e value of a field to loercase.

    SQL LC!SE78 Syntax

    SELECT LC>SE(column_name) FROM table_name

    Syntax for SQL Ser+erSELECT LO,ER(column_name) FROM table_name

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

    ,e "ave t"e folloing 6ersons6 table/

    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/12/2019 Tips Fro SQL

    98/107

    No e ant to select t"e content of t"e 6LastName6 and 6

  • 8/12/2019 Tips Fro SQL

    99/107

    ,e "ave t"e folloing 6ersons6 table/

    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"

    No e ant to extract t"e first four c"aracters of t"e 6&ity6 column above.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    &mallCity

    Sand

    Sand

    Stav

    SQL LEN78 /unction

    The L,N@A %un*tion

    '"e L%N() function returns t"e lengt" of t"e value in a text field.

    SQL LEN78 SyntaxSELECT LE'(column_name) FROM table_name

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

  • 8/12/2019 Tips Fro SQL

    100/107

    ,e "ave t"e folloing 6ersons6 table/

    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"

    No e ant to select t"e lengt" of t"e values in t"e 6Address6 column above.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    Len(t$O"Address

    1

    4

    4

    SQL #)ND78 /unction

    The RUND@A %un*tion

    '"e +5N*() function is used to round a numeric field to t"e number of decimals specified.

    SQL #)ND78 SyntaxSELECT ROB'(column_namedecimals) FROM table_name

    Parameter Description

    column_name Re7ui"ed Te ;ield to "ound

  • 8/12/2019 Tips Fro SQL

    101/107

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

    SQL RUND@A ,/a$)e

    ,e "ave t"e folloing 6roducts6 table/

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10@6

    Masca"-one 1000 # $65

    $ :o"#onola 1000 # 165

    No e ant to display t"e product name and t"e price rounded to t"e nearest integer.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    Prod%ctName !nitPrice

    Da"lsbe"# 10

    Masca"-one $$

    :o"#onola 15

    SQL N#W78 /unction

    The NW@A %un*tion

    '"e N5,() function returns t"e current system date and time.

  • 8/12/2019 Tips Fro SQL

    102/107

    SQL N#W78 Syntax

    SELECT 'O,() FROM table_name

    SQL NW@A ,/a$)e

    ,e "ave t"e folloing 6roducts6 table/

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10@6

    Masca"-one 1000 # $65

    $ :o"#onola 1000 # 165

    No e ant to display t"e products and prices per todayCs date.

    ,e use t"e folloing S%L%&' statement/

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

    '"e result1set ill loo2 li2e t"is/

    Prod%ctName !nitPrice PerDate

    Da"lsbe"# 10@6 10PP00 1160 >M

    Masca"-one $65 10PP00 1160 >M

    :o"#onola 165 10PP00 1160 >M

    SQL /#,!T78 /unction

    The %RM&T@A %un*tion

    '"e

  • 8/12/2019 Tips Fro SQL

    103/107

    SQL /#,!T78 Syntax

    SELECT FORM>T(column_name;o"mat) FROM table_name

    Parameter Description

    column_name Re7ui"ed Te ;ield to be ;o"matted

    ;o"mat Re7ui"ed S-eci;ies te ;o"mat

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

    ,e "ave t"e folloing 6roducts6 table/

    Prod_Id Prod%ctName !nit !nitPrice

    1 Da"lsbe"# 1000 # 10@6

    Masca"-one 1000 # $65

    $ :o"#onola 1000 # 165

    No e ant to display t"e products and prices per todayCs date (it" todayCs date displayed int"e folloing format 6BBBB1--1**6).

    ,e use t"e folloing S%L%&' statement/

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

    FROM %"oducts

    '"e result1set ill loo2 li2e t"is/

    Prod%ctName !nitPrice PerDate

    Da"lsbe"# 10@6 00Q10Q0

    Masca"-one $65 00Q10Q0

    :o"#onola 165 00Q10Q0

  • 8/12/2019 Tips Fro SQL

    104/107

    SQL Quic; eference /rom W3Schools

    &/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

    AL'%+ 'A0L% table;name*+5 &5L-N column;name

    >S (alias) SELECT column_name >S column_alias

    FROM table_name

    or

    S%L%&' 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+-e

    column_name data_t+-e

    column_name data_t+-e

    )

    CRE>TE 'E CRE>TE 'E inde=_name

    O' table_name (column_name)

    or

  • 8/12/2019 Tips Fro SQL

    105/107

    &+%A'% NIQ% IN*%K index;name5N table;name (column;name)

    CRE>TE AE, CRE>TE AE, vie2_name >S

    SELECT column_name(s)

    FROM table_name

    ,HERE condition

    ELETE ELETE FROM table_name

    ,HERE some_column.some_value

    or

    *%L%'% T>!>SE RO% >T>!>SE database_name

    RO% 'E RO% 'E table_nameinde=_name (SNL Se"ve")

    RO% 'E inde=_name O' table_name (MS >ccess)

    RO% 'E inde=_name (!PO"acle)

    >LTER T>!LE table_name

    RO% '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_name

    H>A': a##"e#ate_;unction(column_name) o-e"ato" value

    ' SELECT column_name(s)

    FROM table_name

    ,HERE column_name

    ' (value1value)

    'SERT 'TO 'SERT 'TO table_name

  • 8/12/2019 Tips Fro SQL

    106/107

    A>LBES (value1 value value$)

    or

    INS%+' IN'5 table;name

    (column7! column4! column9!...)MAL%S (value7! value4! value9!....)

    ''ER DO' SELECT column_name(s)

    FROM table_name1

    ''ER DO' table_name

    O' table_name1column_name.table_namecolumn_name

    LEFT DO' SELECT column_name(s)

    FROM table_name1

    LEFT DO' table_name

    O' table_name1column_name.table_namecolumn_name

    R:HT DO' SELECT column_name(s)

    FROM table_name1

    R:HT DO' table_name

    O' table_name1column_name.table_namecolumn_name

    FBLL DO' SELECT column_name(s)

    FROM table_name1

    FBLL DO' table_name

    O' table_name1column_name.table_namecolumn_name

    L&E SELECT column_name(s)FROM table_name

    ,HERE column_name L&E -atte"n

    ORER !3 SELECT column_name(s)

    FROM table_name

    ORER !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/12/2019 Tips Fro SQL

    107/107

    FROM old_table_name

    or

    S%L%&' column;name(s)

    IN'5 ne;table;name GIN externaldatabaseHTE T>!LE table_name

    B'O' SELECT column_name(s) FROM table_name1

    B'O'

    SELECT co