what is sql1

Upload: goldybatra

Post on 06-Jul-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 What is SQL1

    1/62

    What is SQL?

    SQL is Structured Query Language, which is a computer language for storing, manipulatingand retrieving data stored in relational database. SQL is the standard language for RelationDatabase System. All relational database management systems like ySQL, S Access,!racle, Sybase, "nformi#, postgres and SQL Server use SQL as standard database language.

    History:

    1970 -- Dr. $dgar %. &'ed& (odd of ") is known as the father of relational databases.*e described a relational model for databases.

    • 1974 -- Structured Query Language appeared.

    • 1978 -- ") worked to develop (odd+s ideas and released a product named SystemR.

    • 1986 -- ") developed the first prototype of relational database and standardi-ed by

     AS". 'he first relational database was released by Relational Software and its laterbecoming !racle.

    Advantages SQL?•  Allows users to access data in relational database management systems.

    •  Allows users to describe the data.

    •  Allows users to define the data in database and manipulate that data.

    •  Allows to embed within other languages using SQL modules, libraries / pre0compilers.

    •  Allows users to create and drop databases and tables.

    •  Allows users to create view, stored procedure, functions in a database.

    •  Allows users to set permissions on tables, procedures, and views

    SQL Proess! SQL Arhitet"re:

    1hen you are e#ecuting an SQL command for any RD)S, the system determines the best

    way to carry out your re2uest and SQL engine figures out how to interpret the task.

    'here are various components included in the process. 'hese components are QueryDispatcher, !ptimi-ation $ngines, (lassic Query $ngine and SQL Query $ngine, etc. (lassic2uery engine handles all non0SQL 2ueries but SQL 2uery engine won+t handle logical files.

    %ollowing is a simple diagram showing SQL Architecture3

  • 8/16/2019 What is SQL1

    2/62

    SQL #o$$ands:

    'he standard SQL commands to interact with relational databases are (R$A'$, S$L$(',"S$R', 45DA'$, D$L$'$ and DR!5. 'hese commands can be classified into groups basedon their nature3

    %%L - %ata %e&inition Lang"age:

    Data Definition Language '%%L( is a standard &or o$$ands that define the di&&erent

    str"t"res in a data)ase* %%L state$ents reate+ $odi&y+ and re$ove data)ase o),ets

    s"h as ta)es+ inde.es+ and "sers* #o$$on %%L state$ents are #/A+ AL/+ and

    %/2P*

    #o$$and %esri3tion

    (R$A'$ (reates a new table, a view of a table, or other ob6ect in database

     AL'$R odifies an e#isting database ob6ect, such as a table.

    DR!5 Deletes an entire table, a view of a table or other ob6ect in the database.

    %L - %ata ani3"ation Lang"age:

    A data manipulation language '%L( is a &a$iy o& synta. ee$ents si$iar to a o$3"ter 

    3rogra$$ing language "sed &or seeting+ inserting+ deeting and "3dating data in a

    data)ase* Per&or$ing read-ony 5"eries o& data is so$eti$es aso onsidered a

    o$3onent o& %L

    #o$$and %esri3tion

    S$L$(' Retrieves certain records from one or more tables

    "S$R' (reates a record

    45DA'$ odifies records

    D$L$'$ Deletes records

    %#L - %ata #ontro Lang"age:

    A data control language '%#L( is a synta. si$iar to a o$3"ter

    3rogra$$ing language "sed to control aess to data stored in a data)ase

    'A"thoriation(* n 3arti"ar+ it is a o$3onent o& Str"t"red Q"ery Language 'SQL(*

    .a$3es o& %#L o$$ands in"de: /A to ao s3ei&ied "sers to 3er&or$

    s3ei&ied tas;s*

    #o$$and %esri3tion

  • 8/16/2019 What is SQL1

    3/62

    7RA' 7ives a privilege to user 

    R$8!9$ 'akes back privileges granted from user 

     A ransation #ontro Lang"age :'(L; is a computer ang"age and a subset of SQL, usedto ontro transationa processing in a database. A transation is logical unit of work thatcomprises one or more SQL statements, usually a group of Data

    anipulation Lang"age :DL; statements.

    What is table?

    The data in RDBMS is stored in database objects called tables. The table is a

    collection of related data entries and it consists of columns and rows.

    Remember, a table is the most common and simplest form of data storage in a

    relational database. Following is the example of a !ST"M#RS table$

    +----+----------+-----+-----------+----------+| ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    What is field?

    #%er& table is bro'en up into smaller entities called fields. The fields in the

    !ST"M#RS table consist of (D, )*M#, *+#, *DDR#SS and S**R-.

    * field is a column in a table that is designed to maintain specific information

    about e%er& record in the table.

    What is record or row?

    * record, also called a row of data, is each indi%idual entr& that exists in a table.

    For example there are records in the abo%e !ST"M#RS table. Following is a

    single row of data or record in the !ST"M#RS table$

  • 8/16/2019 What is SQL1

    4/62

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    +----+----------+-----+-----------+----------+

    * record is a hori/ontal entit& in a table.

    What is column?* column is a %ertical entit& in a table that contains all information associated with

    a specific field in a table.

    For example, a column in the !ST"M#RS table is *DDR#SS, which represents

    location description and would consist of the following$

    +-----------+

    | ADDRESS |

    +-----------+

    | Ahmedabad |

    | Delhi  |

    | K#$a  |

    | M"mbai  |

    | h#,al  |

    | M |

    | Ind#)e  |

    +----+------+

    SQL Syntax

    S0 is followed b& uni1ue set of rules and guidelines called S&ntax. *ll the S0

    statements start with an& of the 'e&words li'e S##T, ()S#RT, !2D*T#, D##T#,

    *T#R, DR"2, R#*T#, !S#, S3"4 and all the statements end with a semicolon

    567.

    (mportant point to be noted is that S0 is case insensitive, which means S##T

    and select ha%e same meaning in S0 statements

    S0 S##T Statement$

    SELE& #l"mn1 #l"mn2....#l"mnN

    4R5M $able6name7

  • 8/16/2019 What is SQL1

    5/62

    S0 D(ST()T lause$

    SELE& DISIN& #l"mn1 #l"mn2....#l"mnN

    4R5M $able6name7

    S0 43#R# lause$

    SELE& #l"mn1 #l"mn2....#l"mnN

    4R5M $able6name

    8(ERE &5NDII5N7

    S0 *)D8"R lause$

    SELE& #l"mn1 #l"mn2....#l"mnN

    4R5M $able6name

    8(ERE &5NDII5N-1 9AND|5R: &5NDII5N-27

    S0 () lause$

    SELE& #l"mn1 #l"mn2....#l"mnN

    4R5M $able6name

    8(ERE #l"mn6name IN ;

  • 8/16/2019 What is SQL1

    6/62

    SELE& S>M;#l"mn6name=

    4R5M $able6name

    8(ERE &5NDII5N

    GR5> Y #l"mn6name7

    S0 "!)T lause$

    SELE& &5>N;#l"mn6name=

    4R5M $able6name

    8(ERE &5NDII5N7

    S0 3*:()+ lause$

    SELE& S>M;#l"mn6name=

    4R5M $able6name

    8(ERE &5NDII5N

    GR5> Y #l"mn6name

    (A?ING ;a)i$hema$i @"n$i#n #ndi$i#n=7

    S0 R#*T# T*B# Statement$

    &REAE ALE $able6name;

    #l"mn1 da$a$,e

    #l"mn2 da$a$,e

    #l"mn3 da$a$,e.....

    #l"mnN da$a$,e

    RIMARY KEY; #ne #) m#)e #l"mns =

    =7

    S0 DR"2 T*B# Statement$

    DR5 ALE $able6name7

    S0 R#*T# ()D#; Statement $

    &REAE >NI>E INDEB indeC6name

    5N $able6name ; #l"mn1 #l"mn2...#l"mnN=7

    S0 DR"2 ()D#; Statement $

    ALER ALE $able6name

  • 8/16/2019 What is SQL1

    7/62

    DR5 INDEB indeC6name7

    S0 D#S Statement $

    DES& $able6name7

    S0 TR!)*T# T*B# Statement$

     R>N&AE ALE $able6name7

    S0 *T#R T*B# Statement$

    ALER ALE $able6name 9ADD|DR5|M5DI4Y: #l"mn6name 9da$a6,e:7

    S0 *T#R T*B# Statement 5Rename7 $

    ALER ALE $able6name RENAME 5 ne6$able6name7

    S0 ()S#RT ()T" Statement$

    INSER IN5 $able6name; #l"mn1 #l"mn2....#l"mnN=?AL>ES ; SE da$abase6name7

    S0 "MM(T Statement$

    &5MMI7

  • 8/16/2019 What is SQL1

    8/62

    S0 R"B*9 Statement$

    R5LLA&K 7

    Data 'ype

    S0 data t&pe is an attribute that specifies t&pe of data of an& object. #achcolumn, %ariable and expression has related data t&pe in S0.

    -ou would use these data t&pes while creating &our tables. -ou would choose a

    particular data t&pe for a table column based on &our re1uirement.

    S0 Ser%er offers six categories of data t&pes for &our use <

    #xact )umeric Data T&pes

    DATATYP !"#$ T#

    bigint =>,??@,@?,A@,CE,,CAC >,??@,@?,A@,CE,,CA

    int =?,E,EC@,EC ?,E,EC@,E

    smallint =@?,C @?,

    tin&int A ?

    bit A

    decimal =AG@C H AG@C =

    numeric =AG@C H AG@C =

    mone& =>??,@@,?A@,C,E.CAC H>??,@@,?A@,C,E.CA

    smallmone& =?E,EC.@EC H?E,EC.@E

  • 8/16/2019 What is SQL1

    9/62

    *pproximate )umeric Data T&pes

    DATA TYP !"#$ T#

    float =.># H @AC .># H @AC

    real [email protected]# H @C @.EA# H @C

    Date and Time Data T&pes

    DATA TYP !"#$ T#

    datetime Ian , @ Dec @, >>>>

    smalldatetime Ian , >AA Iun , ?A>

    date Stores a date li'e Iune @A, >>

    time Stores a time of da& li'e ?$@A 2.M.

    %ote < 3ere, datetime has @.@@ milliseconds accurac& where as smalldatetime

    has minute accurac&.

    haracter Strings Data T&pes

    DATA TYP Descri&tion

    char Maximum length of C,AAA characters.5 Fixed length

    non=!nicode characters7

    %archar Maximum of C,AAA characters.5:ariable=length

    non=!nicode data7.

    %archar5max7 Maximum length of ?@characters, :ariable=lengthnon=!nicode data 5S0 Ser%er ?AA onl&7.

    text :ariable=length non=!nicode data with a maximum

  • 8/16/2019 What is SQL1

    10/62

    length of ?,E,EC@,E characters.

    !nicode haracter Strings Data T&pes

    DATA TYP Descri&tion

    nchar Maximum length of E,AAA characters.5 Fixed length!nicode7

    n%archar Maximum length of E,AAA characters.5:ariable length

    !nicode7

    n%archar5max7 Maximum length of ?@characters 5S0 Ser%er ?AA

    onl&7.5 :ariable length !nicode7

    ntext Maximum length of ,A@,E,C?@ characters.

    5 :ariable length !nicode 7

    Binar& Data T&pes

    DATA TYP Descri&tion

    binar& Maximum length of C,AAA b&tes5Fixed=length binar&

    data 7

    %arbinar& Maximum length of C,AAA b&tes.5:ariable length

    binar& data7

    %arbinar&5max7 Maximum length of ?@ b&tes 5S0 Ser%er ?AA

    onl&7. 5 :ariable length Binar& data7

    image Maximum length of ?,E,EC@,E b&tes. 5 :ariablelength Binar& Data7

    Misc Data T&pes

    DATA TYP Descri&tion

  • 8/16/2019 What is SQL1

    11/62

    s1lJ%ariant Stores %alues of %arious S0 Ser%er=supported datat&pes, except text, ntext, and timestamp.

    timestamp Stores a database=wide uni1ue number that getsupdated e%er& time a row gets updated

    uni1ueidentifier Stores a globall& uni1ue identifier 5+!(D7

    xml Stores ;M data. -ou can store xml instances in a

    column or a %ariable 5S0 Ser%er ?AA onl&7.

    cursor Reference to a cursor object

    table Stores a result set for later processing

    4hat is an "perator in S0K

    *n operator is a reser%ed word or a character used primaril& in an S0 statementLs

    43#R# clause to perform operation5s7, such as comparisons and arithmetic

    operations.

    "perators are used to specif& conditions in an S0 statement and to ser%e asconjunctions for multiple conditions in a statement.

    • *rithmetic operators

    • omparison operators

    • ogical operators

    • "perators used to negate conditions

    S0 *rithmetic "perators$

    *ssume %ariable a holds A and %ariable b holds ?A, then$

    #&erator Descri&tion xam&le

  • 8/16/2019 What is SQL1

    12/62

    H *ddition = *dds %alues on either side of theoperator

    a H b willgi%e @A

    = Subtraction = Subtracts right hand operand fromleft hand operand

    a = b willgi%e =A

    Multiplication = Multiplies %alues on either side of

    the operator

    a b will

    gi%e ?AA

     8 Di%ision = Di%ides left hand operand b& right hand

    operand

    b 8 a will

    gi%e ?

    N Modulus = Di%ides left hand operand b& right hand

    operand and returns remainder

    b N a will

    gi%e A

    S0 omparison "perators$

    *ssume %ariable a holds A and %ariable b holds ?A, then$

    #&erator Descri&tion xam&le

    O hec's if the %alues of two operands are e1ual or

    not, if &es then condition becomes true.

    5a O b7 is

    not true.

    PO hec's if the %alues of two operands are e1ual or

    not, if %alues are not e1ual then condition

    becomes true.

    5a PO b7 is

    true.

    Q hec's if the %alues of two operands are e1ual or

    not, if %alues are not e1ual then conditionbecomes true.

    5a Q b7 is

    true.

    hec's if the %alue of left operand is greater thanthe %alue of right operand, if &es then condition

    becomes true.

    5a b7 isnot true.

    Q hec's if the %alue of left operand is less than the%alue of right operand, if &es then condition

    5a Q b7 is

  • 8/16/2019 What is SQL1

    13/62

    becomes true. true.

    O hec's if the %alue of left operand is greater than

    or e1ual to the %alue of right operand, if &es thencondition becomes true.

    5a O b7 is

    not true.

    QO hec's if the %alue of left operand is less than or

    e1ual to the %alue of right operand, if &es thencondition becomes true.

    5a QO b7 is

    true.

    PQ hec's if the %alue of left operand is not less thanthe %alue of right operand, if &es then condition

    becomes true.

    5a PQ b7 isfalse.

    P hec's if the %alue of left operand is not greaterthan the %alue of right operand, if &es then

    condition becomes true.

    5a P b7 istrue.

    S0 ogical "perators$

    3ere is a list of all the logical operators a%ailable in S0.

    #&erator Descri&tion

    * The * operator is used to compare a %alue to all %alues inanother %alue set.

    *)D The *)D operator allows the existence of multiple conditions

    in an S0 statementLs 43#R# clause.

    *)- The *)- operator is used to compare a %alue to an&

    applicable %alue in the list according to the condition.

    B#T4##) The B#T4##) operator is used to search for %alues that are

    within a set of %alues, gi%en the minimum %alue and the

    maximum %alue.

    #;(STS The #;(STS operator is used to search for the presence of a

  • 8/16/2019 What is SQL1

    14/62

    row in a specified table that meets certain criteria.

    () The () operator is used to compare a %alue to a list of literal

    %alues that ha%e been specified.

    (9# The (9# operator is used to compare a %alue to similar

    %alues using wildcard operators.

    )"T The )"T operator re%erses the meaning of the logical

    operator with which it is used. #g$ )"T #;(STS, )"T

    B#T4##), )"T (), etc. This is a ne'ate o&erator(

    "R The "R operator is used to combine multiple conditions in an

    S0 statementLs 43#R# clause.

    (S )! The )! operator is used to compare a %alue with a )!

    %alue.

    !)(0!# The !)(0!# operator searches e%er& row of a specified tablefor uni1ueness 5no duplicates7.

    x&ression

    *n expression is a combination of one or more %alues, operators, and S0

    functions that e%aluate to a %alue.

    S0 #;2R#SS(")s are li'e formulas and the& are written in 1uer& language. -ou

    can also use them to 1uer& the database for specific set of data.

    S&ntax$

    onsider the basic s&ntax of the S##T statement as follows$

    SELE& #l"mn1 #l"mn2 #l"mnN

    4R5M $able6name

    8(ERE F&5NDII5N|EBRESSI5N7

    There are different t&pes of S0 expressions, which are mentioned below$

  • 8/16/2019 What is SQL1

    15/62

    S0 = Boolean #xpressions$

    S0 Boolean #xpressions fetch the data on the basis of matching single %alue.

    Following is the s&ntax$

    SELE& #l"mn1 #l"mn2 #l"mnN

    4R5M $able6name

    8(ERE SINGLE ?AL>E MA&(ING EBRESSI5N7

    onsider the !ST"M#RS table ha%ing the following records$

    SLH SELE&  4R5M &>S5MERS7

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 ||  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+* )#s in se$ ;0.00 se=

    3ere is simple example showing usage of S0 Boolean #xpressions$

    SLH SELE&  4R5M &>S5MERS 8(ERE SALARY  100007

    +----+-------+-----+---------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+-------+-----+---------+----------+

    |  * | M"/ |  2% | Ind#)e  | 10000.00 |

    +----+-------+-----+---------+----------+

    1 )# in se$ ;0.00 se=

    S0 = )umeric #xpression$

  • 8/16/2019 What is SQL1

    16/62

    This expression is used to perform an& mathematical operation in an& 1uer&.

    Following is the s&ntax$

    SELE& n"me)ial6eC,)essi#n as  5ERAI5N6NAME

    F4R5M $able6name

    8(ERE &5NDII5N 7

    3ere numericalJexpression is used for mathematical expression or an& formula.

    Following is a simple examples showing usage of S0 )umeric #xpressions$

    SLH SELE& ;1 + '= AS ADDII5N

    +----------+

    | ADDII5N |

    +----------+

    |  21 |+----------+

    1 )# in se$ ;0.00 se=

    There are se%eral built=in functions li'e a%g57, sum57, count57, etc., to perform

    what is 'nown as aggregate data calculations against a table or a specific table

    column.

    SLH SELE& &5>N;= AS JRE&5RDSJ 4R5M &>S5MERS7 

    +---------+

    | RE&5RDS |

    +---------+

    |  * |

    +---------+

    1 )# in se$ ;0.00 se=

    S0 = Date #xpressions$

    Date #xpressions return current s&stem date and time %alues$

    SLH  SELE& &>RREN6IMESAM7

    +---------------------+

    | &"))en$6imes$am,  |

    +---------------------+

  • 8/16/2019 What is SQL1

    17/62

    | 200-11-12 0'%023 |

    +---------------------+

    1 )# in se$ ;0.00 se=

    *nother date expression is as follows$

    SLH  SELE& GEDAE;=77+-------------------------+

    | GEDAE |

    +-------------------------+

    | 200-10-22 120*1.1%0 |

    +-------------------------+

    1 )# in se$ ;0.00 se=

    )"AT DATA*AS statement

    The S0 )"AT DATA*AS statement is used to create new S0 database.

  • 8/16/2019 What is SQL1

    18/62

    S&ntax$

    Basic s&ntax of R#*T# D*T*B*S# statement is as follows$

    &REAE DAAASE Da$abaseName7

    *lwa&s database name should be uni1ue within the RDBMS.

    #xample$

    (f &ou want to create new database QtestDB, then R#*T# D*T*B*S# statement

    would be as follows$

    SLH &REAE DAAASE $es$D7

    Ma'e sure &ou ha%e admin pri%ilege before creating an& database. "nce a

    database is created, &ou can chec' it in the list of databases as follows$

    SLH S(58 DAAASES7+--------------------+

    | Da$abase  |

    +--------------------+

    | in@#)ma$i#n6shema |

    | AMR55D |

    | >5RIALS5IN |

    | msl || #)i |

    | $es$ |

    | $es$D |

    +--------------------+

    * )#s in se$ ;0.00 se=

    D"#P DATA*AS statement

    The S0 D"#P DATA*AS statement is used to drop an existing database in S0

    schema.

  • 8/16/2019 What is SQL1

    19/62

    S&ntax$

    Basic s&ntax of DR"2 D*T*B*S# statement is as follows$

    DR5 DAAASE Da$abaseName7

    *lwa&s database name should be uni1ue within the RDBMS.

    #xample$

    (f &ou want to delete an existing database QtestDB, then DR"2 D*T*B*S#

    statement would be as follows$

    SLH DR5 DAAASE $es$D7

    %#T+ Be careful before using this operation because b& deleting an existing

    database would result in loss of complete information stored in the database.

    Ma'e sure &ou ha%e admin pri%ilege before dropping an& database. "nce adatabase is dropped, &ou can chec' it in the list of databases as follows$

    SLH S(58 DAAASES7

    +--------------------+

    | Da$abase  |

    +--------------------+

    | in@#)ma$i#n6shema |

    | AMR55D |

    | >5RIALS5IN |

    | msl |

    | #)i |

    | $es$ |

    +--------------------+

    ' )#s in se$ ;0.00 se=

    ,S statement

    4hen &ou ha%e multiple databases in &our S0 Schema, then before starting &our

    operation, &ou would need to select a database where all the operations would be

    performed.

  • 8/16/2019 What is SQL1

    20/62

    The S0 ,S statement is used to select an& existing database in S0 schema.

    S&ntax$

    Basic s&ntax of !S# statement is as follows$

    >SE Da$abaseName7

    *lwa&s database name should be uni1ue within the RDBMS.

    )ow, if &ou want to wor' with *MR""D database, then &ou can execute the

    following S0 command and start wor'ing with *MR""D database$

    SLH >SE AMR55D7

    reating a basic table in%ol%es naming the table and defining its columns and each

    columnLs data t&pe.

    )"AT TA*L statement

    The S0 )"AT TA*L statement is used to create a new table.

    S&ntax$

    Basic s&ntax of R#*T# T*B# statement is as follows$

    &REAE ALE $able6name;

      #l"mn1 da$a$,e

      #l"mn2 da$a$,e

      #l"mn3 da$a$,e

      .....

      #l"mnN da$a$,e

      RIMARY KEY; #ne #) m#)e #l"mns =

    =7

    R#*T# T*B# is the 'e&word telling the database s&stem what &ou want to do. (n

    this case, &ou want to create a new table. The uni1ue name or identifier for the

    table follows the R#*T# T*B# statement.

    Then in brac'ets comes the list defining each column in the table and what sort of 

    data t&pe it is. The s&ntax becomes clearer with an example below.

  • 8/16/2019 What is SQL1

    21/62

    * cop& of an existing table can be created using a combination of the R#*T#

    T*B# statement and the S##T statement. -ou can chec' complete details at

    reate Table !sing another Table.

    #xample$

    Following is an example, which creates a !ST"M#RS table with (D as primar& 'e&

    and )"T )! are the constraints showing that these fields can not be )! whilecreating records in this table$

    SLH &REAE ALE &>S5MERS;

      ID IN N5 N>LL

      NAME ?AR&(AR ;20=  N5 N>LL

      AGE IN N5 N>LL

      ADDRESS &(AR ;2= 

      SALARY DE&IMAL ;1 2= RIMARY KEY ;ID=

    =7

    -ou can %erif& if &our table has been created successfull& b& loo'ing at the

    message displa&ed b& the S0 ser%er, otherwise &ou can use DS) command as

    follows$

    SLH DES& &>S5MERS7

    +---------+---------------+------+-----+---------+-------+

    | 4ield  |  ,e  | N"ll | Ke | De@a"l$ | EC$)a |

    +---------+---------------+------+-----+---------+-------+

    | ID | in$;11=  | N5 | RI |  |  |

    | NAME | LL |  |

    | SALARY | deimal;12= | YES |  | N>LL |  |

    +---------+---------------+------+-----+---------+-------+

     )#s in se$ ;0.00 se=

    )ow, &ou ha%e !ST"M#RS table a%ailable in &our database which &ou can use to

    store re1uired information related to customers.

    http://www.tutorialspoint.com/sql/sql-create-table-using-tables.htmhttp://www.tutorialspoint.com/sql/sql-create-table-using-tables.htm

  • 8/16/2019 What is SQL1

    22/62

    S0 D"#P TA*L statement

    The S0 D"#P TA*L statement is used to remo%e a table definition and all data,

    indexes, triggers, constraints, and permission specifications for that table.

    %#T+ -ou ha%e to be careful while using this command because once a table is

    deleted then all the information a%ailable in the table would also be lost fore%er.

    S&ntax$

    Basic s&ntax of DR"2 T*B# statement is as follows$

    DR5 ALE $able6name7

    #xample$

    et us first %erif& !ST"M#RS table and then we would delete it from the

    database$

    SLH DES& &>S5MERS7

    +---------+---------------+------+-----+---------+-------+

    | 4ield  |  ,e  | N"ll | Ke | De@a"l$ | EC$)a |

    +---------+---------------+------+-----+---------+-------+

    | ID | in$;11=  | N5 | RI |  |  |

    | NAME | LL |  |

    | SALARY | deimal;12= | YES |  | N>LL |  |

    +---------+---------------+------+-----+---------+-------+

     )#s in se$ ;0.00 se=

    This means !ST"M#RS table is a%ailable in the database, so let us drop it as

    follows$

    SLH DR5 ALE &>S5MERS7

    "e) 5K  0 )#s a/e$ed ;0.01 se=

    )ow, if &ou would tr& D#S command, then &ou would get error as follows$

    SLH DES& &>S5MERS7

    ERR5R 11%' ;%2S02=  able OES.&>S5MERSO d#esnO$ eCis$

  • 8/16/2019 What is SQL1

    23/62

    3ere, T#ST is database name which we are using for our examples.

    -%S"T -%T# Statement

    The S0 -%S"T -%T# Statement is used to add new rows of data to a table in

    the database.

    S&ntax$

    There are two basic s&ntaxes of ()S#RT ()T" statement as follows$

    INSER IN5 ALE6NAME ;#l"mn1 #l"mn2 #l"mn3...#l"mnN= 

    ?AL>ES ;

  • 8/16/2019 What is SQL1

    24/62

    -ou can populate data into a table through select statement o%er another table

    pro%ided another table has a set of fields, which are re1uired to populate first

    table. 3ere is the s&ntax$

    INSER IN5 P)s$6$able6name F;#l"mn1 #l"mn2 ... #l"mnN= 

    SELE& #l"mn1 #l"mn2 ...#l"mnN

    4R5M se#nd6$able6name

      F8(ERE #ndi$i#n7

    SL)T statement

    S0 SL)T statement is used to fetch the data from a database table which

    returns data in the form of result table. These result tables are called result=sets.

    S&ntax$

    The basic s&ntax of S##T statement is as follows$

    SELE& #l"mn1 #l"mn2 #l"mnN 4R5M $able6name7

    3ere, column, column?...are the fields of a table whose %alues &ou want to fetch.

    (f &ou want to fetch all the fields a%ailable in the field, then &ou can use the

    following s&ntax$

    SELE&  4R5M $able6name7

    #xample$

    Following is an example, which would fetch (D, )ame and Salar& fields of the

    customers a%ailable in !ST"M#RS table$

    SLH SELE& ID NAME SALARY 4R5M &>S5MERS7

    This would produce the following result$

    (f &ou want to fetch all the fields of !ST"M#RS table, then use the following

    1uer&$

    SLH SELE&  4R5M &>S5MERS7

  • 8/16/2019 What is SQL1

    25/62

    The S0 W." clause is used to specif& a condition while fetching the data from

    single table or joining with multiple tables.

    (f the gi%en condition is satisfied then onl& it returns specific %alue from the table.

    -ou would use 43#R# clause to filter the records and fetching onl& necessar&

    records.

    The 43#R# clause is not onl& used in S##T statement, but it is also used in

    !2D*T#, D##T# statement, etc., which we would examine in subse1uent

    chapters.

    S&ntax$

    The basic s&ntax of S##T statement with 43#R# clause is as follows$

    SELE& #l"mn1 #l"mn2 #l"mnN

    4R5M $able6name

    8(ERE F#ndi$i#n

    -ou can specif& a condition using comparison or logical operators li'e , Q, O,

    (9#, )"T, etc. Below examples would ma'e this concept clear.

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    http://www.tutorialspoint.com/sql/sql-operators.htmhttp://www.tutorialspoint.com/sql/sql-operators.htm

  • 8/16/2019 What is SQL1

    26/62

    Following is an example which would fetch (D, )ame and Salar& fields from the

    !ST"M#RS table where salar& is greater than ?AAA$

    SLH SELE& ID NAME SALARY

    4R5M &>S5MERS

    8(ERE SALARY H 20007

    This would produce the following result$

    +----+----------+----------+

    | ID | NAME | SALARY |

    +----+----------+----------+

    |  % | &hai$ali |  '00.00 |

    |   | (a)di!  |  00.00 |

    |  ' | K#mal  |  %00.00 ||  * | M"/  | 10000.00 |

    +----+----------+----------+

    Following is an example, which would fetch (D, )ame and Salar& fields from the

    !ST"M#RS table for a customer with name .ardi/. 3ere, it is important to note

    that all the strings should be gi%en inside single 1uotes 5LL7 where as numeric

    %alues should be gi%en without an& 1uote as in abo%e example$

    SLH SELE& ID NAME SALARY

    4R5M &>S5MERS

    8(ERE NAME  O(a)di!O7

    This would produce the following result$

    +----+----------+----------+

    | ID | NAME | SALARY |

    +----+----------+----------+|   | (a)di!  |  00.00 |

    +----+----------+----------+

  • 8/16/2019 What is SQL1

    27/62

    The S0 A%D and #"  operators are used to combine multiple conditions to

    narrow data in an S0 statement. These two operators are called conjuncti%e

    operators.

    These operators pro%ide a means to ma'e multiple comparisons with different

    operators in the same S0 statement.

    The *)D "perator$

    The A%D operator allows the existence of multiple conditions in an S0

    statementLs 43#R# clause.

    S&ntax$

    The basic s&ntax of *)D operator with 43#R# clause is as follows$

    SELE& #l"mn1 #l"mn2 #l"mnN

    4R5M $able6name

    8(ERE F#ndi$i#n1 AND F#ndi$i#n2...AND F#ndi$i#nN7

    -ou can combine ) number of conditions using *)D operator. For an action to be

    ta'en b& the S0 statement, whether it be a transaction or 1uer&, all conditions

    separated b& the *)D must be TR!#.

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

  • 8/16/2019 What is SQL1

    28/62

    Following is an example, which would fetch (D, )ame and Salar& fields from the

    !ST"M#RS table where salar& is greater than ?AAA *)D age is less tan ? &ears$

    SLH SELE& ID NAME SALARY

    4R5M &>S5MERS

    8(ERE SALARY H 2000 AND ae Q 27

    This would produce the following result$

    +----+-------+----------+

    | ID | NAME | SALARY |

    +----+-------+----------+

    |  ' | K#mal |  %00.00 |

    |  * | M"/ | 10000.00 |

    +----+-------+----------+

    The "R "perator$

    The "R operator is used to combine multiple conditions in an S0 statementLs

    43#R# clause.

    S&ntax$

    The basic s&ntax of "R operator with 43#R# clause is as follows$

    SELE& #l"mn1 #l"mn2 #l"mnN4R5M $able6name

    8(ERE F#ndi$i#n1 5R F#ndi$i#n2...5R F#ndi$i#nN

    -ou can combine ) number of conditions using "R operator. For an action to be

    ta'en b& the S0 statement, whether it be a transaction or 1uer&, onl& an& ")# of

    the conditions separated b& the "R must be TR!#.

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

  • 8/16/2019 What is SQL1

    29/62

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |+----+----------+-----+-----------+----------+

    Following is an example, which would fetch (D, )ame and Salar& fields from the

    !ST"M#RS table where salar& is greater than ?AAA "R age is less tan ? &ears$

    SLH SELE& ID NAME SALARY

    4R5M &>S5MERS

    8(ERE SALARY H 2000 5R ae Q 27

    This would produce the following result$

    +----+----------+----------+

    | ID | NAME | SALARY |

    +----+----------+----------+

    |  3 | !a"shi! |  2000.00 |

    |  % | &hai$ali |  '00.00 |

    |   | (a)di!  |  00.00 |

    |  ' | K#mal  |  %00.00 |

    |  * | M"/  | 10000.00 |

    +----+----------+----------+

    ,PDAT 0uer&

    The S0 ,PDAT 0uer& is used to modif& the existing records in a table.

    -ou can use 43#R# clause with !2D*T# 1uer& to update selected rows otherwise

    all the rows would be affected.

    S&ntax$

    The basic s&ntax of !2D*T# 1uer& with 43#R# clause is as follows$

  • 8/16/2019 What is SQL1

    30/62

    >DAE $able6name

    SE #l"mn1  DAE &>S5MERS

    SE ADDRESS  O"neO

    8(ERE ID  '7

    )ow, !ST"M#RS table would ha%e the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

  • 8/16/2019 What is SQL1

    31/62

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | "ne  |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    (f &ou want to modif& all *DDR#SS and S**R- column %alues in !ST"M#RS

    table, &ou do not need to use 43#R# clause and !2D*T# 1uer& would be as

    follows$

    SLH >DAE &>S5MERS

    SE ADDRESS  O"neO SALARY  1000.007

    )ow, !ST"M#RS table would ha%e the following records$

    +----+----------+-----+---------+---------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+---------+---------+

    |  1 | Ramesh  |  32 | "ne  | 1000.00 |

    |  2 | Khilan  |  2 | "ne  | 1000.00 |

    |  3 | !a"shi! |  23 | "ne  | 1000.00 |

    |  % | &hai$ali |  2 | "ne  | 1000.00 |

    |   | (a)di!  |  2* | "ne  | 1000.00 |

    |  ' | K#mal  |  22 | "ne  | 1000.00 |

    |  * | M"/  |  2% | "ne  | 1000.00 |

    +----+----------+-----+---------+---------+

    DLT 0uer&

    The S0 DLT 0uer& is used to delete the existing records from a table.

    -ou can use 43#R# clause with D##T# 1uer& to delete selected rows, otherwise

    all the records would be deleted.

    S&ntax$

    The basic s&ntax of D##T# 1uer& with 43#R# clause is as follows$

  • 8/16/2019 What is SQL1

    32/62

    DELEE 4R5M $able6name

    8(ERE F#ndi$i#n7

    -ou can combine ) number of conditions using *)D or "R operators.

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    Following is an example, which would D##T# a customer, whose (D is $

    SLH DELEE 4R5M &>S5MERS8(ERE ID  '7

    )ow, !ST"M#RS table would ha%e the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

  • 8/16/2019 What is SQL1

    33/62

    +----+----------+-----+-----------+----------+

    (f &ou want to D##T# all the records from !ST"M#RS table, &ou do not need to

    use 43#R# clause and D##T# 1uer& would be as follows$

    SLH DELEE 4R5M &>S5MERS7

    )ow, !ST"M#RS table would not ha%e an& record.

    S0 L-0 clause

    The S0 L-0 clause is used to compare a %alue to similar %alues using wildcard

    operators. There are two wildcards used in conjunction with the (9# operator$

    • The percent sign 5N7

    • The underscore 5J7

    The percent sign represents /ero, one, or multiple characters. The underscore

    represents a single number or character. The s&mbols can be used in

    combinations.

    S&ntax$

    The basic s&ntax of N and J is as follows$

    SELE& 4R5M $able6name

    8(ERE #l"mn LIKE OBBBBO

    #) 

    SELE& 4R5M $able6name

    8(ERE #l"mn LIKE OBBBBO

    #)

    SELE& 4R5M $able6name

    8(ERE #l"mn LIKE OBBBB6O

    #)

  • 8/16/2019 What is SQL1

    34/62

    SELE& 4R5M $able6name

    8(ERE #l"mn LIKE O6BBBBO

    #)

    SELE& 4R5M $able6name

    8(ERE #l"mn LIKE O6BBBB6O

    -ou can combine ) number of conditions using *)D or "R operators. 3ere, ;;;;

    could be an& numeric or string %alue.

    #xample$

    3ere are number of examples showing 43#R# part ha%ing different (9# clause

    with LNL and LJL operators$

    Statement Descri&tion

    43#R# S**R- (9#

    L?AANL

    Finds an& %alues that start with ?AA

    43#R# S**R- (9#

    LN?AANL

    Finds an& %alues that ha%e ?AA in an& position

    43#R# S**R- (9#

    LJAANL

    Finds an& %alues that ha%e AA in the second and

    third positions

    43#R# S**R- (9#L?JNJNL

    Finds an& %alues that start with ? and are atleast @ characters in length

    43#R# S**R- (9#LN?L

    Finds an& %alues that end with ?

    43#R# S**R- (9#

    LJ?N@L

    Finds an& %alues that ha%e a ? in the second

    position and end with a @

  • 8/16/2019 What is SQL1

    35/62

    43#R# S**R- (9#L?JJJ@L

    Finds an& %alues in a fi%e=digit number that startwith ? and end with @

    et us ta'e a real example, consider the !ST"M#RS table ha%ing the following

    records$

    +----+----------+-----+-----------+----------+| ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 ||  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    Following is an example, which would displa& all the records from !ST"M#RS

    table where S**R- starts with ?AA$

    SLH SELE&  4R5M &>S5MERS

    8(ERE SALARY LIKE O200O7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    +----+----------+-----+-----------+----------+

    The S0 T#P clause is used to fetch a T"2 ) number or ; percent records from a

    table.

  • 8/16/2019 What is SQL1

    36/62

    %ote+ *ll the databases do not support T"2 clause. For example M&S0

    supports L-$-T clause to fetch limited number of records and "racle

    uses"#W%,$ to fetch limited number of records.

    S&ntax$

    The basic s&ntax of T"2 clause with S##T statement would be as follows$

    SELE& 5 n"mbe)|,e)en$ #l"mn6name;s=

    4R5M $able6name

    8(ERE F#ndi$i#n

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 ||  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    Following is an example on S0 ser%er, which would fetch top @ records from

    !ST"M#RS table$

    SLH SELE& 5 3  4R5M &>S5MERS7

    This would produce the following result$

    +----+---------+-----+-----------+---------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+---------+-----+-----------+---------+

    |  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |

  • 8/16/2019 What is SQL1

    37/62

    |  2 | Khilan  |  2 | Delhi  | 100.00 |

    |  3 | !a"shi! |  23 | K#$a  | 2000.00 |

    +----+---------+-----+-----------+---------+

    (f &ou are using M&S0 ser%er, then here is an e1ui%alent example$

    SLH SELE&  4R5M &>S5MERSLIMI 37

    This would produce the following result$

    +----+---------+-----+-----------+---------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+---------+-----+-----------+---------+

    |  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |

    |  2 | Khilan  |  2 | Delhi  | 100.00 |

    |  3 | !a"shi! |  23 | K#$a  | 2000.00 |

    +----+---------+-----+-----------+---------+

    (f &ou are using "racle ser%er, then here is an e1ui%alent example$

    SLH SELE&  4R5M &>S5MERS

    8(ERE R58N>M Q 37

    This would produce the following result$

    +----+---------+-----+-----------+---------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+---------+-----+-----------+---------+

    |  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |

    |  2 | Khilan  |  2 | Delhi  | 100.00 |

    |  3 | !a"shi! |  23 | K#$a  | 2000.00 |+----+---------+-----+-----------+---------+

    The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one ormore columns. Some database sorts query results in ascending order by deault.

  • 8/16/2019 What is SQL1

    38/62

    Synta!"The basic synta! o ORDER BY clause is as ollo#s"

    SELE$T column%list

    &RO' table(name)*+ERE condition

    )ORDER BY column-, column, .. column/ )0S$ 1 DES$2

    You can use more than one column in the ORDER BY clause. 'a3e sure #hate4er column you are using

    to sort, that column should be in column%list.

    E!am5le"$onsider the $6STO'ERS table ha4ing the ollo#ing records"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%71 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 1

    1 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7&ollo#ing is an e!am5le, #hich #ould sort the result in ascending order by /0'E and S0L0RY"

    SQLC SELE$T &RO' $6STO'ERS

      ORDER BY /0'E, S0L0RY2This #ould 5roduce the ollo#ing result"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%71 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 > 1 $haitali 1 = 1 'umbai 1 ?=;;.;; 11 = 1 +ardi3 1 @ 1 Bho5al 1 A=;;.;; 1

    1 : 1 3aushi3 1 : 1 1 8ndore 1 -;;;;.;; 1

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7&ollo#ing is an e!am5le, #hich #ould sort the result in descending order by /0'E"

    SQLC SELE$T &RO' $6STO'ERS  ORDER BY /0'E DES$2

    This #ould 5roduce the ollo#ing result"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

  • 8/16/2019 What is SQL1

    39/62

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 11 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    1 ? 1

  • 8/16/2019 What is SQL1

    40/62

    First, let us see how the following S##T 1uer& returns duplicate salar& records$

    SLH SELE& SALARY 4R5M &>S5MERS

      5RDER Y SALARY7

    This would produce the following result where salar& ?AAA is coming twice which is

    a duplicate record from the original table.

    +----------+

    | SALARY |

    +----------+

    |  100.00 |

    |  2000.00 |

    |  2000.00 |

    |  %00.00 ||  '00.00 |

    |  00.00 |

    | 10000.00 |

    +----------+

    )ow, let us use D(ST()T 'e&word with the abo%e S##T 1uer& and see the

    result$

    SLH SELE& DISIN& SALARY 4R5M &>S5MERS

      5RDER Y SALARY7

    This would produce the following result where we do not ha%e an& duplicate entr&$

    +----------+

    | SALARY |

    +----------+

    |  100.00 |

    |  2000.00 |

    |  %00.00 |

    |  '00.00 |

    |  00.00 |

  • 8/16/2019 What is SQL1

    41/62

    | 10000.00 |

    +----------+

    The S0 #"D" *Y clause is used to sort the data in ascending or descending

    order, based on one or more columns. Some database sorts 1uer& results in

    ascending order b& default.

    S&ntax$

    The basic s&ntax of "RD#R B- clause which would be used to sort result in

    ascending or descending order is as follows$

    SELE& #l"mn-lis$

    4R5M $able6name

    F8(ERE #ndi$i#n F5RDER Y #l"mn1 #l"mn2 .. #l"mnN FAS& | DES&7

    -ou can use more than one column in the "RD#R B- clause. Ma'e sure whate%er

    column &ou are using to sort, that column should be in column=list.

    #xample$

    onsider the !ST"M#RS table ha%ing the following records$

    +----+----------+-----+-----------+----------+| ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    Following is an example, which would sort the result in ascending order b& )*M#

    and S**R-$

  • 8/16/2019 What is SQL1

    42/62

    SLH SELE&  4R5M &>S5MERS

      5RDER Y NAME SALARY7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |+----+----------+-----+-----------+----------+

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    +----+----------+-----+-----------+----------+

    Following is an example, which would sort the result in descending order b& )*M#$

    SLH SELE&  4R5M &>S5MERS

      5RDER Y NAME DES&7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    +----+----------+-----+-----------+----------+

  • 8/16/2019 What is SQL1

    43/62

    To fetch the rows with own preferred order, the S##T 1uer& would as follows$

    SLH SELE&  4R5M &>S5MERS

      5RDER Y ;&ASE ADDRESS

      8(EN ODEL(IO  (EN 1

      8(EN O(5ALO  (EN 2

      8(EN OK5AO  (EN 3

      8(EN OA(MADAADO (EN %

      8(EN OMO  (EN

      ELSE 100 END= AS& ADDRESS DES&7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 ||  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    +----+----------+-----+-----------+----------+

    This will sort customers b& *DDR#SS in &our owno"rder of preference first and in

    a natural order for the remaining addresses. *lso remaining *ddresses will be

    sorted in the re%erse alpha order.

    Constraints

    $onstraints are the rules enorced on data columns on table. These are used to limit the ty5e o data thatcan go into a table. This ensures the accuracy and reliability o the data in the database.

    $onstraints could be column le4el or table le4el. $olumn le4el constraints are a55lied only to one

    column, #hereas table le4el constraints are a55lied to the #hole table.

    &ollo#ing are commonly used constraints a4ailable in SQL. These constraints ha4e already been

    discussed in SQL % RDB'S $once5ts cha5ter but its #orth to re4ise them at this 5oint.

  • 8/16/2019 What is SQL1

    44/62

     /OT /6LL $onstraint" Ensures that a column cannot ha4e /6LL 4alue.

    DE&06LT $onstraint" ro4ides a deault 4alue or a column #hen none is s5eciied.

    6/8Q6E $onstraint" Ensures that all 4alues in a column are dierent.

    R8'0RY

  • 8/16/2019 What is SQL1

    45/62

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 ||  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  %00.00 |

    |  * | M"/  |  2% | Ind#)e  | 10000.00 |

    +----+----------+-----+-----------+----------+

    5b7 *nother table is "RD#RS as follows$

    +-----+---------------------+-------------+--------+

    |5ID | DAE | &>S5MER6ID | AM5>N |

    +-----+---------------------+-------------+--------+

    | 102 | 200-10-0 000000 |  3 |  3000 |

    | 100 | 200-10-0 000000 |  3 |  100 |

    | 101 | 200-11-20 000000 |  2 |  1'0 |

    | 103 | 200-0-20 000000 |  % |  20'0 |

    +-----+---------------------+-------------+--------+

    )ow, let us join these two tables in our S##T statement as follows$

    SLH SELE& ID NAME AGE AM5>N

      4R5M &>S5MERS 5RDERS

      8(ERE &>S5MERS.ID  5RDERS.&>S5MER6ID7

    This would produce the following result$

    +----+----------+-----+--------+

    | ID | NAME | AGE | AM5>N |

    +----+----------+-----+--------+

    |  3 | !a"shi! |  23 |  3000 |

  • 8/16/2019 What is SQL1

    46/62

    |  3 | !a"shi! |  23 |  100 |

    |  2 | Khilan  |  2 |  1'0 |

    |  % | &hai$ali |  2 |  20'0 |

    +----+----------+-----+--------+

    3ere, it is noticeable that the join is performed in the 43#R# clause. Se%eral

    operators can be used to join tables, such as O, Q, , Q, QO, O, PO, B#T4##),

    (9#, and )"T6 the& can all be used to join tables. 3owe%er, the most common

    operator is the e1ual s&mbol.

    S0 Ioin T&pes$

    There are different t&pes of joins a%ailable in S0$

    • ())#R I"()$ returns rows when there is a match in both tables.

    #FT I"()$ returns all rows from the left table, e%en if there are no matchesin the right table.

    • R(+3T I"()$ returns all rows from the right table, e%en if there are no

    matches in the left table.

    • F! I"()$ returns rows when there is a match in one of the tables.

    • S#F I"()$ is used to join a table to itself as if the table were two tables,

    temporaril& renaming at least one table in the S0 statement.

    • *RT#S(*) I"()$ returns the artesian product of the sets of records from

    the two or more joined tables.

    The most requently used and im5ortant o the Ioins is the 8//ER JO8/. They are also reerred to as anEQ68JO8/.

    The 8//ER JO8/ creates a ne# result table by combining column 4alues o t#o tables Gtable- andtableH based u5on the Ioin%5redicate. The query com5ares each ro# o table- #ith each ro# o table to

    ind all 5airs o ro#s #hich satisy the Ioin%5redicate. *hen the Ioin%5redicate is satisied, column 4alues

    or each matched 5air o ro#s o 0 and B are combined into a result ro#.

    Synta!"

    The basic synta! o 8//ER JO8/ is as ollo#s"

    SELE$T table-.column-, table.column...

    &RO' table-

    8//ER JO8/ table

    http://www.tutorialspoint.com/sql/sql-inner-joins.htmhttp://www.tutorialspoint.com/sql/sql-left-joins.htmhttp://www.tutorialspoint.com/sql/sql-right-joins.htmhttp://www.tutorialspoint.com/sql/sql-full-joins.htmhttp://www.tutorialspoint.com/sql/sql-self-joins.htmhttp://www.tutorialspoint.com/sql/sql-cartesian-joins.htmhttp://www.tutorialspoint.com/sql/sql-inner-joins.htmhttp://www.tutorialspoint.com/sql/sql-left-joins.htmhttp://www.tutorialspoint.com/sql/sql-right-joins.htmhttp://www.tutorialspoint.com/sql/sql-full-joins.htmhttp://www.tutorialspoint.com/sql/sql-self-joins.htmhttp://www.tutorialspoint.com/sql/sql-cartesian-joins.htm

  • 8/16/2019 What is SQL1

    47/62

    O/ table-.common(ield K table.common(ield2E!am5le"

    $onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%71 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 1

    1 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 17%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71 O8D 1 D0TE 1 8D 1 0'O6/T 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 11 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 1

    1 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 17%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, let us Ioin these t#o tables using 8//ER JO8/ as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE  &RO' $6STO'ERS

      8//ER JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D2This #ould 5roduce the ollo#ing result"

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 : 1 3aushi3 1 :;;; 1 ;;%-;%;A ;;";;";; 11 : 1 3aushi3 1 -=;; 1 ;;%-;%;A ;;";;";; 1

    1 1 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    The SQL LE&T JO8/ returns all ro#s rom the let table, e4en i there are no matches in the right table.

    This means that i the O/ clause matches ; GMeroH records in right table, the Ioin #ill still return a ro# inthe result, but #ith /6LL in each column rom right table.

  • 8/16/2019 What is SQL1

    48/62

    This means that a let Ioin returns all the 4alues rom the let table, 5lus matched 4alues rom the righttable or /6LL in case o no matching Ioin 5redicate.

    Synta!"

    The basic synta! o LE&T JO8/ is as ollo#s"

    SELE$T table-.column-, table.column...

    &RO' table-

    LE&T JO8/ tableO/ table-.common(ield K table.common(ield2

    +ere gi4en condition could be any gi4en e!5ression based on your requirement.

    E!am5le"

    $onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%71 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 11 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1 O8D 1 D0TE 1 $6STO'ER(8D 1 0'O6/T 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 1

    1 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 11 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, let us Ioin these t#o tables using LE&T JO8/ as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE

      &RO' $6STO'ERS

      LE&T JO8/ ORDERS  O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D2

    This #ould 5roduce the ollo#ing result"

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 - 1 Ramesh 1 /6LL 1 /6LL 1

    1 1

  • 8/16/2019 What is SQL1

    49/62

    1 : 1 3aushi3 1 -=;; 1 ;;%-;%;A ;;";;";; 11 > 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 = 1 +ardi3 1 /6LL 1 /6LL 1

    1 ? 1

  • 8/16/2019 What is SQL1

    50/62

    +----+----------+-----+-----------+----------+

    5b7 *nother table is "RD#RS as follows$

    +-----+---------------------+-------------+--------+

    |5ID | DAE | &>S5MER6ID | AM5>N |

    +-----+---------------------+-------------+--------+| 102 | 200-10-0 000000 |  3 |  3000 |

    | 100 | 200-10-0 000000 |  3 |  100 |

    | 101 | 200-11-20 000000 |  2 |  1'0 |

    | 103 | 200-0-20 000000 |  % |  20'0 |

    +-----+---------------------+-------------+--------+

    )ow, let us join these two tables using R(+3T I"() as follows$

    SLH SELE& ID NAME AM5>N DAE

      4R5M &>S5MERS

      RIG( 5IN 5RDERS

      5N &>S5MERS.ID  5RDERS.&>S5MER6ID7

    This would produce the following result$

    +------+----------+--------+---------------------+

    | ID | NAME | AM5>N | DAE |

    +------+----------+--------+---------------------+

    |  3 | !a"shi! |  3000 | 200-10-0 000000 |

    |  3 | !a"shi! |  100 | 200-10-0 000000 |

    |  2 | Khilan  |  1'0 | 200-11-20 000000 |

    |  % | &hai$ali |  20'0 | 200-0-20 000000 |

    +------+----------+--------+---------------------+

    The SQL &6LL JO8/ combines the results o both let and right outer Ioins.

    The Ioined table #ill contain all records rom both tables, and ill in /6LLs or missing matches oneither side.

  • 8/16/2019 What is SQL1

    51/62

    Synta!"The basic synta! o &6LL JO8/ is as ollo#s"

    SELE$T table-.column-, table.column...

    &RO' table-&6LL JO8/ table

    O/ table-.common(ield K table.common(ield2

    +ere gi4en condition could be any gi4en e!5ression based on your requirement.

    E!am5le"

    $onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 17%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 11 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1O8D 1 D0TE 1 $6STO'ER(8D 1 0'O6/T 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 1

    1 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 11 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, let us Ioin these t#o tables using &6LL JO8/ as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE

      &RO' $6STO'ERS  &6LL JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D2

    This #ould 5roduce the ollo#ing result"

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 - 1 Ramesh 1 /6LL 1 /6LL 1

    1 1 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 = 1 +ardi3 1 /6LL 1 /6LL 1

  • 8/16/2019 What is SQL1

    52/62

    1 ? 1

  • 8/16/2019 What is SQL1

    53/62

    | % | &hai$ali | 2 | M"mbai | '00.00 |

    | | (a)di! | 2* | h#,al | 00.00 |

    | ' | K#mal | 22 | M | %00.00 |

    | * | M"/ | 2% | Ind#)e | 10000.00 |

    +----+----------+-----+-----------+----------+

    )ow, let us join this table using S#F I"() as follows$

    SLH SELE& a.ID b.NAME a.SALARY

      4R5M &>S5MERS a &>S5MERS b

      8(ERE a.SALARY Q b.SALARY7

    This would produce the following result$

    +----+----------+---------+

    | ID | NAME | SALARY |

    +----+----------+---------+

    | 2 | Ramesh | 100.00 |

    | 2 | !a"shi! | 100.00 |

    | 1 | &hai$ali | 2000.00 |

    | 2 | &hai$ali | 100.00 |

    | 3 | &hai$ali | 2000.00 |

    | ' | &hai$ali | %00.00 |

    | 1 | (a)di! | 2000.00 |

    | 2 | (a)di! | 100.00 |

    | 3 | (a)di! | 2000.00 |

    | % | (a)di! | '00.00 |

    | ' | (a)di! | %00.00 |

    | 1 | K#mal | 2000.00 |

    | 2 | K#mal | 100.00 |

    | 3 | K#mal | 2000.00 |

    | 1 | M"/ | 2000.00 |

    | 2 | M"/ | 100.00 |

    | 3 | M"/ | 2000.00 |

  • 8/16/2019 What is SQL1

    54/62

    | % | M"/ | '00.00 |

    | | M"/ | 00.00 |

    | ' | M"/ | %00.00 |

    +----+----------+---------+

    The $0RTES80/ JO8/ or $ROSS JO8/ returns the $artesian 5roduct o the sets o records rom the

    t#o or more Ioined tables. Thus, it equates to an inner Ioin #here the Ioin%condition al#ays e4aluates to

    True or #here the Ioin%condition is absent rom the statement.

    Synta!"

    The basic synta! o $0RTES80/ JO8/ or $ROSS JO8/ is as ollo#s"

    SELE$T table-.column-, table.column...

    &RO' table-, table ), table: E!am5le"$onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%71 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 11 1 =;;.;; 1

    1 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71O8D 1 D0TE 1 $6STO'ER(8D 1 0'O6/T 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 11 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 1

    1 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7 /o#, let us Ioin these t#o tables using 8//ER JO8/ as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE  &RO' $6STO'ERS, ORDERS2

    This #ould 5roduce the ollo#ing result"

  • 8/16/2019 What is SQL1

    55/62

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 - 1 Ramesh 1 :;;; 1 ;;%-;%;A ;;";;";; 1

    1 - 1 Ramesh 1 -=;; 1 ;;%-;%;A ;;";;";; 11 - 1 Ramesh 1 -=?; 1 ;;%--%; ;;";;";; 1

    1 - 1 Ramesh 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 1 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 = 1 +ardi3 1 :;;; 1 ;;%-;%;A ;;";;";; 11 = 1 +ardi3 1 -=;; 1 ;;%-;%;A ;;";;";; 11 = 1 +ardi3 1 -=?; 1 ;;%--%; ;;";;";; 1

    1 = 1 +ardi3 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 ? 1

  • 8/16/2019 What is SQL1

    56/62

    )*+ERE condition

    6/8O/

    SELE$T column- ), column &RO' table- ), table

    )*+ERE condition

    +ere gi4en condition could be any gi4en e!5ression based on your requirement.

    E!am5le"

    $onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 17%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 11 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1O8D 1 D0TE 1 $6STO'ER(8D 1 0'O6/T 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%71 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 1

    1 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 11 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, let us Ioin these t#o tables in our SELE$T statement as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE

      &RO' $6STO'ERS  LE&T JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D

    6/8O/

      SELE$T 8D, /0'E, 0'O6/T, D0TE  &RO' $6STO'ERS

      R89+T JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D2This #ould 5roduce the ollo#ing result"

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 - 1 Ramesh 1 /6LL 1 /6LL 1

  • 8/16/2019 What is SQL1

    57/62

    1 1 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 = 1 +ardi3 1 /6LL 1 /6LL 11 ? 1

  • 8/16/2019 What is SQL1

    58/62

    1 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 17%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, let us Ioin these t#o tables in our SELE$T statement as ollo#s"

    SQLC SELE$T 8D, /0'E, 0'O6/T, D0TE  &RO' $6STO'ERS

      LE&T JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D

    6/8O/ 0LL  SELE$T 8D, /0'E, 0'O6/T, D0TE

      &RO' $6STO'ERS  R89+T JO8/ ORDERS

      O/ $6STO'ERS.8D K ORDERS.$6STO'ER(8D2

    This #ould 5roduce the ollo#ing result"

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%7

    1 8D 1 /0'E 1 0'O6/T 1 D0TE 1

    7%%%%%%7%%%%%%%%%%7%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%71 - 1 Ramesh 1 /6LL 1 /6LL 1

    1 1 1 $haitali 1 ;?; 1 ;;A%;=%; ;;";;";; 1

    1 = 1 +ardi3 1 /6LL 1 /6LL 11 ? 1

  • 8/16/2019 What is SQL1

    59/62

    S&ntax$

    The basic s&ntax of %,LL while creating a table$

    SLH &REAE ALE &>S5MERS;

      ID IN N5 N>LL

      NAME ?AR&(AR ;20=  N5 N>LL

      AGE IN N5 N>LL

      ADDRESS &(AR ;2= 

      SALARY DE&IMAL ;1 2= 

    RIMARY KEY ;ID=

    =7

    3ere, %#T %,LL signifies that column should alwa&s accept an explicit %alue of 

    the gi%en data t&pe. There are two columns where we did not use )"T )!,

    which means these columns could be )!.

    * field with a )! %alue is one that has been left blan' during record creation.

    #xample$

    The )! %alue can cause problems when selecting data, howe%er, because when

    comparing an un'nown %alue to an& other %alue, the result is alwa&s un'nown and

    not included in the final results.

    -ou must use the -S %,LL or -S %#T %,LL operators in order to chec' for a)! %alue.

    onsider the following table, !ST"M#RS ha%ing the following records$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    |  ' | K#mal  |  22 | M |  |

  • 8/16/2019 What is SQL1

    60/62

    |  * | M"/  |  2% | Ind#)e  |  |

    +----+----------+-----+-----------+----------+

    )ow, following is the usage of -S %#T %,LL operator$

    SLH SELE& ID NAME AGE ADDRESS SALARY

      4R5M &>S5MERS  8(ERE SALARY IS N5 N>LL7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+

    |  1 | Ramesh  |  32 | Ahmedabad |  2000.00 |

    |  2 | Khilan  |  2 | Delhi  |  100.00 |

    |  3 | !a"shi! |  23 | K#$a  |  2000.00 |

    |  % | &hai$ali |  2 | M"mbai  |  '00.00 |

    |   | (a)di!  |  2* | h#,al  |  00.00 |

    +----+----------+-----+-----------+----------+

    )ow, following is the usage of -S %,LL operator$

    SLH SELE& ID NAME AGE ADDRESS SALARY

      4R5M &>S5MERS

      8(ERE SALARY IS N>LL7

    This would produce the following result$

    +----+----------+-----+-----------+----------+

    | ID | NAME | AGE | ADDRESS | SALARY |

    +----+----------+-----+-----------+----------+|  ' | K#mal  |  22 | M |  |

    |  * | M"/  |  2% | Ind#)e  |  |

    +----+----------+-----+-----------+----------+

    rename table

  • 8/16/2019 What is SQL1

    61/62

    You can rename a table or a column tem5orarily by gi4ing another name 3no#n as alias.

    The use o table aliases means to rename a table in a 5articular SQL statement. The renaming is a

    tem5orary change and the actual table name does not change in the database.

    The column aliases are used to rename a tableNs columns or the 5ur5ose o a 5articular SQL query.

    Synta!"The basic synta! o table alias is as ollo#s"

    SELE$T column-, column....

    &RO' table(name 0S alias(name

    *+ERE )condition2The basic synta! o column alias is as ollo#s"

    SELE$T column(name 0S alias(name

    &RO' table(name*+ERE )condition2

    E!am5le"$onsider the ollo#ing t#o tables, GaH $6STO'ERS table is as ollo#s"

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 8D 1 /0'E 1 09E 1 0DDRESS 1 S0L0RY 17%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    1 - 1 Ramesh 1 : 1 0hmedabad 1 ;;;.;; 1

    1 1 =;;.;; 11 @ 1 'uy 1 > 1 8ndore 1 -;;;;.;; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%%%%7%%%%%%%%%%7

    GbH 0nother table is ORDERS as ollo#s"

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1O8D 1 D0TE 1 $6STO'ER(8D 1 0'O6/T 17%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

    1 -; 1 ;;%-;%;A ;;";;";; 1 : 1 :;;; 1

    1 -;; 1 ;;%-;%;A ;;";;";; 1 : 1 -=;; 1

    1 -;- 1 ;;%--%; ;;";;";; 1 1 -=?; 11 -;: 1 ;;A%;=%; ;;";;";; 1 > 1 ;?; 1

    7%%%%%7%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%7%%%%%%%%7

     /o#, ollo#ing is the usage o table alias"

    SQLC SELE$T $.8D, $./0'E, $.09E, O.0'O6/T

    &RO' $6STO'ERS 0S $, ORDERS 0S O  *+ERE $.8D K O.$6STO'ER(8D2

    This #ould 5roduce the ollo#ing result"

  • 8/16/2019 What is SQL1

    62/62

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%71 8D 1 /0'E 1 09E 1 0'O6/T 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%7

    1 : 1 3aushi3 1 : 1 :;;; 1

    1 : 1 3aushi3 1 : 1 -=;; 11 1 1 $haitali 1 = 1 ;?; 1

    7%%%%7%%%%%%%%%%7%%%%%7%%%%%%%%7

    &ollo#ing is the usage o column alias"

    SQLC SELE$T 8D 0S $6STO'ER(8D, /0'E 0S $6STO'ER(/0'E  &RO' $6STO'ERS

      *+ERE S0L0RY 8S /OT /6LL2

    This #ould 5roduce the ollo#ing result"

    7%%%%%%%%%%%%%7%%%%%%%%%%%%%%%7

    1 $6STO'ER(8D 1 $6STO'ER(/0'E 1

    7%%%%%%%%%%%%%7%%%%%%%%%%%%%%%71 - 1 Ramesh 1

    1 1 1 $haitali 1

    1 = 1 +ardi3 1

    1 ? 1